Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help to write Client/Server java socket program to transfer files between client/server  RSS feed

 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to write a client/server socket java program to simulate FTP to transfer file between client and server.
Basically, the program performs 3 commands: put: upload files to server, get: downloads files from server,change:change files name resides in server.
the program is running on two local machine(client and sever) by command line input.
Appreciate any help or resource,

Thanks so much!
peter
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to define a protocol for this: the client sends a request, and the server sends a response.

If you don't mind limiting the protocol to Java you could send objects, and wrap the Socket's streams into ObjectInputStream and ObjectOutputStream. You then send a Request object, and wait until the server sends a Response object. You can use one interface for Request with multiple implementations (one for each request type), and one interface for Response with multiple implementations (one for each response type).

If you want to make it more general you will need to go through a bit more trouble. A very simple example for download:
- "download <file>" is sent to the server
- the server sends back the number of bytes to send, followed by the file's bytes. The number is used to determine how many bytes to read, since the stream will not be closed afterwards; you will reuse it for other requests.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!