简体   繁体   English

套接字服务器在同一进程上一次仅接受一个连接

[英]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 OBS:在应用程序上,有意义的是接收对象数组,因为每个消息都带有其标头(类型)及其内容

(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. 我在单独的计算机上运行GlobalServer的实例,然后在连接到服务器的另一台计算机上运行,​​并顺序发送两个消息。 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. 奇怪的是,如果我将这两个messagens作为两个不同的应用发送,则服务器会很好地处理它。 It has something to do with the two messagens beeing sent on the same process? 这与在同一过程中发送的两个消息蜂有关吗?

Code working as diferent aplications: 代码用作不同的应用程序:

(message 1) (消息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) (消息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. 为了读取多个对象并为每个对象调用msgHandler方法,应在循环中调用该方法。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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