How do I force multiple threads to use the same socket for reading and writing?

**

There will be multiple clients sending messages to the server on port 6069. I am looking for multiple requests at the same time, but I am not sure if the code below can do that.

There will be requests for the socket queue. Since there is only one thread, it will iterate through one request and then it will execute the next request from the queue. How can I serve multiple clients at the same time?

**

This is a class that creates a listening thread on port 6069.

public class NetworkModule extends Thread {
  private ServerSocket serverSocket;

  public NetworkModule(int port) throws IOException {

    serverSocket = new ServerSocket(port);
  }

    public void run() {
      while (true) {

                /* Here I read strings from the inputstream and write
                  to outputstream corresponding to "serverSocket" and call
                functions from other classes*/
      }
    }

      

}


below is how class class looks like

public class Main
{

   public static void main(String[] args) 
   {
       try
          {
            int port=6069;
             Thread t = new NetworkModule(port);
             t.start();

          }
       catch(IOException e)
          {
             e.printStackTrace();
          }

   }

}

      


+3


source to share


3 answers


If you can isolate the client handling method, say in a new class ClientHandler

that also extends Thread

where in run()

you read and write to streams. Then in run()

NetworkModule

:

while (true) {
     Socket socket = serverSocket.accept(); // blocks until new client connects
     ClientHandler handler = new ClientHandler(socket); // pass reference to socket
     handler.start(); // start executing in new thread
}

      



It is generally better implement Runnable

instead extending Thread

, because you can implement many interfaces, but only extend from one class due to the Java inheritance model.

+3


source


This is not the best way to write a server program! you are better off having two classes:

  • 1) client_handler: a class that handles clients and is responsible for serving clients. let's call it client_handler.java.
    One object of this class must be created for each client connected to the server.
    Clients must receive services in parallel, so this class must extend Thread or implement an interface Runnable

    .

  • 2) Server: Another class that is waiting for clients to connect! Name it: server.java
    this class implements a method main

    ! The server class should create a new thread for each connection so that clients can receive services in parallel.
    ***********************************
    below, there is some code to demonstrate what I said above:

    /*this is the server class i talked about : */
    public class server{
       static void main (String args[]){
            ServerSocket ss = new ServerSocket (args[0]);
            while (true){
                Socket  s =  ss.accept();
                client_handler ch = new client_handler(s);
                Thread t = new Thread(ch);
                t.start();
            }
        }
    
    }
    
          



and here is some sample code for client_handler:

public class client_handler implements Runnable{
    private Socket s ;
    private Scanner in ;
    print PrintWriter out;

    public client_handler(Socket s){
        this.s =s;
        in= new Scanner(s.getInputStream());
        out= new PrintWriter(s.getOutputStream());        
    }

   public void run(){
      // this is the entry of your thread . 
      // you can analyse the request received here . and send responses !

   }
}

      

0


source


You should modify your code in such a way to provide server side access for more clients to access (process) data sent on port 6069. Your client application, which is the second one, is just simply implemented with logic to connect to the active server (if is, or based on some rules, etc.). The server application provides access to magic. This means that every client sees every message sent by other clients and vice versa.

I find it helpful to read: Knock Knock Protocol . This will explain a lot to you, how things will be managed and they will work in your case.

-1


source







All Articles