• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Transfer multiple files from Server to Client using Sockets

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

A project that I'm working on needs to pass 3 text files from the Server to the Client.
This what I'm doing
Server side :Wait for client, once connected to client pick up the files and transmit it to the client one after another.
Client side :Connect to server and accept the files one after an other.

I'm only able to receive the first txt file after which I'm getting an ArrayIndexOutOfBoundsException.
In debug mode I noticed that no data is being read on the second read segment on the client side.

Here is a segment of the server side code:



This is the client side code :



Any help on how to synchronize the file sending so the client reads all three files is much appreciated
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see in your code how the receiver is supposed to tell where one file ends and the next one starts. It has this mysterious "filesize" variable which I don't see where that comes from or what it's set to. Do you only ever plan to send files of that size and no other size?
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr.Clapman,

That is the problem I'm having, I don't know how to inform the receiver that the first file has ended and the second has started.
How can put such a system of notification into place?
Also I don't plan to fix the size of the files as all three files are of different sizes
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your protocol needs to be redesigned so it knows when a new portion is read. An example of what could be sent:
- an int indicating the name's length when converted to byte[] using a fixed charset (e.g. UTF-8)
- the file name as a byte[] using that charset
- an int indicating the file size
- the file in bytes.

In short, the sending code:
The receiving code:
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr.Prime,
I changed it and it worked perfectly
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I get the working code please.
 
Rancher
Posts: 215
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prajanya Shrestha wrote:Can I get the working code please.


Aside from this forum doesn't work in this way I highly recommend against what was used back then. If you need to transfer data use proper protocols like http or ftp.
 
Saloon Keeper
Posts: 7327
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prajanya Shrestha wrote:Can I get the working code please.


That discussion happened 11 years ago; the participants are likely to have moved on long ago. But the code Rob Spoor posted should work; are you looking for something else?

Matthew Bendford wrote:If you need to transfer data use proper protocols like http or ftp.


There's a place in this world for socket communications where trying to fit it into HTTP doesn't necessarily make sense. And FTP should be taken out and shot :-)
 
Prajanya Shrestha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to send 3 different text files from server to client using socket. I tried the code but not working.
//Server Code


 
Matthew Bendford
Rancher
Posts: 215
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:

Matthew Bendford wrote:If you need to transfer data use proper protocols like http or ftp.


There's a place in this world for socket communications where trying to fit it into HTTP doesn't necessarily make sense. And FTP should be taken out and shot :-)


Well, implementing a whole http stack may be too much for some situations, I agree on that, and ftp has it's flaws (although it still can be set up in a rather secure way) but they are decades old protocols and both server and client implementation are available in many languages. Reinventing the wheel instead of using existing ones also requires some justification.

@OP
As I mentioned: Don't use that code this way - it has quite some flaws and many resource leaks. Also: Doing the same three times? Use a method.
 
Prajanya Shrestha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just need to send 3 .txt files in same network using java.  I am able to send one file. But How to send 3 different files? I'm not able to understand code shown in this forum & is not working. Can some expert help me.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic