Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1401 times)
0 Members and 1 Guest are viewing this topic.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Posted 2010-03-08 20: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 .

Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #1 - Posted 2010-03-08 22: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #2 - Posted 2010-03-08 22: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!
Play the free demo of Revenge of the Titans!
Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #3 - Posted 2010-03-08 22: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #4 - Posted 2010-03-08 22: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (143 views)
2013-05-17 21:29:12

alaslipknot (151 views)
2013-05-16 21:24:48

gouessej (182 views)
2013-05-16 00:53:38

gouessej (176 views)
2013-05-16 00:17:58

theagentd (186 views)
2013-05-15 15:01:13

theagentd (171 views)
2013-05-15 15:00:54

StreetDoggy (214 views)
2013-05-14 15:56:26

kutucuk (239 views)
2013-05-12 17:10:36

kutucuk (237 views)
2013-05-12 15:36:09

UnluckyDevil (243 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.127 seconds with 20 queries.