PDA

Click to See Complete Forum and Search --> : socket problem


Justa Lol
Jun 4th, 2009, 08:25 AM
i have made a socketed application, but i have a problem now...
when someone connects, they connect without problems, but when they log out, often they log out and just leave the connection on the server, which will create a null... i want to terminate the connection when they log out.

my socket

public void run() {
// setup the listener
try {
shutdownClientHandler = false;
clientListener = new java.net.ServerSocket(serverlistenerPort, 1,
null);

while (true) {
java.net.Socket s = acceptSocketSafe(clientListener);

s.setTcpNoDelay(true);
String connectingHost = s.getInetAddress().getHostName();

if (/* connectingHost.startsWith("localhost") || connectingHost.equals("127.0.0.1")*/true) {
if (connectingHost.startsWith("computing")
|| connectingHost.startsWith("server2")) {
misc.println(
connectingHost
+ ": Checking if server still is online...");
} else {
int Found = -1;

for (int i = 0; i < MaxConnections; i++) {
if (Connections[i] == connectingHost) {
Found = ConnectionCount[i];
break;
}
}
if (Found < 3) {
misc.println(
"Connection accepted from " + connectingHost
+ ": " + s.getPort() + ".");
playerHandler.newPlayerClient(s, connectingHost);
} else {
s.close();
s = null;
}
}
} else {
misc.println(
"ClientHandler: Rejected " + connectingHost + ":"
+ s.getPort());
s.close();
s = null;
}
}
} catch (java.io.IOException ioe) {
if (!shutdownClientHandler) {
misc.println("Error: KILL " + serverlistenerPort);
} else {
misc.println("ClientHandler was shut down.");
}
}
}