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
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