2

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

}

user2752385
  • 83
  • 10

1 Answers1

0

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.

Manoj Pandey
  • 4,528
  • 1
  • 17
  • 18
  • @user2752385 can you please upload the new code as well (please do not remove the earlier code). I would be happy to take a look and see if something else is missing. – Manoj Pandey Sep 14 '13 at 16:50
  • can I ask few question ? is it possible to send from one client to another like this ? I'm trying since 2 days but the program hangs and comes with no response when the server sends to the other client – user2752385 Sep 14 '13 at 17:14
  • From your latest code, I still see that the client socket is not bound: "socket = new DatagramSocket();". Let us do this step by step. Are you able to send data to the server first? Once we figure that out, we shoudl look at sending the response from the server back to the client. – Manoj Pandey Sep 15 '13 at 09:07
  • yes,the client is able to send data to the server. I knew what is the problem but do not know how to solve it . The problem is how do the client knows that there is data sent to it ?? – user2752385 Sep 15 '13 at 11:23
  • for example , the sever contains the while loop to notify that there is data sending . but how do I do this in the client side ?? – user2752385 Sep 15 '13 at 11:25