This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes File Transfer using sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "File Transfer using sockets" Watch "File Transfer using sockets" New topic
Author

File Transfer using sockets

Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 200

Hi all,

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.

Code:

Code:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

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.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 200

Thank you Richard. Can you give me a small example on how to do those things? I am totally new to practical java. It would be nice if you can provide me examples
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

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.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 200

Thank you again Richard.

I closed all the streams and moved the connection inside the loop as per your instructions. I get the answer I want. Thanks once again
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 200

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.

Server:


Client:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 200

Thank you Henry,

I got the answer following your instructions

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Transfer using sockets
 
Similar Threads
transfer large file using java code in android
Transfer Multiple File problems via socket
Server and Multiclient application
Transfer File from Android to server via socket
Simple echo. Problem with connection to server.