UDP 客户端/服务器应用程序接受错误字段

问题描述 投票:0回答:1

我正在尝试使两个 Java 应用程序进行通信,但运行它时遇到一些问题。如果我运行程序并输入“Hello”,程序会提示我输入用户名,但会将输入作为密码。这是我的输出的示例:

What is your initial message
Hello
Welcome to Keene State Server, what is your username?
Alice
Incorrect password. Please try again.

我想要的输出是:

What is your initial message
Hello
Welcome to Keene State Server, what is your username?
Alice
User name is OK, what is your password?
123456
Password is OK, you are now connected to the network
import java.io.*;
import java.net.*;

class ClientProcess {
    public static void main(String args[]) throws Exception {
        //Asks user for what their initial message is
        System.out.println("What is your initial message");
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    
        while(true) {
            //create a new client socket and Gets the IP address of the computer on which the other
            //process in running 
            //in this case, it's localhost because both two processes (client and server) are running
            //on the same local host

            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getByName("localhost");
            byte[] sendData = new byte[1024];
            byte[] receiveData = new byte[1024];

            the data message we will send is from the user input

            String username = inFromUser.readLine();
            sendData = username.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,                      
            5000);
            clientSocket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String response = new String(receivePacket.getData()).trim();

            //if the response is good it will ask for the password
            if (response.equals("User name is OK, what is your password?")) {
                System.out.println("Please enter your password:");
                String password = inFromUser.readLine();
                sendData = password.getBytes();
                sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 5000);
                clientSocket.send(sendPacket);

                receivePacket = new DatagramPacket(receiveData, receiveData.length);
                clientSocket.receive(receivePacket);
                response = new String(receivePacket.getData()).trim();
                System.out.println(response);
            } else {
                System.out.println(response);
            }
            clientSocket.close();
        }
    }
}



import java.io.*;
import java.net.*;

class ServerProcess {
    public static void main(String args[]) throws Exception {
        //In the server process, a DatagramSocket object must also be instantiated 
        //and bound to a local port number which is 5000 in this case.  
        DatagramSocket serverSocket = new DatagramSocket(5000);

        while(true) {
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];

            //Receives data messages sent to the server packet
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();

            String username = new String(receivePacket.getData()).trim();
            String response = "";

            //If stetment that takes the input and chooses what to display
            if (username.equals("Hello")) {
                response = "Welcome to the Server, what is your username?";
            } else if (username.equals("Alice")) {
                response = "User name is OK, what is your password?";
            } else {
                response = "Invalid username. Please try again.";
            }

            sendData = response.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);

            receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            IPAddress = receivePacket.getAddress();
            port = receivePacket.getPort();

            String password = new String(receivePacket.getData()).trim();

            if (password.equals("123456")) {
                response = "Password is OK, you are now connected to the network";
            } else {
                response = "Incorrect password. Please try again.";
            }

            sendData = response.getBytes();
            sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
        }
    }
}
java
1个回答
0
投票

答案很简单,如果收到“hello”,则需要重复用户名逻辑。

按照你的

if
逻辑,

            if (username.equals("Hello")) {
                response = "Welcome to the Server, what is your username?";

并发送响应,但发送后您不会再次监听用户名 - 您希望收到的下一个数据报是密码。

© www.soinside.com 2019 - 2024. All rights reserved.