开发者

udp server unable to transmit data

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.


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.

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜