使用UDP中的DatagramPacket

huangapple go评论75阅读模式
英文:

DatagramPacket Using in UDP

问题

以下是翻译好的内容:

代码:

服务器端:

public static void main(String[] args) {
    System.out.println("This is Server");
    DatagramSocket server = null;
    byte[] container = new byte[1024];
    try {
        server = new DatagramSocket(9998);
    } catch (SocketException e) {
        e.printStackTrace();
    }
    DatagramPacket dPacket = new DatagramPacket(container, container.length);	
    try {
        while(true) {				
            server.receive(dPacket);
            byte[] datas = dPacket.getData();
            String temp = new String(datas);
            if (temp.equals("Hello")) {
                System.out.println("Hi~! Nice to meet you!");
            } else if (temp.equals("How are you?")) {
                System.out.println("I am good!");
            } else {
                System.out.println("Have a good day");
            }
            if (temp.equals("exit")) {
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    server.close();
}

客户端:

public static void main(String[] args) {
    System.out.println("This is Client");
    try {
        DatagramSocket client = new DatagramSocket(8888);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        while(!input.equals("exit")) {
            input = br.readLine();
            byte[] msg = input.getBytes();
            DatagramPacket dPacket = new DatagramPacket(msg, msg.length, new InetSocketAddress("localhost", 9998));
            client.send(dPacket);
        }
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

I am trying to use DatagramPacket in Java to communicate with server and client.

My goal is: when the client says: "Hello", the Server should respond with: "Hi, nice to meet you". So I use a if condition to achieve this.

However, when the input is "Hello", it seems like it skips the if condition (I already used the debug). I wonder why this happened? It seems too weird. Can anyone help me with this problem? Any help would be appreciated! Thanks!

Code :

Client:

public static void main(String[] args) {
		System.out.println("This is Server");
		DatagramSocket server = null;
		byte[] container = new byte[1024];
		try {
			server = new DatagramSocket(9998);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		DatagramPacket dPacket = new DatagramPacket(container, container.length);	
		try {
			while(true) {				
				server.receive(dPacket);
				byte[] datas = dPacket.getData();
				String temp = new String(datas);
				if (temp.equals("Hello")) {
					System.out.println("Hi~!Nice to meet you!");
				}else if (temp.equals("How are you?")) {
					System.out.println("I am good!");
				}else {
					System.out.println("Have a good day");
				}
				if (temp.equals("exit")) {
					break;
				}
				
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		server.close();
	}

client:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("This is Client");
		try {
			DatagramSocket client = new DatagramSocket(8888);
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String input = "";
			while(!input.equals("exit")) {
				input = br.readLine();
				byte[] msg = input.getBytes();
				DatagramPacket dPacket = new DatagramPacket(msg, msg.length,new InetSocketAddress("localhost",9998));
				client.send(dPacket);
			}
			
			client.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

答案1

得分: 0

尝试以下操作在接收数据时。

byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());
英文:

Try the following when receiving data.

byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());

huangapple
  • 本文由 发表于 2020年3月16日 18:31:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60704265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定