Search code examples
csocketsserverclientwait

Does reading from a socket wait or get EOF?


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?

Solution

    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.