Solved

socket for file transfer

Posted on 2001-07-09
10
249 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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
 
LVL 7

Expert Comment

by:Igor Bazarny
Comment Utility
> 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
Comment Utility
viswa,

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

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:viswa081400
Comment Utility
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
Comment Utility
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
Comment Utility
bazarny,

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

Author Comment

by:txholdem
Comment Utility
bazarny,

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

Expert Comment

by:Igor Bazarny
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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…

771 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