2

我想通过服务器将数据从一个客户端发送到另一个客户端DatagramSocket class。服务器从客户端接收数据,将客户端端口号和名称添加到数组列表中,但不将数据发送到目标客户端。如何让服务器监听服务器发送的数据??不只是在按下任何按钮时接收数据。服务器包含“while循环”来监听客户端,我怎样才能在客户端做到这一点?

客户端类

 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() {

//GUI 

jtf.addActionListener(new ButtonListener()); // Register listener

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];
ArrayList<Integer> clientPort=new ArrayList<Integer>();
ArrayList<String> clientName=new ArrayList<String>();


public static void main(String[] args) {
new DatagramServer();
}

public DatagramServer() {

//GUI
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) {


clientPort.add(receivePacket.getPort);
clientName.add( receivePacket.getAddress().getHostAddress());
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(clientName.get(0));
  sendPacket = new DatagramPacket(buf, buf.length);

sendPacket.setAddress(addr);
sendPacket.setAddress(clientPort.get(0));

sendPacket.setData(new Double(area).toString().getBytes());
socket.send(sendPacket);
}
}
catch(IOException ex) {
 ex.printStackTrace();
}
} 
4

1 回答 1

1

最简单的方法是启动一个可以运行客户端网络循环的新线程。

创建一个实现“Runnnable”的类,然后创建一个新线程来运行该类。该类的“run()”方法应该循环并接收消息。

DatagramSocket是线程安全的,因此您不必担心从两个单独的线程访问它(从 GUI 线程写入,从接收线程读取)。

但是,JTextArea和其他 GUI 组件是不同的。它们只能从 GUI 线程修改。所以你不能从接收线程追加到'JTextArea'。

将数据传递给 GUI 线程(从接收器线程)的最简单方法是这样的。

SwingUtilities.invokeLater(new sendString(client, receivedMessage);

这需要第二个新RunnablesendString。然后Runnable该类的run()方法可以执行更新,这invokeLater将导致它由 GUI 线程运行。 client应该是对您的DatagramClient实例的引用。

client.jta.append(receivedMessage);
于 2013-09-15T19:09:35.297 回答