How do I implement file transfer using Java sockets?

I am working on an implementation of a chat with Java sockets. I focused on a few features such as authentication, single person chat and group chat. I was thinking about adding file transfer functions and I am wondering what is good practice about this. Should I have a separate socket on the server with different listening ports just for file transfers? Right now, the input and output streams I receive from the server socket are bound to the Scanner and PrintWriter objects respectively, so I find it difficult to use them to transfer files.

Any samples you guys could recommend or give me good advice is greatly appreciated.

Thanks,

ZeKoU

+2


a source to share


2 answers


Well, FTP creates a new socket for every file transfer. Please note that the connection can be established on the same port as in chat, but with a different initialization dialog.



+3


a source


If you can use your own lib then it will be faster. Otherwise, if there is a really good reason, then use a library where all the kinks will be generated.

If you really need to send the file in pure Java. Here one way is based on my old project. Note, however, that there is some serialization overhead, however a lot of this could be eliminated with NIO equivalents as discussed here , or you can go a little further and use Zero Copy , which lets you tell the OS to copy the file directly into the socket without any intermediate copies. This only works with files, not other data.

You must do this in a separate thread so that the chat still works while the file is being transferred. Create a socket and give it some standard port (you can give it port 0, which just picks the next available port, but then you will need to send this information to another recipient to make it easier to use the standard port). Then split the file into blocks and transfer it using a socket:



//Send file (Server)
//Put this in a thread
Socket socket = new Socket(destinationIP, destinationPort);
ObjectOutputStream sender = new ObjectOutputStream(socket.getOutputStream());
sender.writeObject(dataToSend);

//Receive File (Client)
//Kick off a new thread to receive the file to preserve the liveness of the program
ServerSocket serverSocket = new ServerSocket(ListenPort);
socket = new Socket();
while (true) {
        new Thread(new TCPReceiver(socket)).start();
}

//Receive file thread TCPReceiver(Socket socket)
//Get the stream where the object is to be sent
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

/*  Where type is the type of data you send... String or anything really...
    read part of the file into something and send it then at this end use the same data                                                       
    type to recieve it and it will magically pull the entire object across.
*/
while(fileIsIncomplete){
    type recievedData = (type) objectInputStream.readObject();
    //Reconstruct file
}

      

Hope this is enough to get you up and running quickly :)

Edit: Removed the nonsense statement. Added reference to native point and null.

-1


a source







All Articles