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

File transfer through Socket

Guy Geelen

Joined: Apr 18, 2007
Posts: 5

I'm implementing a client-server application in Java. I want to store all my shared files on a server and the application itself (which makes use of thes files) on different workstations.

How can I send these files from the server to the workstation through LAN and vice versa?

Can anyone give me some code or explain what steps I have to take?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm curious about what your server is. What protocols does your client use to talk to the server otherwise?

If you're writing the server, you can do file transfers via sockets for sure. Make up a little protocol where the client sends a request like "GET name" and the server maybe returns "SIZE 9999\n raw bits ".

If the server can do HTTP, or if you could install a separate HTTP server, you could use URL and HTTPUrlConnection in the client.

And if the server can do FTP you could Google for a good Java/FTP package.

Any of that sound useful?

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
Guy Geelen

Joined: Apr 18, 2007
Posts: 5
No that's not what I'm looking for.
My server is a Linux (Slackware) server where in the first place a db4o database is running on port 4488. In the application I use some files, like RTF and xml files. When I store those files on each client PC, all changes are only local. That's why I want to store them on the server...
So I just need to know how I transfer those file on the server to the local PC's (clients)

I'm thinking of the following: create a socket on the server with eg. port 3400 and then the client makes connection on that port for transferring the file. But I'm not quite sure if this is going to worl, or where exactly I need to start...

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
So far, it still sounds to me like Stan's answer gives you a couple good options. I'm not seeing why that's not what you need. Your idea about opening a socket on the server is what Stan talks about in his second paragraph. Is there more than one file that a client might need? Then you need some way for the client to communicate what file it's looking for. If the client says "GET <filename>" to the server socket, followed by a return, that's one simple way to do it. If you need wider functionality like the ability to browse directories or send files as well as receive them, then it may well be easier to just use an existing Java/FTP package.

If this is your first experience with sockets in Java, a good starting point may be the Java Tutorial.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: File transfer through Socket
jQuery in Action, 3rd edition