tags:

views:

55

answers:

2

Hi! all, I have written a simple udp server which has to transmit certain data to few of it's clients. but though server is successfully executing send,but unable to transmit even a single byte.The return value of send is 0 although I have enough data to be transmitted.you can see the code for the said server here: http://pastebin.com/zeMcwd6X

Can you people help in finding the possible culprit for the same.Any reply in this regard will be appreciated. Lot of Thanks in advance! Mawia

Edit:guys as it is rightly pointed by Mr Yasir,that typo has been corrected.but the thing is that problem persist even after correcting that.

+3  A: 

You've used sendto incorrectly. You should specify the number of bytes you're about to send in the third argument, but you have:

n=sendto(sockfd,data,n,0,(struct sockaddr *)&setOfClient[k],(char*)(setOfClient+1)-(char*)setOfClient);

where n on first iteration is assigned 0 because of an earlier int n=0;. Thus, the return value is 0 and never changes because n is always used in the loop.

So using strlen(data) instead of n as an argument when calling sendto, and using another variable which is assigned to value sendto returns would make more sense.

Also your accept rate is too low, consider accepting answers to your earlier questions before you expect to receive any nice answers.

P. S. It's useful to read man pages, they tend to provide you with most of details you'd want to know, so man 2 sendto.

Yasir Arsanukaev
yeah a lot of thanks for that pointing that mistake,but the thing is that the problem persist even after correcting that silly mistake
mawia
Try `sendto(sockfd,data,strlen(data),0,(struct sockaddr *)`. I don't know what made you use that ugly thing `(char*)(setOfClient+1)-(char*)setOfClient`, you should've specified the size of the *structure* you passed to the fucntion instead. [Example](http://www.abc.se/~m6695/udp.html)
Yasir Arsanukaev
+1  A: 

Your problem is here:

int n = 0;
for( k = 0; k < numberOfConnections; k++ )
    n = sendto( sockfd, data, n, 0, ... );

Notice n=0; assignment - you are asking the socket to send an empty datagram.

Some other notes on your code:

  • avoid hardcoding things like port numbers - sooner or later you will have to change them
  • minimize global variables, use function arguments instead, use structures and pointers
  • consider multicast for sending identical messages to many nodes
  • consider connected UDP sockets for multi-message UDP exchange with the same node

Hope this helps.

Edit:

Multicast over the internet is challenging - you'd need either explicit routers support or some sort of tunneling. That is to say that multicast is best on local network. If you're interested, take a look at mbone project.

Take a look at epoll(2)/kqueue(2) facilities for high-performance networking. Also read these pages: http://www.kegel.com/c10k.html and http://pl.atyp.us/content/tech/servers.html

Nikolai N Fetissov
Thanks for the reply and your kind advice.I was trying to make live video streamer.My whole project was dependent upon choosing best way so as to minimize the bandwidth consumption of the server and the internet backbone.But given the multicast it seem all the solution is already there.Just send it to a multicast address and concrnd ppl will recv it.So no headache at all! What is your say??
mawia
@mawia, I added some more info to the answer.
Nikolai N Fetissov