public class Connection {
public void connect() {
closed = false;
ConnectThread thread = new ConnectThread();
thread.start();
}
public boolean isConnected() {
return connected;
}
public boolean reconnect() {
boolean rtn = false;
try {
socket.disconnect();
} catch(Exception e) {}
}
try {
socket.connect();
rtn = true;
} catch (Exception e) {}
return rtn;
}
private class ConnectThread {
public void run() {
while(!this.isInterrupted() && !closed) {
try {
if (reconnect()) {
synchronized(lock) {
connected = true;
}
CheckAliveThread thread = new CheckAliveThread();
thread.start();
}
} catch(Exception e) {
}
try {
sleep(1000l);
} catch (Exception e) {
}
}
}
}
}
Connection connect = new Connection();
connect.connect();
while(!connect.isConnected()) {
Thread.sleep(3000l);
}