SocketServer accept() fails to work (Linux)

J

Janusz

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);
}
}
}
}
 
Y

Yu SONG

Janusz said:
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.
 
G

Gordon Beaton

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
 
J

Janusz

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
 
J

Janusz

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top