I have a problem with the server of Reign of Rebels , that after a few days running, it simply stops accepting connections in an unusual way.
The client tries to connect, it gets no error at first. Instead, it takes 50 - 80 seconds or so for the client to get a connection error. As if the client were negotiating connection.
If server is down, the client gets the message immediatelly.
I believe there’s something wrong my code, I’ll post the code I use to accept/close connection and would appreciate if somebody sees something weird.
Accepting new connections. I start 10 Threads of these:
public void run()
{
while (true)
{
MMTCPConnection c = new MMTCPConnection(welcomeSocket, this);
addConnection(c); /* this does not block, definetely*/
}
}
this is in the constructor of MMTCPConnection :
Socket connectionSocket;
....
try
{
connectionSocket = ss.accept();
inFromClient = new DataInputStream(connectionSocket.getInputStream());
outToClient = new DataOutputStream( new BufferedOutputStream( connectionSocket.getOutputStream()));
}catch (Exception e) {
e.printStackTrace();
try {
connectionSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
In the class MMTCPConnection, when any error occur (including client closed connection), I call this method :
Socket connectionSocket;
DataOutputStream outToClient;
DataInputStream inFromClient;
private void closeConnection()
{
server.connectionsV.remove(this.id);
try
{
inFromClient.close();
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
outToClient.close();
}
catch (Exception e)
{
e.printStackTrace();
}
inFromClient=null;
outToClient=null;
try {
connectionSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("closed connection . "+(int)id);
}
Am I missing something ?