Reputation: 354
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?
Upvotes: 0
Views: 3896
Reputation: 354
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
Upvotes: 0
Reputation: 310916
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.
Upvotes: 0
Reputation: 12334
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
Upvotes: 1