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