Handling partial return from recv() TCP in C

前端 未结 3 1253
情话喂你
情话喂你 2020-11-29 05:09

I\'ve been reading through Beej\'s Guide to Network Programming to get a handle on TCP connections. In one of the samples the client code for a simple TCP stream cl

相关标签:
3条回答
  • 2020-11-29 05:22

    The first thing you need to learn when doing TCP/IP programming: 1 write/send call might take several recv calls to receive, and several write/send calls might need just 1 recv call to receive. And anything in-between.

    You'll need to loop until you have all data. The return value of recv() tells you how much data you received. If you simply want to receive all data on the TCP connection, you can loop until recv() returns 0 - provided that the other end closes the TCP connection when it is done sending.

    If you're sending records/lines/packets/commands or something similar, you need to make your own protocol over TCP, which might be as simple as "commands are delimited with \n".

    The simple way to read/parse such a command would be to read 1 byte at a time, building up a buffer with the received bytes and check for a \n byte every time. Reading 1 byte is extremely inefficient, so you should read larger chunks at a time.

    Since TCP is stream oriented and does not provide record/message boundaries it becomes a bit more tricky - you'd have to recv a piece of bytes, check in the received buffer for a \n byte, if it's there - append the bytes to previously received bytes and output that message. Then check the remainder of the buffer after the \n - which might contain another whole message or just the start of another message.

    0 讨论(0)
  • 2020-11-29 05:34

    Yes, you have to loop over recv() until you receive '\0' or an error happen (negative value from recv) or 0 from recv(). For the first option: only if this zero is part of your protocol (the server sends it). However from your code it seems that the zero is just to be able to use the buffer content as a C-string (on the client side).

    The check for a return value of 0 from recv: this means that the connection was closed (it could be part of your protocol that this happens.)

    0 讨论(0)
  • 2020-11-29 05:38

    Yes, you will need multiple recv() calls, until you have all data.

    To know when that is, using the return status from recv() is no good - it only tells you how many bytes you have received, not how many bytes are available, as some may still be in transit.

    It is better if the data you receive somehow encodes the length of the total data. Read as many data until you know what the length is, then read until you have received length data. To do that, various approaches are possible; the common one is to make a buffer large enough to hold all data once you know what the length is.

    Another approach is to use fixed-size buffers, and always try to receive min(missing, bufsize), decreasing missing after each recv().

    0 讨论(0)
提交回复
热议问题