This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
1) Your server is only accepting one connection before closing. To receive multiple files it needs to loop accepting a new connection each time round the loop.
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.
Sorry but I can't give you a "small example" since I don't have one. Start by making sure you close the streams properly and modify the exception handling so it at least prints a stack trace. When this is working you can then put a loop round the accept() and the following file copy code so that after processing one file it go back to process another file. Then change to write and read multiple bytes at a time and finally move to using threads. Google will help with each of these stages.
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.
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.
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.