Click to See Complete Forum and Search --> : sending wave file through socket programming!


chittivamshi
June 1st, 2004, 05:22 AM
Hi everyone..


please help me in receving wave file from server on the client side.
i am using udp protocol.

here is the server code....

#include <stdio.h>
#include "winsock2.h"


struct packet
{
// declaring variables
char SenderId,MessageId;
int WaveId,volume,repetitions,puaserepit,xpos,ypos,zpos;
FILE *fp;


}pack;

void main() {

pack.SenderId='x';
pack.MessageId='y';
pack.WaveId=1;
pack.fp=fopen("FancyPants.wav","rb+");
if(pack.fp!=NULL)
printf("%d",pack.fp);


pack.volume=2;
pack.repetitions=3;
pack.puaserepit=4;
pack.xpos=4;
pack.ypos=5;
pack.zpos=6;


WSADATA wsaData;
SOCKET SendSocket;
sockaddr_in RecvAddr;
int Port = 1400;

//---------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);

//---------------------------------------------
// Create a socket for sending data
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.456.789.1")
// and the specified port number.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

//---------------------------------------------
// Send a datagram to the receiver
printf("Sending a datagram to the receiver...\n");
// strcpy(SendBuf,"hello client");


int b=sendto(SendSocket,
(const char*) &pack,
sizeof(struct packet),
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));

if(b<0)
{
printf("no bytes sent");
exit(1);
}


//---------------------------------------------
// When the application is finished sending, close the socket.
fclose(pack.fp);

printf("Finished sending. Closing socket.\n");
closesocket(SendSocket);

//---------------------------------------------
// Clean up and quit.
printf("Exiting.\n");
WSACleanup();
return;
}


client code..


#include <stdio.h>
#include "winsock2.h"
#include <stdlib.h>



struct mypacket
{
char SenderId,MessageId,wavefile;
int WaveId,volume,repetitions,pauserepit,xpos,ypos,zpos;
FILE *fp;


}RecvBuf;


void main()
{
char SendId,MesId;
int WaveId,vol,rep,prepit,xp,yp,zp;
FILE *fpoint;
char ch;

WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
int Port = 1400;
// char RecvBuf[255];
//int BufLen = 1024;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);

//-----------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);

//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));

//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
printf("Receiving datagrams...\n");

int a=recvfrom(RecvSocket,
(char*)&RecvBuf,
sizeof(RecvBuf),
0,
(SOCKADDR *)&SenderAddr,
&SenderAddrSize);

if(a<0)
{

printf("no bytes recieved");
exit(1);
}

printf("number of bytes received %d\n", a);


SendId=RecvBuf.SenderId;
printf("%c",SendId);
MesId=RecvBuf.MessageId;
if(SendId=='x' && MesId=='y') // checking for senderid and message id
{
printf("sender id is %c\n",RecvBuf.SenderId);
printf("message id is %c\n",MesId);
WaveId=RecvBuf.WaveId;
printf("wave id is:%d\n",WaveId);
vol=RecvBuf.volume;
printf("volume is %d\n",vol);
rep=RecvBuf.repetitions;
printf("repetitions are %d\n",rep);
prepit=RecvBuf.pauserepit;
printf("pause are %d\n",prepit);
xp=RecvBuf.xpos;
printf("x position is %d\n",xp);
yp=RecvBuf.ypos;
printf("y position is %d\n",yp);
zp=RecvBuf.zpos;
printf("z position is %d\n",zp);
fpoint=fopen("play2.wmv","wb");
if(fpoint==NULL)
{
printf("can not open file\n");
exit(1);
}
while(!feof(RecvBuf.fp))
{
ch=fgetc(RecvBuf.fp);
if(ferror(RecvBuf.fp))
{
printf("error reading source file\n");
exit(1);
}
if(!feof(RecvBuf.fp))
{
fputc(ch,fpoint);
}
if(ferror(fpoint))
{
printf("error writing destination file\n");
exit(1);
}
}
fclose(fpoint);
}
printf("Finished receiving. Closing socket.\n");
closesocket(RecvSocket);

//-----------------------------------------------
// Clean up and exit.
printf("Exiting.\n");
WSACleanup();
return;


}


problem is with the wave file,i could not recieve or copy the wave file,
please give me the correct code .

urgent...
thanx in advance..


chitti

kuphryn
June 1st, 2004, 12:14 PM
Add breakpoints in your code. Determine exactly where the logical error, syntax error, etc. are located.

Kuphryn

chittivamshi
June 1st, 2004, 02:14 PM
hi...

i hope u could not understand the code which i posted previously,sorry for that.

here is the problem,

on client side,i can retrieve all parameters like messgeid,senderid,volume ..etc..

but i could not read the file contents.

i think my code which i have written seems to be wrong,

when i run client program i get "error reading source file"


fpoint=fopen("play2.wmv","wb");
if(fpoint==NULL)
{
printf("can not open file\n");
exit(1);
}
while(!feof(RecvBuf.fp))
{
ch=fgetc(RecvBuf.fp);
if(ferror(RecvBuf.fp))
{
printf("error reading source file\n");
exit(1);
}
if(!feof(RecvBuf.fp))
{
fputc(ch,fpoint);
}
if(ferror(fpoint))
{
printf("error writing destination file\n");
exit(1);
}
}

fclose(fpoint);
}



since the other parameters and the file descriptor are sent with the structue variable from server,
i have used the structure variable on client side to retrieve all required contents.

please help me with appropriate code.
if u dont understand still,ill explain more briefly,

tnx

j0nas
June 1st, 2004, 10:02 PM
You can't send a FILE * over the net !!!

Remove the FILE *fp member from your 'pack' structure.

chittivamshi
June 2nd, 2004, 04:32 AM
hi...
thanx for ur reply.

but i need to send wave file along with other parameters at once.

how it is possible,could u write a small code for me..

thanx..

j0nas
June 2nd, 2004, 10:17 AM
Of course, you should send the file as well as the meta data (your header)... but, you should send the file content (the data), not a file pointer (FILE *). The file pointer is just an address--it's like sending an address to a string, instead of the string itself.

I haven't work with UDP so I don't think I can help you much there (not now at least)... I believe UDP packets must be of the same size all the time.

chittivamshi
June 3rd, 2004, 04:36 AM
hi again..

is it possible to send wave file which is large in size sometimes kb or mb through udp packet...

tanx..

j0nas
June 3rd, 2004, 07:00 AM
Yes, you can send as much or little data you want.

j0nas
June 3rd, 2004, 07:07 AM
Also, if you use UDP, you have to deal with out-of-order packets. This means that you probably want to add a sequence number to all packets you send.

chittivamshi
June 3rd, 2004, 08:44 AM
is it possible to send objects through network in c++,if so could you give me some notes or tutorials where i can go through it.

does c++ supports serialization?

j0nas
June 3rd, 2004, 04:20 PM
Sure, C++ supports serialization... you just have to implement it :p

Is it important for you to use UDP? I mean, with TCP everything is easier (to me at least). There is also ready to use protocols for file transmission (FTP for instance).

There is actually a UDP file transfer protocol (TFTP)... Don't know how good it is on large files (like your .wav files).