Home > Back-end >  C programming voice networks using the recv function accepts data will meet don't know how long
C programming voice networks using the recv function accepts data will meet don't know how long

Time:11-01

Send how much data if you don't know each other, is likely to encounter the recv recv finished just accept the data, but don't know again, which again recv, but recv is blocked, as long as the other party not to send data, recv is always blocked, wait for like program die,
Please ask you is how to solve the problem?

CodePudding user response:

Which give an answer

CodePudding user response:

In a separate thread to wait, in addition, if it's TCP, it is best to define a data structure, the specified length, continuous, because TCP flows may tail half a pack

CodePudding user response:

Accept separate use multithreading, put a single thread, or use a socket non-blocking mode

CodePudding user response:

More convenient method is to use some similar libuv libev library, such as processing data in the callback function directly, don't need to care about data exactly what time to, or use the select, epoll to manage such as sockets, open thread receives can also alone,

CodePudding user response:

Add a header information, to determine the specific for specific how long messages,

CodePudding user response:

With MSG_PEEK recv last parameters, can see whether there is data

CodePudding user response:

Business data and baotou, record data length in baotou,
After receiving data to parse the data length, does not receive enough long to continue, or exit the loop,
  • Related