I have a socket client application which listens for incoming connections in the main thread on a specified port,accepts incoming connections and starts a user thread for each connection.
this setup works for somedays and then the applicaiotns stops accepting any new new connections. the only way to recover is to restart the application. I have no clue why this happens... here is my main which accpets and starts a new thread for each connection.
while(ServerOn)
{
ServerSocket myServerSocket;
private static ArrayList<Socket> connecitons;
try {
// Accept incoming connections.
Socket conn = myServerSocket.accept();
// adds the new connections to the socket
connections.add(conn);
// accept() will block until a client connects to the server.
// If execution reaches this point, then it means that a client
// socket has been accepted.
// For each client, we will start a service thread to
// service the client requests.
// Start a Service thread
ServerThread cliThread = new ServerThread(conn);
cliThread.start();
} catch (IOException ioe) {
System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
ioe.printStackTrace();
}
}
try {
myServerSocket.close();
System.out.println("Server Stopped");
} catch (Exception ioe) {
System.out.println("Problem stopping server socket");
System.exit(-1);
}
}
please help.
EDIT 1
here is the class declaration:
class ServerThread extends Thread {
Socket conn;
boolean m_bRunThread = true;
InputStream in = null;
OutputStream outStream = null;
//calling the 1-argument constructor with the socket parameters
ServerThread(Socket s) {
conn = s;
}
//Subclasses of Thread should override this method.
public void run() {
//lot of variables declared here.
try {
// Class InputStream is used for receiving data (as bytes) from client.
in = conn.getInputStream();
// Class OutputStream is used for sending data (as bytes) to client.
outStream = conn.getOutputStream();
/*
* 1. Go to Read Thread
* 2. Check for incoming data stream from Client
* 3. Go to read routine and process only if the data is received from the client
* 4. If there is no data for X minutes then close the socket.
*/
conn.setSoTimeout(time in milliseconds);
String inLine=null;
while (m_bRunThread) {
// read incoming stream
while ((c=in.read(rec_data_in_byte))!= -1)
{...............
and the run method continues...
ConcurrentModificationExceptionfor example, which is a runtime exception. When your application stops accepting, (a) is it still running, and (b) doesnetstat -nap tcpstill show the port as LISTENING? I would also like to know exactly what the constructor ofServerThreaddoes. If it does any I/O at all, even constructing object streams, it shouldn't.