SocketServer accept() fails to work (Linux)

Discussion in 'Java' started by Janusz, May 31, 2004.

  1. Janusz

    Janusz Guest

    Hello,

    I have the following problem with my server appllication:

    This is typical server implemented in Java, using ServerSocket object,
    waiting for client to connenct on ServerSocket.accept()
    method. When the client connects (socket is received from accept() method)
    it is handled by the server in separate thread.
    The server writes the received data to the file and then closes the socket.
    The problem:
    the server handles about 20 connections daily, receiving about 100kB - 1MB
    of data in each connection.
    However after a few days the server fails to work,
    the server waits on ServerSocket.accept() (as usually), however the clients
    fail to connect to the server, but the server does not see
    that any client tried to connect to it, it just waits on
    ServerSocket.accept().

    I'm using jre1.4.2_04 on Linux - Red Hat 9

    Is there some bug in java-Linux-socket implementation or in my code ?
    Thanks in advance.

    This is the result of system netstat (after a few clients failed to connect
    to the server):

    tcp 78 0 localhost:32323 localhost:2560 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2568 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2576 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2584 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2340 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2592 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2348 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2344 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2356 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2352 CLOSE_WAIT
    tcp 78 0 localhost:32323 localhost:2608 CLOSE_WAIT

    A snippet of the server source:

    private class Server
    {
    private ServerSocket _serverSckt = null;
    private boolean _break = false;

    ServerRunnable()
    {
    _serverSckt= new ServerSocket(32323);
    _serverSckt.setSoTimeout(2000);
    }

    public void exeServer()
    {
    try {
    Socket clientSckt = null;
    while (!_break)
    {
    try {
    clientSckt = _serverSckt.accept();

    } catch (SocketTimeoutException ex) {
    // ok, timeout occured, while loops again
    } catch (InterruptedIOException ex) {
    final String sMsg = "_serverSckt Interrupred!!!
    managed to transfer " + ex.bytesTransferred + " [bytes]";
    _LOGGER.error(sMsg, ex);
    }

    if (clientSckt != null) {

    getClientHandler().handleClient(clientSckt); //
    handles client in separate thread
    // note: clientSckt is closed by client handler

    clientSckt = null;
    }
    }
    } catch (IOException e) {
    _LOGGER.error(e);
    } catch (Throwable thr) {
    _LOGGER.fatal(thr);
    } finally {
    _serverRunning = false;
    try {
    _serverSckt.close();
    } catch (IOException e) {
    Assert.catchReport(e);
    }
    }
    }
    }
    Janusz, May 31, 2004
    #1
    1. Advertising

  2. Janusz

    Yu SONG Guest

    Janusz wrote:
    > Hello,
    >
    > I have the following problem with my server appllication:
    >
    > This is typical server implemented in Java, using ServerSocket object,
    > waiting for client to connenct on ServerSocket.accept()
    > method. When the client connects (socket is received from accept() method)
    > it is handled by the server in separate thread.
    > The server writes the received data to the file and then closes the socket.
    > The problem:
    > the server handles about 20 connections daily, receiving about 100kB - 1MB
    > of data in each connection.
    > However after a few days the server fails to work,
    > the server waits on ServerSocket.accept() (as usually), however the clients
    > fail to connect to the server, but the server does not see
    > that any client tried to connect to it, it just waits on
    > ServerSocket.accept().
    >
    > I'm using jre1.4.2_04 on Linux - Red Hat 9
    >
    > Is there some bug in java-Linux-socket implementation or in my code ?
    > Thanks in advance.
    >
    > This is the result of system netstat (after a few clients failed to connect
    > to the server):
    >
    > tcp 78 0 localhost:32323 localhost:2560 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2568 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2576 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2584 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2340 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2592 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2348 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2344 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2356 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2352 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2608 CLOSE_WAIT
    >
    > A snippet of the server source:
    >
    > private class Server
    > {
    > private ServerSocket _serverSckt = null;
    > private boolean _break = false;
    >
    > ServerRunnable()
    > {
    > _serverSckt= new ServerSocket(32323);
    > _serverSckt.setSoTimeout(2000);
    > }
    >
    > public void exeServer()
    > {
    > try {
    > Socket clientSckt = null;
    > while (!_break)
    > {
    > try {
    > clientSckt = _serverSckt.accept();
    >
    > } catch (SocketTimeoutException ex) {
    > // ok, timeout occured, while loops again
    > } catch (InterruptedIOException ex) {
    > final String sMsg = "_serverSckt Interrupred!!!
    > managed to transfer " + ex.bytesTransferred + " [bytes]";
    > _LOGGER.error(sMsg, ex);
    > }
    >
    > if (clientSckt != null) {
    >
    > getClientHandler().handleClient(clientSckt); //
    > handles client in separate thread
    > // note: clientSckt is closed by client handler
    >
    > clientSckt = null;
    > }
    > }
    > } catch (IOException e) {
    > _LOGGER.error(e);
    > } catch (Throwable thr) {
    > _LOGGER.fatal(thr);
    > } finally {
    > _serverRunning = false;
    > try {
    > _serverSckt.close();
    > } catch (IOException e) {
    > Assert.catchReport(e);
    > }
    > }
    > }
    > }
    >
    >
    >


    The "SocketTimeoutException" may raise at that time.
    put ex.printStackTrace() there to do more tests.


    --
    Song

    More info.:
    http://www.dcs.warwick.ac.uk/~esubbn/
    Yu SONG, Jun 1, 2004
    #2
    1. Advertising

  3. On Mon, 31 May 2004 16:49:35 +0200, Janusz wrote:
    > This is typical server implemented in Java, using ServerSocket
    > object, waiting for client to connenct on ServerSocket.accept()
    > method. When the client connects (socket is received from accept()
    > method) it is handled by the server in separate thread. The server
    > writes the received data to the file and then closes the socket. The
    > problem: the server handles about 20 connections daily, receiving
    > about 100kB - 1MB of data in each connection. However after a few
    > days the server fails to work, the server waits on
    > ServerSocket.accept() (as usually), however the clients fail to
    > connect to the server, but the server does not see that any client
    > tried to connect to it, it just waits on ServerSocket.accept().


    [...]

    > This is the result of system netstat (after a few clients failed to
    > connect to the server):
    >
    > tcp 78 0 localhost:32323 localhost:2560 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2568 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2576 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2584 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2340 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2592 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2348 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2344 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2356 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2352 CLOSE_WAIT
    > tcp 78 0 localhost:32323 localhost:2608 CLOSE_WAIT


    So where's the LISTEN socket?

    Some things to test:

    - does client handler really close every client socket?
    - "lsof -i tcp:32323" (as root) to find any open descriptors
    - "ulimit -a" in same shell you start server process (look at "open files")
    - tcpdump while connection fails

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 1, 2004
    #3
  4. Janusz

    Janusz Guest


    > > tcp 78 0 localhost:32323 localhost:2352 CLOSE_WAIT
    > > tcp 78 0 localhost:32323 localhost:2608 CLOSE_WAIT

    >
    > So where's the LISTEN socket?



    Sorry, I haven't run netstat with -l options (which shows the LISTEN ports),
    I will do it now.

    > - does client handler really close every client socket?
    > - "lsof -i tcp:32323" (as root) to find any open descriptors
    > - "ulimit -a" in same shell you start server process (look at "open

    files")
    > - tcpdump while connection fails
    >


    Thanks for the reply, I will gather the suggested infos next time the server
    fails,

    Regards,

    Janusz
    Janusz, Jun 1, 2004
    #4
  5. Janusz

    Janusz Guest


    > >

    >
    > The "SocketTimeoutException" may raise at that time.
    > put ex.printStackTrace() there to do more tests.
    >
    >


    Thanks for the reply, I will add the suggested debug info,
    and wait for another server failure :)
    Janusz, Jun 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Sidwell
    Replies:
    7
    Views:
    2,671
    Gavin Williams
    Jun 24, 2004
  2. Andrea Desole
    Replies:
    2
    Views:
    893
    Andrea Desole
    Aug 31, 2005
  3. Barry Sprajc
    Replies:
    1
    Views:
    377
    Skip Montanaro
    Aug 3, 2003
  4. MrSmile
    Replies:
    3
    Views:
    167
    Tamer Higazi
    Nov 1, 2011
  5. Eli Sidwell
    Replies:
    1
    Views:
    119
    John Bokma
    Jun 10, 2004
Loading...

Share This Page