javasocketsserversocket

thread on server will stop and I have no idea why


i am making a client handler for my chat app so I can have more then one client connected but right it just 2 connections and the 2 connections run on 2 threads(1 client per thread) now everything work fine and the server will get info from both client but after a while (1 sec) which every client will talk with the last will stay connected and the server stops the other thread running the other connection and I have no idea why

Server code

import java.io.IOException;
import java.net.ServerSocket;


public class server{
   
    public static ServerSocket serverSocket;
    public static void main(String[] args)throws IOException{
    serverSocket = new ServerSocket(1234);        
    clientHandler clientHandler = new clientHandler();
    Thread clienThread = new Thread(clientHandler);
    Thread clienThread2 = new Thread(clientHandler);
    clienThread.start();
    clienThread2.start();
    }
}

client Handler code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class clientHandler implements Runnable{
    public static server server = new server();
    public static Socket socket;
    public static BufferedReader in;
    public static PrintWriter out;

    @Override
    public void run(){
    try {
    socket = server.serverSocket.accept();
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream(), true);
    while(true){
        String msg = in.readLine();
        System.out.println(msg);
    }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

I tried to use 2 server sockets on 2 different port but got the same problem so please help


Solution

  • Usually, you create a single ServerSocket and a loop where

    You have two threads and each thread calls socket = server.serverSocket.accept(); I'm not sure how that works but I don't recommend it.

    I've modified your code with as few changes as I could so you can follow it:

    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server
    {
        public static class ClientHandler implements Runnable
        {
            private Socket socket;
            private Thread thread;
            
            public ClientHandler(Socket socket) {
                this.socket = socket;
                thread = new Thread(this);
                thread.start();
            }
    
            @Override
            public void run() {
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    String msg;
                    while ((msg = in.readLine()) != null) {
                        System.out.println(msg);
                        out.println(msg);
                    }
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args)throws IOException {
            ServerSocket serverSocket = new ServerSocket(1234);
            while (true) {
                Socket socket = serverSocket.accept();
                ClientHandler clientHandler = new ClientHandler(socket);
            }
        }
    }
    
    

    Note: this allows an "infinite" number of clients to connect. If you want to cap it at 2, you'll need to add a little extra logic.