Udp server sending only 0 bytes of data
Posted
by mawia
on Stack Overflow
See other posts from Stack Overflow
or by mawia
Published on 2010-05-30T15:22:51Z
Indexed on
2010/05/30
15:32 UTC
Read the original article
Hit count: 200
Hi all, This is a simple Udp server.I am trying to transmit data to some clients,but unfortunetly it is unable to transmit data.Though send is running quite successfully but it is returning with a return value meaning it has send nothing.On the client they are receiving but again obviously,zero bytes.
void* UdpServerStreamToClients(void *fileToServe)
{
int sockfd,n=0,k;
struct sockaddr_in servaddr,cliaddr;
socklen_t len;
char dataToSend[1000];
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(32000);
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
FILE *fp;
if((fp=fopen((char*)fileToServe,"r"))==NULL)
{
printf("can not open file ");
perror("fopen");
exit(1);
}
int dataRead=1;
while(dataRead)
{
len = sizeof(cliaddr);
if((dataRead=fread(dataToSend,1,500,fp))<0)
{
perror("fread");
exit(1);
}
//sleep(2);
for(list<clientInfo>::iterator it=clients.begin();it!=clients.end();it++)
{
cliaddr.sin_family = AF_INET;
inet_aton(inet_ntoa(it->addr.sin_addr),&cliaddr.sin_addr);
cliaddr.sin_port = htons(it->udp_port);
n=sendto(sockfd,dataToSend,sizeof(dataToSend),0,(struct sockaddr *)&cliaddr,len);
cout<<"number of bytes send by udp: "<< n << endl;
printf("SEND this message %d : %s to %s :%d \n",n,dataToSend,inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
}
}
}
I am checking the value of sizeof(dataTosend) and it is pretty much as expected ie thousand ie the size of buffer. Are you people seeing some possible flaw in it. All of the help in this regard will be appreciated. Thanks!
© Stack Overflow or respective owner