Solved

socket for file transfer

Posted on 2001-07-09
10
251 Views
Last Modified: 2010-04-16
How do I use Socket and ServerSocket to transfer a file from a server to a client?

I have 2 programs: Server.java and Client.java, which can communicate by sending/receiving strings. How do I modify the programs so that I can send and receive files? I do not need full-featured ftp server. What are the minimum modifications I need?

Thanks.
0
Comment
Question by:txholdem
  • 4
  • 4
  • 2
10 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6266781
Hi,

Server side:
You create ServerSocket  and specify port number to listen using
new ServerSocket(port).
Then you call ServerSocket.accept() It will block until client connects to that socket, than return Socket which you can use for communication. Call Socket.getInputStream(), read client request, than write file content to stream returned from Socket.getOutputStream();

Client side:
You create Socket, providing server's host name and port number:
new Socket("server.host",port). Server should be already listening (inside accept() call) at this time. Use Socket.getOutputStream() to send command to server and Socket.getInputStream() to receive file content.

If Server is supposed to handle several requests from multiple clients, it's a good idea to call accept() within a loop and create a separate thread for each communication (upon accept() return). On client side, it's a good idea to repeat attempt to establish a connection when previous attempt was unsuccessful, so you can start server later then client.

Hope it helps,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com 
0
 
LVL 1

Author Comment

by:txholdem
ID: 6266925
>write file content to stream returned from Socket.getOutputStream

How do I write file content of an existing file to the stream?
0
 
LVL 1

Accepted Solution

by:
viswa081400 earned 30 total points
ID: 6267110
Hi,
You can refer a previously answered question on file transfers using socket. There is a example provided in it.
Check if that answers your queries.
If not post your queries.
Thanx

http://itworld.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20141869
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6267139
> How do I write file content of an existing file to the stream?

Open it using FileInputStream than copy bytes from file input stream to socket's output stream. You may consider wrapping streams into buffered streams for efficiency. Don't forget flush() data on completion--socket has internal buffer which can cause delay in sending the last portion of the file.
0
 
LVL 1

Author Comment

by:txholdem
ID: 6271182
viswa,

I do not see in the documents that FileInputStream read by 8 bytes while FileReader by 16. How do you know that?

0
 
LVL 1

Expert Comment

by:viswa081400
ID: 6271211
I found that on the sun's site. For details look at the following link on file streams.

http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6271508
In fact, FileReader typically returns 1 16-bit char per 1 8-bit input byte. java.io.Reader and java.io.InputStream documentation explain what you get from read() call. Question which viswa mentions had bytes and chars mixed initially. Just don't use reader--byte streams will work for you.
0
 
LVL 1

Author Comment

by:txholdem
ID: 6281451
bazarny,

I'll post the points for you in a new thread,
thanks.
0
 
LVL 1

Author Comment

by:txholdem
ID: 6281458
bazarny,

I'll post the points for you in a new thread,
thanks.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6281752
Thanks,

What a surprise, I didn't expect points from this question after viswa comment. It's pleasure to help you.

Regards,
Igor Bazarny
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
Oracle SQL syntax check  without executing 6 50
configure dependency in POM for new database 3 25
Desingning Refactoring existing code 2 24
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now