Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Socket InputStream.read keeps blocking dispite lost connection  (Read 7151 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2008-03-27 21:52:04 »

I have a serious problem here.

Lets say I have this code on the client:
1  
2  
3  
4  
5  
6  
7  
8  
9  
socket.getSoTimeout(4*1000);
InputStream in = socket.getInputStream();

while(true)
{
   int r = in.read();
   if(r==-1) break;
   System.out.print((char)r);
}


The socket receives data every second, so normally the timeout occurs when the connection is lost for whatever reason.

Sometimes, the TCP connection is lost (about once every 3-8 days - this is a very long lasting tcp-session!) and the server correctly notices the session is gone (IOException or a read of -1), however the client thinks it is still connected, and blocked indefinetly at read(), dispite the timeout of 4000ms! Stopping/restarting the ServerSocket at the server doesn't help. The server sees all clients disconnect at the same time (which indicates a router reset/failure...?) and all clients are turned into zombies, blocking at read() forever, until I terminate the process of each client.

Any reason this can occur? Losing a TCP session isn't bad, as long as I can notice this and reconnect... Undecided

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Wildern

Junior Member





« Reply #1 - Posted 2008-03-28 00:38:29 »

Can you afford Thread.sleep?
You may want to do your timeout manually.

I realized that you probably don't want to go 4 seconds without reading anything, so I updated the code snippet.

int timeout=0;
int a;
while(true) {
   a = in.available();
   if (a > 0) {
      int r = in.read();
      if (r == -1) break;
      System.out.print((char)r);
      tineout = 0;
   } else {
      Thread.sleep(0,100);
      a = in.available();
      if (a == 0) timeout++;
      if (timeout > 40) break;  // timeout
   }
}
Offline sunsett

Senior Member




ribbit!


« Reply #2 - Posted 2008-03-28 12:50:31 »

Not ideal, but you could always maintain another thread keeping a reference to this one and interrupt() it in these situations?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-03-28 15:33:30 »

Yeah, I'm doing that right now... Another thread that monitors all TCP sessions and compares the last actual IO with the socket's timeout value... I coded it this morning, so I'll have to see whether it's 'stable' now.

Feels like I'm doing the task the OS is supposed to do...!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sunsett

Senior Member




ribbit!


« Reply #4 - Posted 2008-03-28 20:52:51 »

Well, although you'd think it should throw an Exception, it doesn't seem like a good way to code it either.  I mean, as far as the system is concerned you tell it to read(), so it blocks until it gets more data.  Although it should throw an exception when the connection is lost, it is waiting like you told it to. Wink

Would the following be an easier fix?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
socket.getSoTimeout(4*1000);
InputStream in = socket.getInputStream();

while(true) {
   if (in.available() > 0) {
       int r = in.read();
       if(r==-1) break;
       System.out.print((char)r);
    }
}
Offline DzzD
« Reply #5 - Posted 2008-03-28 21:17:51 »

hum... seems that you forgot a Thread.sleep in your loop , this way cpu will burn Wink

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2008-03-28 21:24:23 »

Well, although you'd think it should throw an Exception, it doesn't seem like a good way to code it either.  I mean, as far as the system is concerned you tell it to read(), so it blocks until it gets more data.  Although it should throw an exception when the connection is lost, it is waiting like you told it to. Wink


Sorry to be a bit blunt, but that's plain wrong.

input.read() is documented to block until either data is available, or the connection is lost - whichever happens first.



It's just so weird that the server instantly knows the connection is lost, and the client just keeps blocking...  Undecided

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sunsett

Senior Member




ribbit!


« Reply #7 - Posted 2008-03-28 21:55:14 »

hum... seems that you forgot a Thread.sleep in your loop , this way cpu will burn Wink

I thought about that as I was typing it up, but knew Riven knows better than to make a greedy thread so I felt it more useful to focus on the concept I was trying to convey.  Tongue

input.read() is documented to block until either data is available, or the connection is lost - whichever happens first.

It's just so weird that the server instantly knows the connection is lost, and the client just keeps blocking...  Undecided

No, I definitely agree. I've experienced this myself and you'd think it would be fixed in Java by now, but perhaps there's a good migration path to NIO. Wink
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2008-03-28 22:45:31 »

Well uh... well Smiley I kinda stuck with java.io + java.net because I expected it to be extremely stable, and NIO has a bit of a rough history in that regard. I need extreme stability for this app at my work, and now I simplified the test-case to such degree that it simply can't be my fault anymore. It's either the JVM or the OS.


And you're right I wouldn't make a CPU-hungry loop..

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Wildern

Junior Member





« Reply #9 - Posted 2008-03-29 00:09:11 »

does setting
socket.setSoLinger(false,0);
have any effect?  I have a client that has similar issues in that it doesn't detect certain abnormal terminations, but it always respects the soTimeout value (at least the first one I set, doesn't like me trying to change it)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sunsett

Senior Member




ribbit!


« Reply #10 - Posted 2008-03-29 15:51:41 »

Not that you'd necessarily be interested, but you did contribute some code to JGN back in the day, and you know it's foundation.  It's actually being used currently in a few business applications now as well (non-game) since it's not explicitly tied to game development apart from the name. Wink  Creating an sending Message objects and adding listeners makes life pretty easy. Smiley
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2008-03-29 19:12:57 »

I'm not exactly sure what you're trying to say there, but I can't use JGN, as I'm redirecting raw traffic among servers, which is pretty much 'send what you receive', so there is no room for any protocol there. It just must be stable, it has to work for ever, and I hate it when it breaks down, because people rely on that silly app.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sunsett

Senior Member




ribbit!


« Reply #12 - Posted 2008-03-31 01:02:50 »

Oh well, you could still make the switch to NIO. Wink

BTW, I just wasted about 10 minutes on TinyCode:Capitalizer. :-p

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
boolean b = true;
int i;
while ((i = in.read()) != -1) {
char c = (char)i;
if (c == ' ') {
b = true;
} else if (b) {
if ((i > 96) && (i < 123)) c = (char)(i - 32);
b = false;
}
out.write(c);
}
Offline ddyer

Senior Member


Medals: 5



« Reply #13 - Posted 2008-04-22 11:40:24 »

I think it is just a fact of life that there are four ends of every socket pair, and when connections are severed
involuntarily, the four ends can be closed independently in any order, and the clients may or may not agree
on which pipes are open and which are broken.

The only solution is to have a live ping running, with the clients closing and attempting to re-establish
communication when a timeout occurs.  Depending on the underlying cause, this may or may not
be possible.

The most infuriating aspect of this is that there is no upper bound to how long a "temporary" interruption,
which will eventually be resolved without loss of data or connection, can last..  A few seconds is common,
a minute is not unheard of.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (12 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!