2

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();
  }
 } 

}

4

1 に答える 1

0

これが私が考えていることです-私はあなたのコードを実行して検証しませんでした。

あなたのコードから、クライアントソケットはどのポートにもバインドされていないように見えます-あなたのコードが他のクライアントにも使用されていると想定しています。基本的に、UDP サーバー (受信者) がパケット (データグラム) を受信する必要がある場合、パケットはポートにバインドされ、UDP クライアント (送信者) はこのポートでパケットを送信する必要があります。クライアントの送信側では、「DatagramPacket(buf, buf.length, address, 8000);」としてパケットを作成するときにこれを正しく行います。

サーバーがこのパケットを受信すると、パケットをローカルホスト (「127.0.0.1」) に送信しますが、この発信パケットのポート番号を指定していないようです。したがって、2 つのことを行う必要があります。まず、クライアントを別のポート (9000 としましょう) にバインドしてから、パケットの宛先ポートを 9000 にして送信します。

ところで、送信側と受信側が 1 つしかない場合は、受信したパケットのポート番号 "receivePacket.getPort()" を使用して、応答を送信側に反映させることができます。ただし、クライアントが 2 つあるため、この方法は使用できません。そのため、2 番目のクライアントもポートにバインドすることを確認する必要があります。

于 2013-09-11T15:56:26.207 に答える