为什么我的服务器不响应TCP数据包

时间:2019-07-08 15:34:27

标签: java networking tcp

我正在尝试将TCP数据包发送到服务器,然后服务器通过发送更多数据包来响应,但这些数据包似乎无法正常工作,并且出现了令人讨厌的“连接超时”错误。

这是我的代码,试图将数据包发送到服务器

试图更改端口等,但没有任何作用。

这是我的代码,试图将数据包发送到服务器

try {
                    //packet sending to vps to check key
                    Socket socket = new Socket("78.141.211.150",792);



                    // I/O
                    BufferedReader in_socket = new BufferedReader (new InputStreamReader (socket.getInputStream()));
                    PrintWriter out_socket = new PrintWriter (new OutputStreamWriter (socket.getOutputStream()), true);

                    out_socket.println("key " + text);
                    String message = in_socket.readLine();
                    if(message == "valid") {
                        // key is valid
                        socket.close();
                    } else if(message == "invalid") {
                        // key is invalid
                        socket.close();
                    }
                    } catch(Exception e) {
                        e.printStackTrace();
                        label.setText("Something went wrong.");
                    }


                }
                if(tries == 5) { 
                    System.exit(0);

                }
            }
        });

这是接收数据包的服务器L


        ServerSocket server_socket = new ServerSocket(792); //opening a new port
        System.out.println("Port 792 is open.");

        Socket socket = server_socket.accept();
        System.out.println("Client " + socket.getInetAddress() + " has connected.");

        // I/O buffers:
        BufferedReader in_socket = new BufferedReader(new InputStreamReader (socket.getInputStream()));
        PrintWriter out_socket = new PrintWriter(new OutputStreamWriter (socket.getOutputStream()), true);

        String packet = in_socket.readLine();
        if(packet.startsWith("key ")) {
            String keyin = packet.substring(4);

            if(key.contains(keyin)) {
                out_socket.println("valid");
                System.out.println("Client " + socket.getInetAddress() + "has put in a valid key.");
                online = online + 1;
                socket.close();
            } else {
                out_socket.println("invalid");
                System.out.println("Client " + socket.getInetAddress() + "has put in an invalid key.");
                socket.close();
            }

        }

0 个答案:

没有答案