File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes File transer through ServerSocket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "File transer through ServerSocket" Watch "File transer through ServerSocket" New topic

File transer through ServerSocket

Ratheesh Narayanan
Ranch Hand

Joined: Oct 30, 2004
Posts: 38
Hi All,

I have an application which is having client and server components as console applications. The client has to send a file to the server then the server recieves the file and store it. Now the server has to take another file and send it back to the client.

Can anyone help to find out how can I send send a file back to the client through the ServerSocket I have created at the server.

Thanks in advance..


SCJP 1.4 & SCBCD 1.5
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Have you run through the Sun tutorial on sockets? The Knock Knock Server is a neat example of a client and server holding a conversation with messages going both ways. Your program will send files back and forth instead of simple messages, but the basic idea is the same.

One thing you'll have to make up is some kind of protocol. For example the client might send a header that says "I'm about to send a file. The name is The size is nnnnnn", then send the file. The server could read the header, and save the next nnnnnn bytes to file It might send a reply "Received file, status OK".

Then the server could repeat the process the other direction and the client could save bytes to a file. Hey, that sounds like a lot of the same code on both ends. Be sure to pull the common code out to common classes rather than copy/paste it to both programs.

Do the Knock Knock tutorial, then see if all this makes sense.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: File transer through ServerSocket
It's not a secret anymore!