Java客户端/服务器问题通过套接字发送对象[重复]

时间:2022-08-26 19:53:39

This question already has an answer here:

这个问题在这里已有答案:

I'm creating a standard multi-client/server program in Java. The server accepts connections and spawns a new thread to handle each one. The client also spawns a thread to wait for messages from the server. The client and server communicate by passing Message objects through ObjectInputStream and ObjectOutputStreams.

我正在用Java创建一个标准的多客户端/服务器程序。服务器接受连接并生成一个新线程来处理每个连接。客户端还生成一个线程来等待来自服务器的消息。客户端和服务器通过MessageInputStream和ObjectOutputStreams传递Message对象进行通信。

The initial handshake works fine. When the client starts, it opens a socket connection to the server. The server accepts the socket, sends a Message to the client that the connection was successful. Then the client sends its username back, and both client and server start waiting for messages.

初始握手工作正常。当客户端启动时,它会打开与服务器的套接字连接。服务器接受套接字,向客户端发送连接成功的消息。然后客户端返回其用户名,客户端和服务器都开始等待消息。

Then I send some text from my client which creates a chat message, and sends it successfully to the server. The server receives this message, and attempts to send it out to all connected clients, which it does (there's only one). The problem is that this message never gets back to the client.

然后我从我的客户端发送一些文本,创建聊天消息,并将其成功发送到服务器。服务器收到此消息,并尝试将其发送给所有连接的客户端(它只有一个)。问题是这条消息永远不会回到客户端。

// This is Message.send
public void send(ObjectOutputStream stream) throws IOException{
        stream.writeObject(this);
}

// ClientThread.run
public void run(){
    try {
        out = client.getOutputStream();
        out.flush();
        ObjectInputStream in = client.getInputStream();

        Message msg = null;
        int len;
        byte[] bytes = null;
        int i = 0;

        // Continuously read new Messages from the server
        while(true){
            msg = (Message)in.readObject();

            processInput(msg);
        }
    } catch (Exception e) {
        Util.showError(e.getMessage(), "Connection Error");
    }

    System.out.println("Client exited");
}

// ServerThread.run
public void run() {
    try {
        out = new ObjectOutputStream(client.getOutputStream());

        ObjectInputStream in = new ObjectInputStream(client.getInputStream());

        Message msg = null;

        while(client.isConnected()){
            msg = (Message)in.readObject();
            processInput(msg);
        }

        in.close();
        client.close();
    } catch (Exception e) {
        server.addMessage(e.getMessage());
    }
}

1 个解决方案

#1


0  

I don't see any calls to flush(), without which the data may never make it from point a to point b.

我没有看到对flush()的任何调用,没有它,数据可能永远不会从点a到点b。

I'd recommend using the ObjectOutputStream atop a ByteArrayOutputStream, and pushing the resulting byte array across the wire, and then reversing the process on the other end.

我建议在ByteArrayOutputStream上使用ObjectOutputStream,并在线上推送生成的字节数组,然后在另一端反转该过程。

#1


0  

I don't see any calls to flush(), without which the data may never make it from point a to point b.

我没有看到对flush()的任何调用,没有它,数据可能永远不会从点a到点b。

I'd recommend using the ObjectOutputStream atop a ByteArrayOutputStream, and pushing the resulting byte array across the wire, and then reversing the process on the other end.

我建议在ByteArrayOutputStream上使用ObjectOutputStream,并在线上推送生成的字节数组,然后在另一端反转该过程。