File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes client-server file exchange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "client-server file exchange" Watch "client-server file exchange" New topic
Author

client-server file exchange

Haritha Gorijavolu
Greenhorn

Joined: Feb 18, 2011
Posts: 21

The following are the codes for client and server to exchange a file. It works like this: The client sends a connection request and the server begins to transfer the file and the client just recieves the file. The location of the file and the size of the file are predefined, for the sake of simplicity.

How is it that the client reads from the InputStream only after the Server sends the data and not before it sends data? How does it know when to start reading from InputStream? I am presently running the client and server on local host and the code is working fine.

client code:


Server code:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Haritha Gorijavolu wrote:How is it that the client reads from the InputStream only after the Server sends the data and not before it sends data? How does it know when to start reading from InputStream?

What would it read if the server hasn't sent anything yet? The read method simply blocks until some data is available. Be aware that this may not everything that was sent; some bytes may still be on their way. That's why you usually should read in while loops.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Haritha Gorijavolu
Greenhorn

Joined: Feb 18, 2011
Posts: 21

Got it. Thanks for explaining.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
 
subject: client-server file exchange
 
Similar Threads
Sending a file over socket
Transferring file name then file data over socket
copying files over a network
Socket connection program works for localhost but not for other ip
Transfer multiple files from Server to Client using Sockets