title: UDP Unicast Communication between three servers using java
body: <p>I want to make three servers communicate using java UDP. If a particular method which exists for all the three servers is called on the first server instance the other two servers should also communicate the result for those methods to the first servers using UDP. Should I be using multicast ? I tried using Unicast but its not working out. </p>
  
  <p>UDPDataExchangeclass</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.SocketException;
  import java.net.SocketTimeoutException;
  import java.net.UnknownHostException;
  
  public class UDPDataExchange {
  
      String              Hostname;
      static InetAddress  IPAddress;
  
      UDPDataExchange() {
          Hostname = new String("127.0.0.1");
          try {
              IPAddress = InetAddress.getByName(Hostname);
          }
          catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void requestData(int portNumber, String data) {
          try {
              DatagramSocket clientSocket = new DatagramSocket();
              byte[] receiveData = new byte[1024];
              DatagramPacket receivePacket = new DatagramPacket(receiveData,
                      receiveData.length);
              byte[] sendData = new byte[1024];
              sendData = data.getBytes();
              System.out.print("Ready to send data ");
              DatagramPacket sendPacket = new DatagramPacket(sendData,
                      sendData.length, IPAddress, portNumber);
              clientSocket.send(sendPacket);
              clientSocket.setSoTimeout(10000);
              try {
                  clientSocket.receive(receivePacket);
                  String modifiedSentence = new String(receivePacket.getData());
  
                  InetAddress returnIPAddress = receivePacket.getAddress();
  
                  int port = receivePacket.getPort();
  
                  System.out.println("From server at: " + returnIPAddress + ":"
                          + port);
                  System.out.println("Message: " + modifiedSentence);
  
              }
              catch (SocketTimeoutException ste) {
                  System.out.println("Timeout Occurred: Packet assumed lost");
              }
              //clientSocket.close();
  
          }
          catch (SocketException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      public void responseData(int portNumber) {
              try {
  
                  DatagramSocket serverSocket = new DatagramSocket(portNumber);
                  byte[] receiveData = new byte[1024];
                  byte[] sendData = new byte[1024];
  
                  while (true) {
  
                      receiveData = new byte[1024];
  
                      DatagramPacket receivePacket = new DatagramPacket(receiveData,
                              receiveData.length);
  
                      System.out.println("Waiting for datagram packet");
  
                      serverSocket.receive(receivePacket);
  
                      String sentence = new String(receivePacket.getData());
  
                      InetAddress IPAddress = receivePacket.getAddress();
  
                      int port = receivePacket.getPort();
  
                      System.out.println("From: " + IPAddress + ":" + port);
                      System.out.println("Message: " + sentence);
  
                      String capitalizedSentence = sentence.toUpperCase();
  
                      sendData = capitalizedSentence.getBytes();
  
                      DatagramPacket sendPacket = new DatagramPacket(sendData,
                              sendData.length, IPAddress, port);
  
                      serverSocket.send(sendPacket);
  
                  }
  
              }
              catch (SocketException ex) {
                  System.out.println("UDP Port 9876 is occupied.");
                  System.exit(1);
              }
              catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
      }
  
  
  }
  </code></pre>
  
  <p>UDPTest</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  class UDPTest extends UDPDataExchange {
  
      public static void main(String args[]) throws Exception {
          int UDPClientConcordiaPortNumber = 9876;
          int UDPClientMcgillPortNumber=9890;
          int UDPClientDawsonPortNumber=9891;
          UDPTest McgillServer=new UDPTest();
          UDPTest DawsonServer=new UDPTest();
          McgillServer.responseData(UDPClientConcordiaPortNumber);
          DawsonServer.responseData(UDPClientDawsonPortNumber);
  
          UDPTest concordiaTest = new UDPTest();
  
          System.out.println("Attemping to connect to " + IPAddress
                  + ") via UDP port" + UDPClientConcordiaPortNumber);
  
          String concordiaData = "Hello from concordia";
          System.out.println("Sending data  " + concordiaData.length()
                  + " bytes to server.");
  
          concordiaTest.requestData(UDPClientConcordiaPortNumber, concordiaData);
  
      }
  }
  </code></pre>
  
  <p>In the UDPDataExchange I've defined two methods to send and receive data. In the UDPTest class I am trying to create two server instances namely mcgill and dawson server who would listen to concordia's port for data sent. Once they receive it they send a response back to Concordia</p>