0

I am currently working on a file server in C.

When the client requests a file from the server, it writes to a socket. The server then writes back the data with a header on it. The client reads the header and then reads the actual data. When the client is being debugged, the server terminates the connection before the client has a chance to read the data.

To address this problem, I put in code to write a byte of 0 to the server when the client is done. The server, has a final read of the socket, looking for that byte but when the client is running under the debugger, it does not wait for the read on the server.

The socket is created with the following call on the server:
int socketId = socket(AF_INET, SOCK_STREAM, 0);

What should I do?

Rekovni
  • 6,319
  • 3
  • 39
  • 62
Bob
  • 290
  • 3
  • 12
  • Have you tried adding a read timeout on your server socket? That way it will wait for some time for the client to send the 0 byte. – Kelly S. French Apr 11 '17 at 14:50
  • @KellyS.French And if you don't set a read timeout it will wait forever. The point of your suggestion is unclear. – user207421 Apr 11 '17 at 21:42
  • EJP, It is my understanding that if you do not set a read timeout, it will wait forever. However, it does not. – Bob Apr 12 '17 at 00:45
  • It will wait until data arrives. *Ergo,* if it isn't waiting, data has arrived, or end of stream, or an error. No other possibility. I suggest you're ignoring end of stream and/or errors in your code. Post it. – user207421 Apr 12 '17 at 01:36
  • EJP or anybody else, I am using this socket for two way communication. That is, both the server and the client read and write on the socket. If the server is doing a write and then doing a read, I am assuming that it will not read the data it just wrote. I want it to read the data sent from the client. Do I have that right? Thanks, Bob – Bob Apr 12 '17 at 01:40
  • You will read data sent by the other side, and the other side will read data sent by you. Ths is rather trivial. You cannot rationally expect to get further help until you post some code. – user207421 Apr 12 '17 at 09:54

3 Answers3

1

There are many challenges with writing client-server code. In this case you are also writing a protocol but may not realize it. Your protocol needs to be defined in a way that makes it clear what is expected from each side of the communication and the scenarios are non-trivial.

Here are some related questions:
(java) basic java socket programming problem
(c) Socket Programming Problem
(c) Socket Programming -- recv() is not receiving data correctly

Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
0

What if the file contains a byte of 0?

You don't need this. Just close the socket. If the peer receives a clean close, it must have already received the entire file.

It sounds like you have no error checking in your unposted code.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

We found the problem yesterday. The client was writing more bytes than the server was reading due to the fact that a variable was declared of the wrong type. Thanks for the responses.

Bob

Bob
  • 290
  • 3
  • 12