我正在使用Datagramsocket和DatagramPacket创建一个简单的server_client应用程序。 我想要做的是:一个客户端将数据发送到服务器,服务器将这些数据发送到另一个客户端。问题是服务器接收来自第一个客户端的数据,但不将它们发送到另一个客户端,我怎么知道我将发送给客户端的端口?端口没有变化吗?
这是客户端类:
public class DatagramClient extends JFrame {
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DatagramSocket socket;
private byte[] buf = new byte[256];
private InetAddress address;
private DatagramPacket sendPacket;
private DatagramPacket receivePacket;
public static void main(String[] args) {
new DatagramClient();
}
public DatagramClient() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter radius"), BorderLayout.WEST);
p.add(jtf, BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
setLayout(new BorderLayout());
add(p, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
jtf.addActionListener(new ButtonListener()); // Register listener
setTitle("DatagramClient");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
socket = new DatagramSocket();
address = InetAddress.getByName("localhost");
sendPacket =
new DatagramPacket(buf, buf.length, address, 8000);
receivePacket = new DatagramPacket(buf, buf.length);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Arrays.fill(buf, (byte)0);
sendPacket.setData(jtf.getText().trim().getBytes());
socket.send(sendPacket);
socket.receive(receivePacket);
jta.append("Radius is " + jtf.getText().trim() + "\n");
jta.append("Area received from the server is "
+ Double.parseDouble(new String(buf).trim()) + '\n');
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
服务器类:
public class DatagramServer extends JFrame {
private JTextArea jta = new JTextArea();
private byte[] buf = new byte[256];
public static void main(String[] args) {
new DatagramServer();
}
public DatagramServer() {
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("DatagramServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
DatagramSocket socket = new DatagramSocket(8000);
jta.append("Server started at " + new Date() + '\n');
DatagramPacket receivePacket =
new DatagramPacket(buf, buf.length);
DatagramPacket sendPacket ;
while (true) {
Arrays.fill(buf, (byte)0);
socket.receive(receivePacket);
jta.append("The client host name is "+receivePacket.getAddress().getHostAddress() +
" and port number is " + receivePacket.getPort() + '\n');
jta.append("Radius received from client is " +
new String(buf).trim() + '\n');
double radius = Double.parseDouble(new String(buf).trim());
double area = radius * radius * Math.PI;
jta.append("Area is " + area + '\n');
InetAddress addr=InetAddress.getByName("127.0.0.1");
sendPacket = new DatagramPacket(buf, buf.length);
sendPacket.setAddress(addr);
sendPacket.setData(new Double(area).toString().getBytes());
socket.send(sendPacket);
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
以下是我的想法 - 我没有运行您的代码并验证。
从您的代码看,客户端套接字似乎没有绑定到任何端口 - 我假设您的代码也用于其他客户端。基本上,如果UDP服务器(接收方)需要接收数据包(数据报),则它必须绑定到端口,UDP客户端(发送方)必须在此端口上发送数据包。对于客户端的发送端,当您将数据包构建为“DatagramPacket(buf,buf.length,address,8000);”时,可以正确执行此操作。
当服务器收到此数据包时,它会将数据包发送到localhost(“127.0.0.1”),但您似乎没有指定此传出数据包的端口号。所以,你需要做两件事。首先,将客户端绑定到另一个端口(让我们说9000),然后创建数据包9000的目标端口并发送它。
顺便说一下,如果您只有一个发件人和收件人,那么您可以使用收到的数据包“receivePacket.getPort()”中的端口号将响应反映给发件人。但是,由于您有两个客户端,因此无法使用此方法。因此,您需要确保第二个客户端也绑定到端口。