[英]How to get UDP server to respond to a UDP client packet?
I'm having trouble getting the server to send data back to the client. 我无法让服务器将数据发送回客户端。 Here is my code and my inputs and results.
这是我的代码以及我的输入和结果。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPServer {
public static void main(String[] args) throws IOException {
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 1345;
//Creates connection socket.
DatagramSocket serverSocket = new DatagramSocket(port);
System.out.println("Server Active");
while(true) {
//Receiving packet
byte[] buffer = new byte[100];
DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
serverSocket.receive(receivePacket);
String clientInput = new String(buffer);
System.out.println("Server has Received : " + clientInput);
//Sending packet
byte[] data = clientInput.getBytes();
DatagramPacket sendPack = new DatagramPacket(data, data.length, ip, port);
serverSocket.send(sendPack);
String serverInput = new String(data);
System.out.println("Server has sent: " + serverInput);
}
}
}
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
public class UDPClient
{
public static void main(String args[]) throws IOException
{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress ip = InetAddress.getByName("127.0.0.1");
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
int port = 1345;
System.out.println("Would you like to continue?");
while(inFromUser.readLine().equals("Yes") || inFromUser.readLine().equals("yes")) {
System.out.println("Enter an expression to be evaluated");
//gets userInput
String userInput = inFromUser.readLine();
byte[] buffer = userInput.getBytes();
//sends packet to server
DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length, ip, port);
clientSocket.send(sendPacket);
System.out.println("Client has sent: " + userInput);
//receives packet from server
byte[] data = new byte[100];
DatagramPacket receivePacket = new DatagramPacket(data, data.length);
clientSocket.receive(receivePacket);
String serverInput = new String(data);
System.out.println("Client has received: " + serverInput);
System.out.println("Would you like to continue?");
}
}
}
I am not sure why the server does not echo back to the client? 我不确定服务器为什么不回显客户端? I'm not sure what I am doing wrong, or about the overall quality of this code.
我不确定自己在做什么错,也不知道这段代码的整体质量。
The server is sending the reply to itself. 服务器正在将回复发送给自己。 When you create the
sendPacket
in the server, you're giving it ip
and port
but the ip
and port
are the server's own IP and bound (listening) port, not the port of your client. 在服务器中创建
sendPacket
时,将为其提供ip
和port
但ip
和port
是服务器自身的IP和绑定(监听)端口,而不是客户端的端口。
Because the client created its DatagramSocket
without specifying a port, the system will dynamically choose an unused port number for it. 由于客户端创建其
DatagramSocket
未指定端口,因此系统将为其动态选择一个未使用的端口号。 You need to obtain the client's port number with receivePacket.getSocketAddress()
(in the server), then use that address in your construction of sendPacket
. 您需要使用
receivePacket.getSocketAddress()
(在服务器中)获取客户端的端口号,然后在构造sendPacket
使用该地址。 In fact, there is an alternate constructor for DatagramPacket
that accepts buffer, length and SocketAddress
which is ideal for constructing the reply packet. 实际上,
DatagramPacket
有一个备用构造函数,它接受缓冲区,长度和SocketAddress
,这对于构造应答数据包是理想的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.