简体   繁体   中英

Socket Server accepting only one connection at a time on the same process

I'm trying to implement a client-server application in which the server may accept some Objects from the clients and for each Object it must interpret it as a message and handle it properly.

Here is the code:

(server)

public class GlobalServer{
GlobalServer(){new Thread(() ->{
    try {
        serverSocket = new ServerSocket(1234);
        Socket clientSocket;
        while (true) {
            clientSocket = serverSocket.accept();
            handleClient(clientSocket);
        }
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}).start();
}

public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
    ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
    Object [] objArr = (Object[]) is.readObject();
    msgHandler(objArr, clientSocket);
}

public void msgHandler(Object [] objArr, Socket clientSocket){
    int msg_type = (int) objArr[0];
    switch (msg_type) {
    case 1:
        System.out.println("type 1 received");
        break;
    case 2:
        System.out.println("type 2 received");
        break;
    case 3:
        System.out.println("type 3 received");
        break;

    default:
        break;
    }
}
public static void main(String [] args){
    GlobalServer s = new GlobalServer();
}
}

OBS: on the application it makes sense to receive a array of objects as each message carries its header (the type) and its content

(client)

public class Client {
    public static void main(String [] args){
        try {
            Socket client = new Socket("192.168.0.105", 1234);

            ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
            Object [] objArr = {3, "Type 3 message"};
            os.writeObject(objArr);

            Object []objArr1 = {1, "Type 1 message"};
            os.writeObject(objArr1);        
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   }

I run a instance of the GlobalServer on a separate machine and on another machine I connect to the server and send two messages sequentially. The probleme is that the server receives and handle only the first message and the second one get lost and the client finish without the server receive this second message. The strange thing is that if I send this two messagens as two diferent aplications the server handles it fine. It has something to do with the two messagens beeing sent on the same process?

Code working as diferent aplications:

(message 1)

public class Message1 {
    public static void main(String [] args){
        try {
            Socket client = new Socket("192.168.0.105", 1234);

            ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());

            Object [] objArr = {3, "Type 3 message"};
            os.writeObject(objArr);

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

(message 2)

public class Message2 {
    public static void main(String [] args){
        try {
            Socket client = new Socket("192.168.0.105", 1234);

            ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());

            Object []objArr1 = {1, "Type 1 message"};
            os.writeObject(objArr1);        
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   }

Problem is in following code block:

public void handleClient(Socket clientSocket) throws IOException, ClassNotFoundException{
    ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
    Object [] objArr = (Object[]) is.readObject();
    msgHandler(objArr, clientSocket);
}

You are only reading one object. (Object[]) is.readObject(); shall be called in a loop in order to read multiple objects and call msgHandler method for each object.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM