title: Why can't I send packets between client and server using UDP
body: <p>I have a problem with communication between client and server using UDP protocol in Java. I made Client and Server classes and two classes SingleCom responsible for sending and receiving packets.
  I tried to test the connection (I run client on one computer and server on another; both create DatagramSocket on the same port) simply by sending "START" string from client to server and after that server should print the string on the console and send back "ACCEPTED", but either client nor server print anything on its console. Could anyone tell me what is going on and why?
  Below I show simplified classes (I separated server and client in other packets):</p>
  
  <pre><code>//Client:
  public class Client
  {
      protected static InetAddress serverAddress;
      protected static InetAddress clientAdr;
      protected static int port;
      protected static SingleCom connection;
      public static final int PACKET_SIZE = 1024;
  
      public static void main(String[] args) throws IOException, InterruptedException
      {
          //get server address and port typed from console
          load();
  
          connection = new SingleCom();
          connection.initilize();
          Thread privcastTread = new Thread(connection);
          privcastTread.start();
  
          //sending the simplest packet
          connection.sendPacket("START", serverAddress);
  
      }
  
      public static synchronized void handlePacket(DatagramPacket packet, String msg) throws IOException
      {
          if (msg.startsWith("ACCEPTED"))
          {
              System.out.println("Accepted");
          }
  
      }
  
  }
  
  //SingleCom for Client
  public class SingleCom implements Runnable
  {
      DatagramSocket udpSocket;
      @Override
      public void run()
      {
          try
          {
              byte[] buffer = new byte[8192];
              DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  
              while(true)
              {
                  udpSocket.receive(packet);
                  String msg = new String(buffer, 0, packet.getLength());
                  Client.handlePacket(packet, msg);
                  packet.setLength(buffer.length);
              }
          }
          catch (SocketException e)
          {
              System.exit(1);
          }
          catch (IOException e)
          {
              System.exit(1);
          }
      }
  
      public void initilize() throws IOException
      {
          udpSocket = new DatagramSocket(Client.port, InetAddress.getLocalHost());
      }
  
      public void sendPacket(String text, InetAddress target) throws IOException
      {
          byte[] sendedMsg = text.getBytes();
          DatagramPacket sendedPacket = new DatagramPacket(sendedMsg, sendedMsg.length, target, Client.port);
          udpSocket.send(sendedPacket);
      }
  
      public void sendPacket(String text) throws IOException
      {
          sendPacket(text, Client.serverAddress)
      }
  
      public void sendPacket(byte[] content) throws IOException
      {
          //byte[] sendedMsg = text.getBytes();
          DatagramPacket sendedPacket = new DatagramPacket(content, content.length, Client.serverAddress, Client.port);
          udpSocket.send(sendedPacket);
      }
  }
  
  //Server
  public class Server
  {
      protected static InetAddress serverAdr;
      protected static InetAddress clientAdr;
      protected static SingleCom connection;
      protected static int port;
      protected static int speed; //in KB/s
  
      private static FileOutputStream fos;
  
      public static void main(String[] args) throws IOException
      {
          load();
  
          connection = new SingleCom();
          connection.initilize();
          Thread privcastTread = new Thread(connection);
          privcastTread.start();
      }
  
      public static synchronized void handlePacket(DatagramPacket pakiet, String msg) throws IOException
      {
          String[] pieces;
          if (msg.startsWith("START"))
          {
              System.out.println(pieces[0]);
              connection.sendPacket("ACCEPTED", clientAdr);
          }
      }
  
  }
  
  //connection for Server
  public class ServerCom implements Runnable
  {
      DatagramSocket udpSocket;
      @Override
      public void run()
      {
          try
          {
              byte[] buffer = new byte[8192];
              DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  
              while(true)
              {
                  udpSocket.receive(packet);
                  String msg = new String(buffer, 0, packet.getLength());
                  Server.handlePacket(packet, msg);
                  packet.setLength(buffer.length);
              }
          }
          catch (SocketException e)
          {
              System.exit(1);
          }
          catch (IOException e)
          {
              System.exit(1);
          }
      }
  
      public void initilize() throws IOException
      {
          udpSocket = new DatagramSocket(Server.port, InetAddress.getLocalHost());
      }
  
      public void sendPacket(String text, InetAddress target) throws IOException
      {
          byte[] sendedMsg = text.getBytes();
          DatagramPacket sendedPacket = new DatagramPacket(sendedMsg, sendedMsg.length, target, Server.port);
          udpSocket.send(sendedPacket);
      }
  
      public void sendPacket(String text) throws IOException
      {
          sendPacket(text, Server.clientAdr);
      }
  
  }
  </code></pre>