javasocketsserversocketobjectoutputstream

ObjectoutStram not set when trying to connect two client Socket to a server


I Have Class like below trying to connect two client socket to a server but when they get accepted by server I can only send data to the server through first socket (named s1 in code) and the second socket can do not send data to the server

public class Client_1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    Socket s1 = new Socket("localhost", 8888);
    Socket s2 = new Socket("localhost", 8888);
    BufferedOutputStream bos1 = new BufferedOutputStream(s1.getOutputStream());
    ObjectOutputStream oos1 = new ObjectOutputStream(bos1);
    oos1.flush();
    BufferedOutputStream bos2 = new BufferedOutputStream(s2.getOutputStream());
    ObjectOutputStream oos2 = new ObjectOutputStream(bos2);
    oos2.flush();
    BufferedInputStream bis1 = new BufferedInputStream(s1.getInputStream());
    ObjectInputStream ois1 = new ObjectInputStream(bis1);
    BufferedInputStream bis2 = new BufferedInputStream(s2.getInputStream());
    ObjectInputStream ois2 = new ObjectInputStream(bis2);


        oos1.writeObject("a message from first client s1");
        oos1.flush();
        oos2.writeObject("a message from second client s2"); // sever does not receive this one
        oos2.flush();
}
}

here is server code waiting for client

public class Main {
    public static void main(String[] args) throws IOException {

        WaitForClient();
    }

    public static void WaitForClient() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        int i = 0;
        while(true) {
            Socket client = serverSocket.accept();
            i++;
            System.out.println(i + " client connected");
            ClientThread clientThread = new ClientThread(client);
            Thread thread = new Thread(clientThread);
            thread.setDaemon(true);
            thread.start();
        }

    }

and this is ClientThread who get info from socket

public class ClientThread implements Runnable {
    
    Socket clientSocket;
    ObjectInputStream oIStream;
    ObjectOutputStream oOStream;
    Object inputObject;
    BufferedInputStream bIS;
    BufferedOutputStream bOS;
    

    public ClientThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        try {
            bOS = new BufferedOutputStream(clientSocket.getOutputStream());
            bIS = new BufferedInputStream(clientSocket.getInputStream());
            oOStream = new ObjectOutputStream(bOS);
            oOStream.flush();
            oIStream = new ObjectInputStream(bIS);

            while (clientSocket.isConnected()) {
                if (bIS.available() > 0) {
                    inputObject = oIStream.readObject();
                    doService(inputObject);
                    System.out.println(inputObject.toString());
                    inputObject = null;
                }
            }
            System.out.println("connection is closed!!!");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("socket exception" + e.getMessage());
        }

    }
}

and this is what printed to console

1 client connected
2 client connected
a message from first client s1   // input from the first socket but nothing from the second socket

Solution

  • This code should work,Are you getting any error in doService method?. In case any exception while loop will break and print statement will not be executed. Otherwise it should print data from both client