Udp server cannot transfer data

I wrote a simple udp server that needs to transfer some data to several of its clients. but although the server successfully sends, it cannot transmit at least one byte. The return value of send is 0, although I have enough data to transmit. Here you can see the code for the specified server: http://pastebin.com/zeMcwd6X

Can you help people find a possible culprit for this? Any answer in this regard would be appreciated. Many thanks in advance! Mawia

Edit: Guys, as Mr. Yasser rightly pointed out, this typo has been fixed. But the fact is that the problem persists even after fixing this.

+3
source share
2 answers

Your problem is here:

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

Pay attention to the purpose - you are asking the socket to send an empty datagram. n=0;

Some other notes on your code:

  • Avoid hard-coding things like port numbers - sooner or later you will have to change them.
  • minimize global variables, use function arguments, use structures and pointers
  • consider multicast to send the same message to many nodes
  • consider connected UDP sockets for UDP messaging with multiple messages with the same node

Hope this helps.

Edit:

- , - . . , mbone.

epoll(2)/kqueue(2) . : http://www.kegel.com/c10k.html http://pl.atyp.us/content/tech/servers.html

+2

sendto. , , :

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

n 0 - int n=0;. , 0 , n .

, strlen(data) n sendto , sendto, .

. S. , , , , , man 2 sendto.

+3

Source: https://habr.com/ru/post/1745438/


All Articles