News for hackers. Stuff that matters.
Re:What developers should know about TCP by jfkebwjsbx on Friday May 15, 2020 @06:53PM (#23198463)
It seems to me GP is talking about using TCP, not implementing it.
Re:What developers should know about TCP by SilasX on Friday May 15, 2020 @07:52PM (#23198924)
Right, I mean I thought that the TCP protocol implementation itself handled that issue, and your calls to such a library abstracted away from that.
Re:What developers should know about TCP by jacobolus on Friday May 15, 2020 @09:24PM (#23199567)
No, it does not, by design. When you use any standard TCP implementation the abstraction provided at both ends is just a stream of bytes. The guarantee TCP makes is that the bytes received at one end will be in the same order as the bytes that were sent at the other end.

If you want to use TCP to implement some (higher-level) message-based protocol, you need to parse those out for yourself.

Re:What developers should know about TCP by SilasX on Friday May 15, 2020 @09:58PM (#23199788)
The original question didn't refer to such issues of parsing, just checking for whether the full message was received, which, I would think, would be handled by the TCP library read() (or whatever) call. It sounded like the OP (outworlder) was delving into lower-level TCP details that should have been abstracted away -- my thinking is that the TCP caller shouldn't have to concern itself with details about checking whether the full message has been received, at least not as a separate step. That is, it just wouldn't return anything until the full message is received, or would include some data structure that indicates it's not complete. Does that make sense?

Edit: On second thought, I guess OP meant that all of the results were coming back "complete" which doesn't obviate the issue of needing to do a check that handles the "not done" case.

Re:What developers should know about TCP by jacobolus on Friday May 15, 2020 @10:36PM (#23199998)
> the TCP caller shouldn't have to concern itself with details about checking whether the full message has been received

The caller absolutely must concern itself with this crucial “detail”. If you do otherwise, your code is broken, full stop.

You can implement a higher-level protocol on top which handles this kind of thing internally and presents a higher-level interface (e.g. not passing any partial data along to its caller until a full message has been received), but if you are just working with TCP directly, what you get is just a stream of bytes. The guarantee you get is that the bytes will be in order and without any gaps.

If you e.g. send UTF-8 encoded text, you must be prepared on the read side to have your stream of bytes cut off arbitrarily in the middle of a character.

Re:What developers should know about TCP by SilasX on Friday May 15, 2020 @11:00PM (#23200100)
Let function A call TCP read() and pass back a struct that includes the bytes it's received and a flag that indicates whether it's read to the end of the message.

Let function B call TCP read() and never return anything until it's received all the bytes of the message.

Both of those seem (IMHO) like functions you could have in a TCP library. Neither seems (IMHO) like a higher level protocol.

Re:What developers should know about TCP by yuribro on Saturday May 16, 2020 @01:38AM (#23200768)
The reason that this function doesn't exist is that even if the sender sent a "message" in a single API call, there is no guarantee that the networking code will send it in one IP packet (or one layer 2 frame). We don't want to couple the message size with the lower network protocol, the type of network equipment and so on, and also want to allow merging of messages for more efficient use of the network (If we have a large window size it allows better throughput).

So the job of separating the stream into messages is left to the application layer. (unlike for example in UDP, but then you have to worry about dropped messages)

Re:What developers should know about TCP by SilasX on Saturday May 16, 2020 @11:39AM (#23204287)
What is that responding to? Of course TCP can split a message across multiple packets of the lower level protocol; that has no bearing on the concepts TCP works in and whether it can indicate end-of-message.

If your point is just that TCP doesn't have a concept of a "message" (a bytestream with a clear beginning and ending), then that's fair, but, as I said elsewhere [1] the original comment took for granted that TCP does have a well defined notion of "you've reached the end of the message", or at least, "there is no further data to receive". No one seemed to have a problem with that there, and I was just working off that assumption.

As before, I haven't checked whether this is true (I can't quickly verify from descriptions of TCP).

And, interestingly enough, there's this comment [2], which says that what I described does exist, but isn't the default. So ... I'm at least not getting a consistent answer to my question, and people who think they know what they're talking about are inconsistent with each other.

[1] https://news.ycombinator.com/item?id=23200293

[2] https://news.ycombinator.com/item?id=23200906

Re:What developers should know about TCP by jacobolus on Saturday May 16, 2020 @04:52PM (#23207125)
> the original comment took for granted that TCP does have a well defined notion of "you've reached the end of the message"

No, the original comment was complaining about a coworker who didn’t understand (and refused to listen when told otherwise) that there is no such notion in TCP. It was a response to another comment complaining about people on the internet (e.g. Stack Overflow) too often making the same mistake.

You’re more or less playing the part of that coworker here. It’s unclear why.

Re:What developers should know about TCP by yuribro on Sunday May 17, 2020 @02:36AM (#23210056)
> Both of those seem (IMHO) like functions you could have in a TCP library. Neither seems (IMHO) like a higher level protocol.

It's responding to the last part. It is a higher level of protocol. In the traditional TCP/IP model, it's in the application level. There are many libraries with an API like you asked, they are just in a higher level.

(and TCP_WAITALL is a partial solution, applicable only if you know in advance the exact size of the message you are about to receive)

Re:What developers should know about TCP by Matthias247 on Friday May 15, 2020 @11:53PM (#23200351)
There is no "message" in TCP. So no - you can not have this in a library.
Re:What developers should know about TCP by scott_s on Monday May 18, 2020 @09:09AM (#23221824)
If it makes you feel better, calling read() on a TCP socket is basically the same as calling read() on a file in a file system. In both cases, you can always end up reading less than you expected, and you always must check how much you actually read. In practice, this means that calls to read() for both should always be in a loop.
Re:What developers should know about TCP by jfkebwjsbx on Saturday May 16, 2020 @10:38AM (#23203715)
Those are perfect examples of a higher level protocol, since there is no way you can do it only with TCP.
Re:What developers should know about TCP by jayshua on Friday May 15, 2020 @11:32PM (#23200270)
That might be nice. But that's not how it works.
Re:What developers should know about TCP by Nursie on Friday May 15, 2020 @10:16PM (#23199893)
> which, I would think, would be handled by the TCP library read()

You would be wrong.

Re:What developers should know about TCP by SilasX on Friday May 15, 2020 @10:37PM (#23199999)
Thanks for the explanation, but I don't see why a library utility function wouldn't do that.
Re:What developers should know about TCP by spydum on Friday May 15, 2020 @11:13PM (#23200165)
But if the goal is to process or retrieve a string of bytes, how do you know when you've got them all? That is the root of the problem: tcp isn't built to exchange messages, it's just the stream transport layer. If you want messages you have to encode and decide that yourself.
Re:What developers should know about TCP by SilasX on Friday May 15, 2020 @11:36PM (#23200293)
Sorry, the rest of the conversation seemed to be assuming that "whether you have received the full message" is well-defined at the level of TCP, as suggested by the original comment where I joined[1]:

>I had to fight a coworker on this. I had quickly created some client code just to validate that the server was working. Due to some quirk, all the messages were arriving in full in every read call. He told me to ship it.

>I said no! "I need to check if there's more data and if so add a loop to read again" "But it is working, release it". That went on for a while, to no avail. Wouldn't look at documentation either.

I was just going along with that.

I didn't check up on TCP further to verify whether this was actually true; if you are saying that's not a well-defined concept, you might want to reply to that comment to say so.

[1] https://news.ycombinator.com/item?id=23195230

Re:What developers should know about TCP by jfkebwjsbx on Saturday May 16, 2020 @10:34AM (#23203684)
Those quotes don't claim messages are received in full, you are misinterpreting them.
Re:What developers should know about TCP by SilasX on Saturday May 16, 2020 @11:36AM (#23204257)
Of course, but they do indicate that "there is still more of the message to receive" is a well-defined concept (or at least "there is more data to receive" is), which is all my point requires.

(Edit: also, it would help if you said what the correct interpretation would be, since it has the phrase "messages were arriving in full in every read call".)

Re:What developers should know about TCP by Nursie on Saturday May 16, 2020 @01:53PM (#23205613)
>but they do indicate that "there is still more of the message to receive"

Not at the TCP level they don't.

They just give you some bytes. It's up to you to decide whether you have the full message or not and if you want to try to read more. The TCP read functions just give you the data they have. There is no concept of one write at the sender end translating to a complete message at the other end. It's just a stream of bytes.

Re:What developers should know about TCP by anonymoushn on Saturday May 16, 2020 @11:10PM (#23209232)
That poster's coworker was implementing a message-oriented protocol and testing the client and server on the same machine. When running the software in this configuration, the coworker observed that each read returned abd entire message, even though this was not going to be true in other configurations where the client and server are different machines or the messages are larger.
Re:What developers should know about TCP by anonymoushn on Friday May 15, 2020 @11:08PM (#23200139)
Most TCP libraries simply do not come with a function that does "read, but only give me 0 bytes or n bytes, and if you get some other number please hang on to the leftovers until next time". I guess I could follow your advice, but the first step of doing that would be to write such a function again.
Re:What developers should know about TCP by userbinator on Saturday May 16, 2020 @02:18AM (#23200906)
They do... with an option called MSG_WAITALL.

That naturally leads to the question of why it's not the default, which can be answered by understanding the history of TCP and computer networking; and more interestingly, how might things have been different if MSG_WAITALL was the default from the beginning.

Re:What developers should know about TCP by anonymoushn on Saturday May 16, 2020 @04:15AM (#23201448)
I don't think this gets you nonblocking all-or-nothing recv.
Re:What developers should know about TCP by bavell on Friday May 15, 2020 @09:28PM (#23199588)
If you're using a library, then sure. But if you're just reading from a raw TCP socket, it's just a stream of bytes. It's up to your application to parse those bytes (e.g. into a http request).

The OS will buffer bytes received from TCP packets for you until you read from the socket again to drain the buffer. Your application needs to determine how to semantically chop those bytes up into the protocol it's expecting (e.g. http request).

My low-level networking chops are a little rusty so please correct my understanding if I'm off-base somewhere.