This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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

 
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.
 
Saloon Keeper
Posts: 5327
143
  • 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?
 
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.
 
author
Posts: 23820
140
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
 
Rancher
Posts: 1020
6
  • 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.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!