title: Why does my UDP connection stop working?
body: <p><strong>After creating an UDP connection and connecting from the client to the server, it stops working randomly.</strong> Why is this happening?</p>
  
  <p>This doesn't usually happen when running the client and the server on the same computer using "localhost" as the IP, but when using different computers on the same network it happens.</p>
  
  <p>When I try and connect using different computers at first it works but after some time it just stops; the connection is "terminated".
  Also, ignore the <code>game.player</code> stuff, it is just a player of mine.</p>
  
  <p>This is my code:</p>
  
  <p>Client:</p>
  
  <pre><code>public class GameClient extends Thread {
    private InetAddress ipAddress;
    private DatagramSocket socket;
    private Main game;
  
    public GameClient(Main main, String ipAddress) {
      this.game = main;
      try {
        this.socket = new DatagramSocket();
        this.ipAddress = InetAddress.getByName(ipAddress);
      } catch (SocketException e) {
        e.printStackTrace();
      } catch (UnknownHostException e) {
        e.printStackTrace();
      }
    }
  
    public void run() {
      while (true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        try {
          socket.receive(packet);
        } catch (IOException e) {
          e.printStackTrace();
        }
        String message = new String(packet.getData());
        message = message.trim();
        if (message.startsWith("00")) {
          System.out.println("Player connected. Got server response...");
          String msg = "01" + game.player.getPos();
          sendData(msg.getBytes());
        }
        if (message.startsWith("01")) {
          message = message.substring(2);
          List&lt;String&gt; coords = Arrays.asList(message.split(","));
          game.updateMP(coords);
          String msg = "01" + game.player.getPos();
          sendData(msg.getBytes());
        }
  
      }
    }
  
    public void sendData(byte[] data) {
      DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress,
          1331);
      try {
        socket.send(packet);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>My server class:</p>
  
  <pre><code>public class GameServer extends Thread {
    private DatagramSocket socket;
    private Main game;
  
    public GameServer(Main main) {
      this.game = main;
      try {
        this.socket = new DatagramSocket(1331);
      } catch (SocketException e) {
        e.printStackTrace();
      }
    }
  
    public void run() {
      while (true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        try {
          socket.receive(packet);
        } catch (IOException e) {
          e.printStackTrace();
        }
        String message = new String(packet.getData());
        message = message.trim();
        if (message.startsWith("00")) {
          message = message.substring(2);
          game.playerConnected = true;
          sendData("00".getBytes(), packet.getAddress(), packet.getPort());
        }
        if (message.startsWith("01")) {
          message = message.substring(2);
          List&lt;String&gt; coords = Arrays.asList(message.split(","));
          game.updateMP(coords);
          String msg = "01" + game.player.getPos();
          sendData(msg.getBytes(), packet.getAddress(), packet.getPort());
        }
  
      }
    }
  
    public void sendData(byte[] data, InetAddress ipAddress, int port) {
      DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress,
          port);
      System.out.println(ipAddress + ", " + port);
      try {
        socket.send(packet);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  
  }
  </code></pre>
  
  <p>And my "Main" class:
  (just a part of it):</p>
  
  <pre><code>if (JOptionPane.showConfirmDialog(null, "Do you want to run the server?") == 0) {
    socketServer = new GameServer(this);
    socketServer.start();
    socketType = 0;
  } else {
    socketClient = new GameClient(this, JOptionPane.showInputDialog(null, "IP:"));
    socketClient.start();
    socketClient.sendData("00".getBytes());
    socketType = 1;
  }
  </code></pre>