I have a class called class1 in which I have the following code:
public class class1 {
public static ServerSocket serverSocket;
public static void main(String args[]) {
try {
serverSocket = new ServerSocket(1234);
while (true) {
class2 t = new class2(serverSocket.accept());
t.start();
}
} catch (IOException ex) {
Logger.getLogger(LisenerServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
So in class 1 I open a new thread for each connection.
And I have another class called class2 in which I have the following code:
public class class2 extends Thread {
private Socket socket;
public class2(Socket socket) {
try {
this.socket = socket;
in = new BufferedInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(ListenerServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run() {
while (true) {
try {
// do something with in and out.
} catch (IOException ex) {
System.out.println(ex);
break;
}
}
}
}
I want clients (not java programs) to connect to my socket server and send sting commands, and I want to know if a client (I will identify the client by the thread name and/or id opened in class 1) is connected or not.
Can I make another class let's say class 3 in which I open a single thread to continuously check if the clients are connected or not? If I can, can you please give me an example?