Solved

socket for file transfer

Posted on 2001-07-09
10
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 103
How do I remove an object from a 3 58
jboss 7.1 start up error 1 80
add projects t working set in maven 2 62
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

742 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