UDP network stuck while waiting / receiving system data
I need to create a two way UDP networking system, that is, send and receive server and client data as shown in the diagram below:
I took a ready-to-use example from http://www.binarytides.com/udp-socket-programming-in-winsock/
However, on the client, when the information (string) is sent, the client gets stuck waiting for incoming data on this line:
recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)
The client cannot send more data until it receives the incoming data.
Is there some method by which I can send data to the server and also wait for incoming data?
source to share
This is because sockets are blocked by default, which means that family calls recv
and read
will hang until data is available. You need to either use non-blocking multiplexed I / O like select()
or poll()
, or use a separate dedicated stream to receive data.
Non-blocking I / O differs significantly in design from blocking I / O code, so there is no simple change you can make. I recommend that you read something like the Beej Guide to Network Programming which covers all of these issues.
source to share