search

Home  >  Q&A  >  body text

c++ - Socket (TCP) using recv to receive data blocking problem?

Generally speaking: socket (TCP) uses recv to receive data. If there is no data in the kernel cache, it will block.

If the TCP connection is not disconnected, but the other party never sends data, will the recv function call block the program forever?

扔个三星炸死你扔个三星炸死你2713 days ago1282

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-10 09:50:28

    Sockets are divided into two types: blocking and non-blocking. The default blocking mode can be modified to non-blocking through parameter settings;
    The meaning of blocking: It is actually the buffer in the TCP/IP protocol stack.
    (1) In blocking mode: when If there is no data in the buffer, then wait until data is returned;
    (2) In non-blocking mode: If there is no data, a parameter will be returned immediately (for example, an exception is thrown under Python)

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-10 09:50:28

    You can use the setsockopt() function to set the timeout period

    reply
    0
  • Cancelreply