微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

邮件发送后,客户端应如何适当地等待服务器的响应?

如何解决邮件发送后,客户端应如何适当地等待服务器的响应?

我正在尝试使客户端和服务器正确地回显消息。

这意味着客户端应该向服务器发送一条消息,服务器应该接收并发送相同的消息给客户端,然后客户端打印出收到的消息。

我无法让客户端正确地等待服务器完成响应发送,然后客户端才接收并打印出来。

唯一可行的方法是当我注释掉EchoClient2.java RECEIVE MESSAGE代码时。

EchoClient2.java

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class EchoClient2 {
    private Socket clientSocket;
    private DataOutputStream out;
    private DataInputStream in;

    private void start(String ip,int port) {
        try {
            clientSocket = new Socket(ip,port);
            out = new DataOutputStream(clientSocket.getoutputStream());
            in = new DataInputStream(clientSocket.getInputStream());
        } catch (IOException e) {
            System.out.println("Error when initializing connection");
        }
    }

    private void sendMessage(String message) throws IOException {
        //SENDING MESSAGE
        byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);

        System.out.println("Client will send the text: " + message);

        out.write(messageBytes);
        out.flush();
    
        //RECEIEVE MESSAGE - Doesn't give server a chance to respond?
        byte[] data = new byte[8];
        ByteArrayOutputStream getData = new ByteArrayOutputStream();
        int numBytes;
        while ((numBytes = in.read(data)) != -1) { 
            getData.write(data,numBytes);
        }
        
        byte[] message2Bytes = getData.toByteArray();
        String text = new String(message2Bytes,StandardCharsets.UTF_8);
        
        System.out.println("Server sent the client text: " + text);
    }

    private void stop() {
        try {
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException e) {
            System.out.println("error when closing");
        }
    }

    public static void main(String[] args) throws IOException {
        EchoClient2 client = new EchoClient2();
        client.start("127.0.0.1",4444);
        client.sendMessage("exit");
        client.stop();
    }
}

EchoServer2.java

import java.net.*;
import java.nio.charset.StandardCharsets;
import java.io.*;

public class EchoServer2 {
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private DataOutputStream out;
    private DataInputStream in;

    public void start(int port) {
        try {
            serverSocket = new ServerSocket(port);
            clientSocket = serverSocket.accept();
            out = new DataOutputStream(clientSocket.getoutputStream());
            in = new DataInputStream(clientSocket.getInputStream());   
        } catch (IOException e) {
            System.out.println("Error when establishing/accepting server connection");
        }
    }

    private void actionMessage() throws IOException {
        //RECEIEVE MESSAGE
        byte[] data = new byte[8];
        ByteArrayOutputStream getData = new ByteArrayOutputStream();
        int numBytes;
        while ((numBytes = in.read(data)) != -1) { 
            getData.write(data,numBytes);
        }

        byte[] messageBytes = getData.toByteArray();
        String text = new String(messageBytes,StandardCharsets.UTF_8);
        
        System.out.println("Server got " + text + " and will send this back to client");

        //SENDING BACK SAME MESSAGE
        out.write(messageBytes);
        out.flush();
    }

    public void stop() {
        try {
            in.close();
            out.close();
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        EchoServer2 server = new EchoServer2();

        System.out.println("Now waiting for incoming connection...");

        server.start(4444);

        server.actionMessage();
        
        server.stop();
    }
}

有人知道为什么这不能正常工作吗?

解决方法

服务器正在读取8字节块中的客户端输入,直到客户端断开连接(它没有这样做),然后它正在写回响应。相反,服务器需要写出每个块到达时,然后在客户端断开连接后断开连接。这意味着在out.write()阅读循环内移动while

private void actionMessage() throws IOException {
    byte[] data = new byte[8];
    int numBytes;
    while ((numBytes = in.read(data)) != -1) {
        out.write(data,numBytes);
        out.flush();
    }
} 

这对于简单的回显服务器来说很好,但是对于更现实的消息传递服务器来说却不是很有用。您需要一个更定义的协议来回交换消息,而不必失去它们之间的连接(除非您想实现无状态协议,例如HTTP,它允许请求之间的断开连接)。例如:

客户:

private void sendMessage(String message) throws IOException {
    System.out.println("Client will send the text: " + message);

    //SEND MESSAGE
    byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
    out.writeInt(messageBytes.length);
    out.write(messageBytes);
    out.flush();

    //RECEIVE MESSAGE
    int len = in.readInt();
    byte[] data = new byte[len];
    in.readFully(data);
    String text = new String(data,StandardCharsets.UTF_8);
    System.out.println("Server sent the client text: " + text);
}

public static void main(String[] args) throws IOException {
    EchoClient2 client = new EchoClient2();
    client.start("127.0.0.1",4444);
    try {
        client.sendMessage("hello");
        client.sendMessage("exit");
    } catch (IOException e) {
        ...
    }
    client.stop();
}

服务器:

private void actionMessage() throws IOException {
    //RECEIVE MESSAGE
    int len = in.readInt();
    byte[] data = new byte[len];
    in.readFully(data);
    String text = new String(data,StandardCharsets.UTF_8);
    System.out.println("Server got " + text + " and will send this back to client");

    //SEND MESSAGE
    data = text.getBytes(StandardCharsets.UTF_8);
    out.writeInt(data.length);
    out.write(data);
    out.flush();
}

public static void main(String[] args) throws IOException {
    EchoServer2 server = new EchoServer2();
    System.out.println("Now waiting for incoming connection...");
    server.start(4444);
    while (true) {
        try {
            server.actionMessage();
        } catch (IOException e) {
            ...
            break;
        }
    }
    server.stop();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。