I am trying to send multiple files through a socket. I am traversing files using walkFileTree. The problem is when I send files only one file is written. I am sending the size of the file and then I am sending the files still I am not able to receive the file in server side. I am using netbeans 7.0.1 and I am using it in ubuntu. Here is a piece of my code.
2) You do not seem to close the streams.
3) Your exception handling is downright dangerous since you ignore anything that goes wrong!
4) You would do better to write and read a number of bytes at a time rather than just one byte at a time.
Note - in the server, once one has accepted a connection it is normally better to create a thread to do the actual work of processing the connection. This way one can have multiple simultaneous connections from clients.
Note - there is no need to use Writers and Readers since this will limit you to text files. Just use byte streams and then you will be able to copy any sort of file.
Partheban Udayakumar wrote:hi all, I have the following code for file transfer between client and server. Now when i write the files to server from client, if there are multiple files(ab,ac,...) in client, contents of all the files are written into the first file(ab) when they are transferred to server side. If I close the streams, socket connection gets disconnected. Please help me.
That's exactly how you coded it. Your server reads the file name, and then reads everything else to be placed into the file -- including the other file names and file content.
To fix it, you need to modify the protocol. Your client needs to send the filename, the number of bytes, and the bytes of the file. On the server side, you need to read the filename, the number of bytes, and then only read the number of bytes for the file. Then of course, loop to get the next file and number of bytes. Without this change, there is no way for the server to determine where one file ends and the next one begins.