0

In the most abstract, platform agnostic way possible, can someone explain what actually determines the end of input/output a socket? Is this something the programming language itself typically handles and indicates by a special return value (for example, -1 bytes read)?

I am exposed with an IO API that will throw EOF when the known end of a stream is reached, however, for network IO, EOF is never thrown (until the Socket is closed), so when I am reading a socket, I never know when to stop reading, as, rather than -1, it will return 0 bytes read. Is 0 bytes read where I should stop reading? Or should I keep trying to read bytes, and consider it EOF/input done being read when 0 is returned so many times (polling basically)?

That solution (polling) seems quite inelegant.. but I can't think of any other way to know when to stop attempting to read input from a socket.

  • 1
    That needs to be handled at the higher levels of a protocol definition. An EOF indicator isn't helpful for doing so. The protocol defined above the transport level should always know when a response is complete or not. – πάντα ῥεῖ Feb 17 at 19:10
  • @πάνταῥεῖ Thank you! That was going to be my next question, is this something a protocol handles (delimiting messages). So you're saying that, the same way HTTP Messages are delimited with CRLF CRLF, I need to be using a protocol to figure out the bounds of a message? – user1538301 Feb 17 at 19:11
  • @πάνταῥεῖ you may refrain from doing so for rep purposes (perhaps this isn't a great question due to its simplicity) but if you post this as an answer I'd be happy to select it as the best answer, as I am pretty certain this does answer my question. – user1538301 Feb 17 at 19:13
  • I refrained posting that as an answer, since your question lacks of some more detail and context. It was more like a shot in the dark. – πάντα ῥεῖ Feb 17 at 19:15
  • @πάνταῥεῖ I guess the correct question I should have been asking is whether or not the bounds of network messages are determined at the protocol level or transport level. – user1538301 Feb 17 at 19:17
1

In the most abstract, platform agnostic way possible, can someone explain what actually determines the end of input/output a socket? Is this something the programming language itself typically handles and indicates by a special return value (for example, -1 bytes read)?

Well, regarding input the number of bytes should have been sent with the request beforehand, thus that's quite clear.

When receiving the response, there's either the same mechanism in place, or you just decide for completion at the protocol level.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.