Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  TCP ServerSocket stops listening  (Read 2154 times)
0 Members and 1 Guest are viewing this topic.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Posted 2010-03-08 19:48:12 »

Hello guys . I am having a really strange problem with my ServerSockets.
first, this is my code of starting a serverSocket:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
ServerSocket serverSocket = new ServerSocket(P.PORT);

while (true)
 {

 ...

 Socket connectionSocket= serverSocket .accept();
//launch thread to listen to data on this connectionSocket and go to accept new connections
}


basically that's it . It works quite fine , for a long while . It accepts the connections sends and receives a few data, then closes the connection .
 
But something happens that it just does not accept connections anymore . In the client, I don't get any errors, the thread just hangs waiting for the connection (if a timeout is not set). I dont get a "Server not found", it seems that it can see the server, but the connection is never stabilished.

Sometimes  my serverSocket lasts for a week, sometimes a few days . That's with 200 connections/day .

Anybody has an idea ?
Thanks in advance .

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-03-08 21:22:20 »

I have had ServerSockets listening for many months, with hundreds of connections per second, not a single problem.

Probably out of file descriptors?

For a more robust serversocket listener:
1. ensure all connections are properly closed, no matter what
2. prevent a flood-attack from bringing down your server (lack of file descriptors will crash other processes too)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
final Semaphore preventFlooding = new Semaphore(maxConcurrentConnections);

while(true)
{
   preventFlooding.acquireUninterruptibly();
   final Socket socket;
   try {
       socket = server.accept();
   } catch(IOException exc) {
       preventFlooding.release();
       continue; // maybe next time!
  }

   Runnable task = new Runnable() {
      public void run() {
           InputStream input = null;
           OutputStream output = null;
           try {
               socket.setSoTimeout(2000); // we must receive the first byte within 2 sec, change your timeout later to something larger
              input = socket.getInputStream();
               output = socket.getOutputStream();

               DO_A_LOT_OF_WORK(socket, input, output);
           }
           finally {
               preventFlooding.release();

               try { if(input!=null) input.close(); } catch(IOException exc) { /* ignore */ }
               try { if(output!=null) output.close(); } catch(IOException exc) { /* ignore */ }
               try { socket.close(); } catch(IOException exc) { /* ignore */ }
           }
      }
   }

   // better: a thread-pool, with a few connections per second
  // creating threads really starts to drain performance
  new Thread(task).start();
}


Regarding the flood attack: I got hit by this so many times that it's silly. Thousands of connections in a few seconds, and if you're not careful, something breaks. Either VM heap, RAM (swapping), threads, running out of file descriptors... that is a serious issue for a server, as every process relies on them. Avoid IOException("Too many files open") at all cost, and my Semaphore will cap your file-descriptor count pretty well.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #2 - Posted 2010-03-08 21:44:45 »

Riven, that way I would only accept connections  after I DO_A_LOT_OF_WORK.
This is not actually desirable, since this can take several seconds, and I would be out of listeners in that interval, right ?
Of course I could start like 5 Threads of listeners but then I think it just breaks the anti-flooding strategy that the semaphore is there for .

I think I'll stick to 1. ensure all connections are properly closed, no matter what 
Come to think of it, I wrote my networking code a long time ago, so there is probably some error handling missing .

Thank you for now .

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-03-08 21:47:56 »

Riven, that way I would only accept connections  after I DO_A_LOT_OF_WORK.

No.

You can have N (=maxConcurrentConnections) threads doing A_LOT_OF_WORK(...)

See: new Thread(task).start()

If you set maxConcurrentConnections to 100, you will at most have 100 thread handling 100 connections.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #4 - Posted 2010-03-08 21:52:17 »

ah okay I just skipped the Semaphore(maxConcurrentConnections); and thought of a mutex .

Thanks again

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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!