【问题标题】:cannot run simple tcp server/client connection on Mac?无法在 Mac 上运行简单的 tcp 服务器/客户端连接?
【发布时间】:2023-01-30 19:00:32
【问题描述】:

我写了一个简单的 tcp 服务器/客户端连接,客户端将用户名和密码传递给服务器以进行验证。我在服务器和客户端上将 ip 地址设置为 127.0.0.1 并将端口设置为 8080。该代码在 Windows 机器上运行,但无法在 Mac 上完成用户名和密码验证。这是 server.java 代码:

public class TcpServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("starting the server...");
        while(true){
            Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        InputStream inputStream = socket.getInputStream();
                        byte[] bytes = new byte[1024];
                        int len=inputStream.read(bytes);
                        String text= new String(bytes,0,len);
                        String[] split = text.split("&");
                        String username = split[0].split("=")[1];
                        String pwd = split[1].split("=")[1];
                        OutputStream outputStream = socket.getOutputStream();
                        if(("mayikt").equals(username)&&"123456".equals(pwd)){
                            outputStream.write("ok".getBytes(StandardCharsets.UTF_8));
                        }else{
                            outputStream.write("failed".getBytes(StandardCharsets.UTF_8));
                        }
                        inputStream.close();
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

这是客户的代码:

public class TcpClient {
    public static void main(String[] args) throws IOException {
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("Please enter username: ");
            String username = scanner.nextLine();
            System.out.println("Please enter password: ");
            String pwd = scanner.nextLine();
            Socket socket = new Socket("127.0.0.1", 8080);
            OutputStream outputStream = socket.getOutputStream();
            String text ="userName="+username+"&userPwd="+pwd;
            outputStream.write(text.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            if("ok".equals(len)){
                System.out.println("login successfully");
            }else{
                System.out.println("failed to login");
            }
            outputStream.close();
            socket.close();
        }

    }
}

即使我输入了正确的用户名和密码,客户端仍然不断收到登录失败消息。

【问题讨论】:

    标签: java tcp tcpclient tcpserver


    【解决方案1】:

    问题出在客户端代码中的这一行:

    如果(“确定”。等于(len)){

    这应该检查是否“ok”.equals(new String(bytes,0,len)),因为 len 是接收字节的长度。修复此行应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2017-02-24
      • 2015-08-21
      • 1970-01-01
      • 2021-08-12
      • 2020-06-03
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多