Does reading from a socket wait or get EOF?
Asked Answered
H

2

11

I'm implementing a simple connection between a client and a server in C. In client side, I'm in a loop, reading from a file; every time BUFFER_SIZE bytes and sending it to the server side (didn't upload error handling).

//client side
bytesNumInput = read(inputFileFD,bufInput,BUFFER_SIZE)
bytesSend = write(sockfd,bufInput,bytesNumInput)

Of course the server is also in a loop.

//server side
bytesRecv = read(sockfd,bufOutput,bytesNumInput)

Now, my questions are:

  1. Can I get EOF in the middle of the connection if the server reads faster than the client?
  2. Does the read function wait to get all the data or is it the same as reading from a file?
  3. Is it possible the server will handle 2 read iteration in 1 write iteration?
Honeycomb answered 12/2, 2017 at 13:29 Comment(5)
Docs are here. It's all in there.Kanzu
"Can I get EOF in the middle ...", by "EOF", are you asking "Can end-of-file to occur in the middle ..." or ""Can read() return EOF in the middle ..."?Siding
@chux Both forms of that question are meaningless. 'EOF in the middle' is already a contradiction in terms. Your comment is pointless.Tensile
@ejp A read(inputFileFD,bufInput,BUFFER_SIZE) is expecting BUFFER_SIZE bytes, the end-of-file condition can occur in the middle of of that. read() can return -1 for a variety of conditions after partially receiving some of the BUFFER_SIZE bytes.Siding
@chux For what read() returns, see my answer. The question is about sockets, not files, and it is about the middle of the connection, not the middle of the read. -1 is not an EOF when returned by read(). Your question makes a distinction without a difference.Tensile
P
5
  1. No, server will wait for incoming data. Sockets provide control flow.
  2. Question not clear to me, read always try to get all requested data, but if there is no so much then it will get what is available
  3. Yes, socket does not have semantic of messages, just a flow of bytes.
Phew answered 12/2, 2017 at 13:45 Comment(1)
(2) is self-contradictory.Tensile
T
10

Can I get EOF in the middle of the connection if the server reads faster than the client?

No. EOF means the peer has disconnected. If the connection is still alive, read() will block until (a) at least one byte is transferred, (b) EOF occurs, or (c) an error occurs.

Does the read function wait to get all the data or is it the same as reading from a file?

See (a) above.

Is it possible the server will handle 2 read in 1 write iteration?

Yes. TCP is a byte-stream protocol, not a messaging protocol.

Tensile answered 12/2, 2017 at 15:16 Comment(1)
It indeed seems that read() is supposed to return 0 on EOF: "On success, the number of bytes read is returned (zero indicates end of file), ..."Bitters
P
5
  1. No, server will wait for incoming data. Sockets provide control flow.
  2. Question not clear to me, read always try to get all requested data, but if there is no so much then it will get what is available
  3. Yes, socket does not have semantic of messages, just a flow of bytes.
Phew answered 12/2, 2017 at 13:45 Comment(1)
(2) is self-contradictory.Tensile

© 2022 - 2024 — McMap. All rights reserved.