title: Binding error in UDP networking
body: <p>In my Server Client program by UDP my Server receives message, shows that message to the console and from Client and send it to Client again. </p>
  
  <p>Here is my Server, it takes input of its name from command line</p>
  
  <pre><code>import java.io.IOException;
  import java.net.*;
  
  class Stest implements Runnable {
  private String str[];
  DatagramSocket sock;
  DatagramPacket pack;
  String Sname;
  
  Stest(String str[]) throws SocketException, UnsupportedEncodingException, IOException {
      this.str = str;
      this.Sname = str[0];
      System.out.println(Sname);
      new Thread(this).start();
  }
  
  public void run() {
      while (true) {
          byte[] data = new byte[1024];
          try {
              sock = new DatagramSocket(5050);
          } catch (SocketException ex) {
              Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
          }
          pack = new DatagramPacket(data, data.length);
          try {
              sock.receive(pack);
          } catch (IOException e) {
              e.printStackTrace();
          }
          String s = null;
          try {
              s = new String(pack.getData(), "UTF-8");
              System.out.println(s);
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
          DatagramPacket dp = null;
          try {
              dp = new DatagramPacket(s.getBytes("UTF-8"), s.getBytes("UTF-8").length);
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
          dp.setPort(1);
          dp.setAddress(pack.getAddress());
          try {
              sock.send(dp);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  
  }
  
  public class test{
  public static void main(String[] args) {
      try {
         Stest stest= new Stest(args);
  
      } catch (IOException ex) {
          Logger.getLogger(test.class
                  .getName()).log(Level.SEVERE, null, ex);
      }
  }
  }
  </code></pre>
  
  <p>And here is my Client that takes its name, listening port, server ip address and server name and sends message to server and receives that message from Server again and shows that message to the console.  </p>
  
  <pre><code>import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  
  class UDPClient {
  String username;
  String listening_port;
  String serverAddress;
  String servername;
  public UDPClient(String username, String listening_port, String serverAddress, String servername) {
      this.username = username;
      this.listening_port = listening_port;
      this.serverAddress = serverAddress;
      this.servername = servername;
  }
  }
  class ClientListen extends UDPClient implements Runnable {
  Thread t;
  DatagramSocket clientlistensocket;
  DatagramPacket clientlistenpacket;
  byte[] data;
  public ClientListen(String username, String listening_port, String serverAddress, String servername) {
      super(username, listening_port, serverAddress, servername);
      try {
          clientlistensocket = new DatagramSocket(Integer.parseInt(listening_port));
  
      } catch (Exception e) {
          System.out.println(e + "    ClientListen");
      }
      t = new Thread(this, "ClientListen");
      t.start();
  }
  
  public void go() {
      String messag = new String(clientlistenpacket.getData());
      System.out.println(messag);
  }
  
  void processing() {
      data=new byte[1024];
  
      clientlistenpacket = new DatagramPacket(data,data.length);
  
      try {
          clientlistensocket.receive(clientlistenpacket);
          go();
      } catch (Exception e) {
          System.out.println(e + "   ClientlistenProcessing");
      }
  }
  public void run() {
      while (true) {
          try {
              processing();
          } catch (Exception e) {
              System.out.println(e + "Processing+run");
          }
      }
  }
  
  }
  class ClientSpeak extends UDPClient {
  
  DatagramPacket clientspeakpacket;
  DatagramSocket clientspeaksocket;
  InetAddress ipAddress;
  Thread t;
  
  public ClientSpeak(String username, String listening_port, String serverAddress, String servername) {
      super(username, listening_port, serverAddress, servername);
  
      try {
  
          clientspeaksocket = new DatagramSocket();
  
      } catch (Exception e) {
          System.out.println(e + "ClientSpeak");
      }
      try {
  
          String firstmessage = "Via:" + servername + "To:" + servername + "From:" + username + "Port:" + listening_port;
          byte[] firstdata = firstmessage.getBytes();
          ipAddress = InetAddress.getByName(serverAddress);
  
          clientspeakpacket = new DatagramPacket(firstdata, firstdata.length, ipAddress, 5050);
          clientspeaksocket.send(clientspeakpacket);
      } catch (Exception e) {
          System.out.println(e + "Clientspeak");
      }
  }
  
  
  }
  
  public class testC {
  
  public static void main(String[] args) {
      if (args.length == 4) {
          String username = args[0].trim();
          String listening_port = args[1].trim();
          String serverAddress = args[2].trim();
          String servername = args[3].trim();
  
          ClientSpeak clientSpeak = new ClientSpeak(username, listening_port, serverAddress, servername);
          ClientListen clientListen = new ClientListen(username, listening_port, serverAddress, servername);
      }
  }
  }
  </code></pre>
  
  <p>But it is always showing error like:</p>
  
  <pre><code>S
  Via:STo:SFrom:nahPort:1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  Nov 21, 2016 7:20:34 PM Stest run
  SEVERE: null
  java.net.BindException: Address already in use: Cannot bind
  at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
  at    java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:84)
  at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
  at java.net.DatagramSocket.bind(DatagramSocket.java:392)
  at java.net.DatagramSocket.&lt;init&gt;(DatagramSocket.java:242)
  at java.net.DatagramSocket.&lt;init&gt;(DatagramSocket.java:299)
  at java.net.DatagramSocket.&lt;init&gt;(DatagramSocket.java:271)
  at Stest.run(test.java:41)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>What should i do?</p>