QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 612|回复: 0

【求助】菜鸟的udp收发问题,高手帮忙!!十万火急,在线等

[复制链接]
发表于 2005-6-12 11:09:25 | 显示全部楼层 |阅读模式
我用UDP来传WAV格式的文件,发送端设定1024大小每次,用sendto()发送,接收端用recvfrom()接收。但收到返回值很小甚至到0.奇怪的是传文本文件都没问题,收发正常,就是其它二进制文件出问题。请教高手,这是什么原因啊?是不是收发函数对二进制文件有问题.?以下是程序
服务器端
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>

#include <sys/stat.h>

#include <fcntl.h>
#include<unistd.h> /*close()*/
#include<string.h> /*memset()*/
#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 1024

int main(int argc,char *argv[])
{
int sd,rc,n,cliLen,key,fd,trans,sn,i;
int ctl;
char testbuf[1024];
struct sockaddr_in cliAddr,servAddr;
char msg[MAX_MSG];
sn=0; /*audio data packet No.*/
/*--------------------------------------------*/

/*socket creation*/
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("%s:cannot open socket\n",argv[0]);
exit(1);
}

/*bind local server port*/
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(LOCAL_SERVER_PORT);
rc=bind(sd,(struct sockaddr*)&servAddr,sizeof(servAddr));
if(rc<0)
{
printf("%s:cannot bind port number %d\n",argv[0],LOCAL_SERVER_PORT);
exit(1);
}
printf("%saiting for data on port UDP %u\n",argv[0],LOCAL_SERVER_PORT);

/*-----------------------------------------------*/

/*server infinite loop*/
while(1)
{
/*init buffer*/
memset(msg,0x0,MAX_MSG);
/*receive message*/
cliLen=sizeof(cliAddr);
/*printf("waiting!\n");*/
n=recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr*)&cliAddr,&cliLen);
printf("n=%d",n);
if(n<0)
{
printf("%s:cannot receive data\n",argv[0]);
continue;
}

printf("%s:from %s:UDP%u:%s\n",argv[0],inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),msg);


if(strcmp(msg,"start")==0) /*begin sending audio data*/
{
printf("Start sending audio data now!\n");

/*open file ,read and send file.*/
if((fd=open("/home/transfile/audiofile.wav",O_RDONLY))<0) /*open file "test.wav"*/
{

fprintf ( stderr, " Can't open input file!\n");

exit (-1 );

}
printf("Open input file successfully.\n");
ctl=1; /*allow the first data packet to be sent*/

for(;

{
sleep(1);
i=read(fd,testbuf,1024); /*read data to testbuf ,then send data by UDP*/
printf("i:%d\n",i);
if(i==0)
{
printf("Finish reading file!\n");
close(sd);
exit(1);
}
else if(i>0)
{
trans=sendto(sd,testbuf,strlen(testbuf),0,(struct sockaddr*)&cliAddr,sizeof(cliAddr));
if(trans<0)
{
printf("%s:cannot send data No.%d\n",argv[0],sn);
close(sd);
exit(-1);
}
sn+=1;
printf("%s: sent data No.%d successfully!\n",argv[0],sn);
}
sleep(1);


}


}
}/*end of server infinite loop*/
return 0;
}


客户端
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h> /*close()*/
#include<string.h> /*memset()*/
#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 1024 /*playing buffer*/
#include <sys/soundcard.h>

#include <sys/stat.h>

#include <fcntl.h>
int main(int argc,char *argv[])
{
int sd,rc,i,rmsev_len,p,id,n,fd,resp,tfile,sn;
struct sockaddr_in cliAddr,remoteServAddr;
struct hostent *h;
char msg[MAX_MSG];
/*check command line args*/
if(argc<3)
{
printf("usage:%s<server><data1>...<dataN>\n",argv[0]);
exit(-1);
}

/*get server IP address(no check if input is IP address or DNS name*/
h=gethostbyname(argv[1]);
if(h==NULL)
{
printf("%s:unkown host '%s'\n",argv[0],argv[1]);
exit(-1);
}
printf("%s:sending data to '%s'(IP:%s)\n",argv[0],h->h_name,inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family=h->h_addrtype;
memcpy((char *)&remoteServAddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
remoteServAddr.sin_port=htons(REMOTE_SERVER_PORT);
/*----------------------------------------------------------------*/

/*socket creation*/
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("%s:cannot open socket\n",argv[0]);
exit(-1);
}

/*bind any port*/
cliAddr.sin_family=AF_INET;
cliAddr.sin_addr.s_addr=htonl(INADDR_ANY);
cliAddr.sin_port=htons(0);
rc=bind(sd,(struct sockaddr *)&cliAddr,sizeof(cliAddr));
if(rc<0)
{
printf("%s:cannot bind port\n",argv[0]);
exit(-1);
}



/*-------------------------------------------------------*/
if((tfile=open("/home/transfile/out.wav",O_WRONLY|O_CREAT,S_IXUSR))<0)
{

fprintf(stderr,"Can't open output file!\n");

exit (-1 );

}




/*----------------------------------------------------------------*/
/*init buffer*/
memset(msg,0x0,MAX_MSG);
/*receive data packets*/
rmsev_len=sizeof(remoteServAddr);

/*------------------------------------------------------*/

/*send data*/
for(i=2;i<argc;i++)
{
rc=sendto(sd,argv,strlen(argv)+1,0,(struct sockaddr *)&remoteServAddr,sizeof(remoteServAddr));
if(rc<0)
{
printf("%s:cannot send data %d\n",argv[0],i-1);
close(sd);
exit(-1);
}

}

/*----------------------------------------------------------------*/


/*begin receiving audio data packets from server*/
if(strcmp(argv[2],"start")==0)
{
sn=1;
printf("Command has been sent successfully!\nNow,waitting for data!\n");

/*sleep(1);*/

for (; /*receive and play the audio data*/
{


n=recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr*)&remoteServAddr,&rmsev_len);
/*------------------------*/
/*n=read(fd,msg,1024); test to see whether soundcard works properly*/
/*-----------------------*/
printf("n=%d\n",n); /*print the amount of data received .*/
if(n<0)
{
printf("%s:cannot receive data\n",argv[0]);
close(id);
exit(-1);
close(sd);
}
else if(n>0)
{ n-=1;
write(tfile,msg,n);
/*printf("Playing data!%s\n",msg);*/


printf("%s:received No.%d pack.ACK sent\n",argv[0],sn);/*display response to server*/ sn++;
}
else
{

continue;

printf("Finish playing file!\n");
close(id);
close(sd);
break;
}
}
close(id);
close(sd);
}
/*----------------------------------------------------------------*/

printf("Well done!\n");

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-5 21:56 , Processed in 0.065768 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表