如何使客户端侦听从服务器发送的数据

时间:2013-09-15 14:53:27

标签: java datagram

我想使用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();
}
} 

1 个答案:

答案 0 :(得分:1)

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

创建一个实现'Runnnable'的类,然后创建一个新的Thread来运行该类。该类的'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);