Pregunta

I'm making a simple server_client application using the Datagramsocket and DatagramPacket. what I want to do is: one client sends data to the server and the server sends these data to another client . The problem is server recieves the data from the first client but do not send them to the other client , and how can I know the port of the client that I will send to ?? does not the port changes ??

this is the client class :

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

the server class:

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

}

¿Fue útil?

Solución

Here is what I am thinking -- I did not run your code and verify.

From your code, it looks like the client socket is NOT bound to any port -- I am assuming that your code is used for the other client as well. Basically, if a UDP server (receiver) needs to receive a packet (datagram), then it must be bound to a port and the UDP client (sender) must send the packet on this port. For the send side of the client you do this correctly when you build a packet as "DatagramPacket(buf, buf.length, address, 8000);".

When the server receives this packet, it sends the packet to the localhost ("127.0.0.1") but you dont seem to be specifying what is the port number for this outgoing packet. So, you would need to do two things. First, bind the client to another port (let us say 9000) and then make the destination port of the packet 9000 and send it.

Btw, if you have only one sender and reciever, then you can use the the port number from the received packet "receivePacket.getPort()" to reflect the response back to the sender. But, since you have two clients, you cannot use this method. So, you are left with the case of making sure that the second client also binds to the port.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top