Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

socket for file transfer

Posted on 2001-07-09
10
Medium Priority
?
263 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 120 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

636 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