javatcptcpclienttcpserver

cannot run simple tcp server/client connection on Mac?


I wrote a simple tcp server/client connection that client passes username and pwd to server to get verification. I set the ip address to 127.0.0.1 and port to 8080 on both server and client. The code works on a Windows machine but cannot finish the username and pwd verification on Mac. Here is the server.java code:

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();
        }
    }
}

and here is the client's code:

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();
        }

    }
}

the client just kept getting failed to login message even though I put correct username and password as the inputs.


Solution

  • The issue is with this line in the client code:

    if("ok".equals(len)){
    

    This should be checking if ("ok".equals(new String(bytes,0,len)), since len is the length of the received bytes. Fixing this line should resolve the issue.