This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Read, Send, Write a File through a socket  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • 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.



thank you in advance for the help.
Posts: 24089
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.

All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!