0
votes

I have a client serever application, My server accepts connections from more than one clients.

After a client is connected to server it sends command to the server and the sever sends replies the replies are either strings or files.

On the server side after accepting connection, there is a socket (seperate from listening socket) which is responsible for communication with client.

On the client side after a client sends a command to server, I start reading for the response on the same socket.

Now my problem is with files,

client sends a command to server asking for file, the server starts responding by sending binary data of file, if file is all good it transfers fine, But if on the server side in the middle of file transfer the server gets a read problem, it has no way to infrom that problem to client, because this is a one to one socket communication... the client will treat any incoming data as if it is a file data untill the file size sent in the start is not complete.,

I am sure this could be a recurring pattern how to can I resolve this ?

2

2 Answers

0
votes

FTP does this by having two connections: a command connection and a data connection.

0
votes

As long as these are TCP/IP sockets, all you need is an agreement between the server and the client that the first eight bytes(for example) sent() and recv()ed, respectively, represents the size of the binary data to follow. TCP/IP will make sure that all the pieces arrive and are in order for you. If you have a variety of files that could be transferred, then you agree that the next four bytes after that represent characters for the file type. So you basically keep recv()ing until you have 12 bytes, which will probably take only one recv(). Then keep using recv() until you have all the bytes you expected to receive.