• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read, Send, Write a File through a socket

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I am trying to send a file "Props.txt" from a client to a server using a Socket.
I am currently receiving and error from the server side stating that there is a "Connection Reset".

I am wondering what I am doing wrong.

Client:


Server:


thank you in advance for the help.
 
Marshal
Posts: 28193
95
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
There are numerous things wrong with that.

First of all the protocol is a mess. Don't mix text with bytes, for one thing. Use a DataOutputStream to send the size of the file as a long value (not an int, that isn't big enough) and a DataInputStream to receive it. Avoid the use of Writers and Readers unless your protocol is entirely text.

And you might be losing data by opening more than one buffered input over the same input stream: the first one may buffer data which you then ignore when you start using the second one. You don't want to ignore data like that.

And don't use a PrintWriter: it doesn't tell you about errors (read its documentation to find out about that). In network communications you do want to be told about errors when they occur.

And your code for reading the data from the socket to the file is wrong. You read chunks of data from the socket until the length of a chunk is equal to the total number of bytes you expect to get. This might just happen to work for a small file, if you get it all at once, but for a larger file it's just going to hang when you get to the end of the stream. Change it to count the received bytes properly.
 
Jae Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your input.
i wish my professor went over this before hand.
after reading up on the topics, i seem to have a better grasp of things.
thanks for you help. i really appreciate it.

J
 
reply
    Bookmark Topic Watch Topic
  • New Topic