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

SocketException on a readLine

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this problem is driving me crazy. I have one little program where I open a Socket, send a byte for 'P' and then do a readLine...this works perfectly fine 100% of the time.

I now have a different program where I've put the bit of working code to get the data from a server (from my other program) which is part of a much larger system. I SEEM to do the send ok (no exceptions), but as soon as the readLine() gets called, I get the following:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)

Here's the two methods that get called (Note: getOutputStream() and getInputStream() get the i/o stream of the open socket:



So in one case, it works fine...in the other case, it never works...with practically the exact same code. I have a feeling I'm missing something blatantly obvious, but I don't know what to look for or where to start.
Any ideas?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using readLine() with a socket is a bad idea since it uses the current platform's EOL indicator and different platforms have different EOL indicators. For example, if you're running your program on a Windows computer, the EOL is "\r\n" but a Unix server is going to send "\n" by default.
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally figured it out (I guess a weekend of not staring at it in frustration helps).
It turns out that it was a Threading issue...I had created the socket in one thread and sent it into another one.
 
Water! People swim in water! Even tiny ads swim in water:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic