简体   繁体   English

没有收到来自服务器的响应

[英]No response received from the server

I can't receive a response from the server. 我无法从服务器收到响应。 When I start the client and the server nothing happens. 当我启动客户端和服务器时,什么也没有发生。

Here is the Server Code 这是服务器代码

public class Server {

    public static void main(String[] zero) throws IOException {
        ServerSocket socketServer;
        Socket socketDuServer;
        PrintWriter out;
        BufferedReader in;
        socketServer = new ServerSocket(2009);
        socketDuServer = socketServer.accept();
        in = new BufferedReader(new InputStreamReader(socketDuServer.getInputStream()));
        String message = in.readLine();
        out = new PrintWriter(socketDuServer.getOutputStream(), true);
        String numberMessage = "The Number of ET est:" + StringToArrayChar(message);
        System.out.println(numberMessage);
        out.print(numberMessage);
        out.flush();
        socketDuServer.close();
        socketServer.close();
    }

    public static int StringToArrayChar(String s) {
        int c = 0, k = 0;
        char[] charArray = s.toCharArray();
        for (int j = 0; j < s.length(); j++) {
            if (charArray[j] == 'e') {
                k = j + 1;
                if (charArray[k] == 't') {
                    c++;
                }
            }
        }
        return c;
    }
}

and here is the Client Code 这是客户代码

public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket;
        BufferedReader in;
        PrintWriter out;
        String message;
        socket = new Socket(InetAddress.getLocalHost(), 2009);
        out = new PrintWriter(socket.getOutputStream(), true);
        out.print("helloet");
        out.flush();
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        message = in.readLine();
        System.out.println(message);
        socket.close();
    }
}

Your server tries to receive a line but your client never sends a line. 您的服务器尝试接收一条线路,但是您的客户端从不发送一条线路。

There is no way to fix this problem. 没有办法解决此问题。 Why? 为什么? Because we cannot tell whether the server is incorrect in trying to receive a line or the client is incorrect in not sending a line. 因为我们无法判断服务器尝试接收线路是不正确还是客户端不发送线路是不正确。 The only we could tell is by consulting the specification for the protocol which explains how the server and client exchange data. 我们唯一可以说的是通过查阅协议规范来解释服务器和客户端如何交换数据。 And there isn't one. 而且没有一个。 Fail. 失败。

Please take a huge step back. 请退后一步。 Read the specifications for a few protocols layered on top of TCP such as HTTP, IRC and SMTP. 阅读TCP之上的一些协议的规范,例如HTTP,IRC和SMTP。 Then, before you start writing code that uses TCP, spend the time to clearly document your protocol. 然后,在开始编写使用TCP的代码之前,请花时间清楚地记录您的协议。 This will avoid mistakes like this one, and the hundreds of others people who first try to use TCP inevitably make. 这样可以避免像这样的错误,而其他许多尝试使用TCP的人不可避免地会犯错误。

I know this sounds like a huge pain. 我知道这听起来很痛苦。 But trust me, there are dozens of similar mistakes just waiting for you to make them. 但是请相信我,有数十种类似的错误正等着您做出。

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

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