Solved

socket for file transfer

Posted on 2001-07-09
10
253 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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
netstat -ano | find "8000" and taskkill /f /pid 2984 3 83
login jsp example 24 65
maven module vs maven project 3 46
Html Table Looping (part 2) 5 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

839 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