0

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Liviu
  • 51
  • 5

1 Answers1

1

I hope I got the question right, according to what I understood you could keep a global list of connected clients, for example:

List<class2> MyList;

When the class2 thread starts it adds itself to the list:

public class2(Socket socket) {
   try {
      this.socket = socket;
      in = new BufferedInputStream(socket.getInputStream());
      out = new DataOutputStream(socket.getOutputStream());
      MyList.add(this);
   }
   catch (IOException ex) 
   {
       Logger.getLogger(ListenerServerThread.class.getName()).log(Level.SEVERE, null, ex);
   }
}

When the thread terminates it removes itself from the list:

Try
{
   while (true) {
      try {
          // do something with in and out.
      } catch (IOException ex) {
         System.out.println(ex);
         break;
      } 
    }
} 
finally {
    for (int i=0; i < MyList.size(), i++)
       if (MyList.get(i) == this) 
       {
           MyList.delete(i);
           break;
       }
}

This way you could have a list of connected clients through its thread. You could add some method in your class2 to access more information, like the socket, the time it has been running, etc, etc. Also, you should consider accessing this list (when adding/deleting) with a mutex or some other synchronization method.

ja_mesa
  • 1,971
  • 1
  • 11
  • 7