Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Client/Server File Transfer Over Sockets (Java)  RSS feed

 
Nick Mario
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build an application that can transfer files between a client and a server. I tried writing some code that uses ObjectInputStreams on the client side and ObjectInputStreams on the the server side, but it's not working at all.

Client:



Server:


Obviously this isn't all my code, but it's the pertinent parts. I figured it would be as easy as writing the file to the ObjectOutputStream.

I've done some research and I found this code:

Server:



Client:



The writer doesn't do a very good job of explaining the code hence my question. Could someone please help me understand? I specifically don't understand the use of BufferedOutputStream and the byte[]. Thanks so much.
 
Tim Moores
Saloon Keeper
Posts: 3953
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I advise not to use object input/output streams between JVMs - their behavior can depend on the precise version of the JVM being used - which can be different on different machines. Also, they're used for transferring Java objects, not so much raw data. And the actual problem with your code is that you're serializing the File object, not its contents - which will not work at all.

The code you found looks about OK. For transferring data over sockets, you would be working with bytes and byte[], and you would want the buffering provided by BufferedOutputStream. What, specifixally, are you having problems understanding?
 
Marius Zilenas
Greenhorn
Posts: 5
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the server side you listen for the connection on the socket. And when you get a connection you accept it and read from the stream of this connection.

On the client side you stream to some specific IP together with port through a socket.

Make sure ports on which the server listens and to which client streams are the same.
 
Nick Mario
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:I advise not to use object input/output streams between JVMs - their behavior can depend on the precise version of the JVM being used - which can be different on different machines. Also, they're used for transferring Java objects, not so much raw data. And the actual problem with your code is that you're serializing the File object, not its contents - which will not work at all.

The code you found looks about OK. For transferring data over sockets, you would be working with bytes and byte[], and you would want the buffering provided by BufferedOutputStream. What, specifixally, are you having problems understanding?


I was hoping that you could just explain that code.  Why do I need a byte array and BufferedStreams? What is it about files that require this? And also what is a BufferedStream? Sorry for the loaded question.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Mario wrote:
I was hoping that you could just explain that code.  Why do I need a byte array and BufferedStreams? What is it about files that require this? And also what is a BufferedStream?


The Oracle tutorial on I/O, and also, on Sockets, may be a good place to start here...

https://docs.oracle.com/javase/tutorial/essential/io/
http://docs.oracle.com/javase/tutorial/networking/sockets/index.html

Henry
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cited example is not very nice. You might fare better with the tutorials.



And it is not necessary to build the image of the whole file in the memory when the data are transferred in chunks anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!