title: Corrupt UDP Packet java
body: <p>Sender</p>
  
  <pre><code>package core;
  
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.SocketException;
  import java.net.UnknownHostException;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import message.JOutMessages;
  
  /**
   * 
   * @author padaboo I.B Aleksandrov jetananas@yandex.ru
   */
  public class JUDPSender extends Thread{
      /**
       * run stop flag
       */
      public static boolean isRunning = true;
      /**
       * pool size
       */
      public int poolSize;
      /**
       * out passage
       */
      public JOutMessages message;
      /**
       * client port
       */
      public int clientPort;
  
      public JUDPSender(int poolSize,int clientPort){
          this.poolSize    = poolSize;
          this.clientPort  = clientPort;
      }
  
      @Override
      public void run() {
          try{
              ExecutorService executor   =  Executors.newFixedThreadPool(poolSize);
              DatagramSocket socket      =  new DatagramSocket();
  
              DatagramPacket packet;
              byte[] buffer;
              InetAddress inetAddress;
              JUDPSenderHandler handler;
  
              while(JUDPSender.isRunning){
  
                  for (String key : JOutMessages.getKeySet()) {
  
                      message     =  (JOutMessages) JOutMessages.get(key);
  
                      buffer      = message.json.toJSONString().getBytes();
  
                      //System.out.println(message.json.toJSONString());
  
                      inetAddress = InetAddress.getByName(message.ip);
  
                      packet = new DatagramPacket(buffer, buffer.length, inetAddress, this.clientPort);
  
                      handler = new JUDPSenderHandler(socket,packet);
                      executor.execute(handler);
  
                  } 
              }
          } catch ( SocketException ex) {
              Logger.getLogger(JUDPSender.class.getName()).log(Level.SEVERE, null, ex);
          } catch (UnknownHostException ex){
  
          }
      }
  
      class JUDPSenderHandler implements Runnable{
  
          /**
           * datagram socket
           */
          DatagramSocket socket;
          /**
           * datagram packet
           */
          DatagramPacket packet;
  
          public JUDPSenderHandler(DatagramSocket socket,DatagramPacket packet) {
              this.socket = socket;
              this.packet = packet;
          }
  
          @Override
          public void run() {
              try {
  
                  socket.send(packet);
  
              } catch (IOException ex) {
                  Logger.getLogger(JUDPSender.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      }
  }
  </code></pre>
  
  <p>Recivier</p>
  
  <pre><code>/*
   * jsock framework https://github.com/nnpa/jsock open source
   * Each line should be prefixed with  * 
   */
  
  package jsock.core;
  
  import conf.JConfig;
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import jsock.message.JInMessages;
  
  /**
   * 
   * @author padaboo I.B Aleksandrov jetananas@yandex.ru
   */
  public class JUDPRecivier extends Thread{
  
      /**
       * server port
       */
      private final int port;
      /**
       * pool size
       */
      private final int poolSize;
      /**
       * run stop flag
       */
      public static boolean isRunning = true;
  
      public JUDPRecivier(int poolSize,int port){
          this.port     = port;
          this.poolSize = poolSize;
      }
  
      /**
       * Create thread poll 
       */
      @Override
      public void run() {
          try (DatagramSocket serverSocket = new DatagramSocket(port)) {
  
              ExecutorService executor     = Executors.newFixedThreadPool(poolSize);
  
              byte[] receiveData           = new byte[JConfig.socket_buffer_size];
  
  
              while(JUDPRecivier.isRunning){
                  DatagramPacket receivePacket = new DatagramPacket ( receiveData, receiveData.length );
                  serverSocket.receive ( receivePacket );
  
                  executor.execute(
                      new JUDPRecivierHandler(serverSocket,receivePacket)
                  );
              }
              serverSocket.close();
          } catch (IOException ex) {
              Logger.getLogger(JTCPReciver.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
  
      /**
       * Listen user sockets 
       * send messages to message list
       * send connection to connection list
       */
      class JUDPRecivierHandler implements Runnable{
          //socket
          DatagramSocket socket;
  
          DatagramPacket receivePacket;
  
          public JUDPRecivierHandler(DatagramSocket serverSocket,DatagramPacket receivePacket) {
              this.socket        = serverSocket;
              this.receivePacket = receivePacket;
          }
  
          @Override
          public void run() {
              InetAddress inetAdress    = receivePacket.getAddress();
              String ip = inetAdress.getHostAddress();
  
              byte[] data = new byte[receivePacket.getLength()];
  
              data  = receivePacket.getData();
  
              String stringData = new String(data,0, receivePacket.getLength());
  
              JInMessages inputSocketMessage = new JInMessages(ip,stringData);
              inputSocketMessage.insert();
          }
      }
  }
  </code></pre>
  
  <p>Send packet</p>
  
  <blockquote>
    <p>{"task":"JGameTask","data":{"isDirectRight":false,"Left":false,"blockMove":false,"fallSpeed":100,"LeftRelease":false,"LClickRelease":false,"DownRelease":false,"Right":false,"SitRelease":false,"camLeftZ":0.08650045,"camLeftY":-4.0163286E-9,"camLeftX":-0.18032658,"speed":1.0,"LClick":false,"camRotationW":-0.22177358,"camRotationX":-2.1658896E-4,"JumpRelease":false,"startLocation":{"x":-67.11953,"y":8.058952,"z":-55.18904},"camRotationY":0.9750977,"camRotationZ":-9.522566E-4,"levelId":"Gilkph036QbP","isRightDirect":false,"isSitRight":false,"Down":false,"camDirectionZ":-0.2704894,"RightRelease":false,"isDirectLeft":false,"camDirectionX":-0.12975043,"camDirectionY":-5.859462E-4,"CtrlRelease":false,"isSit":false,"isSitLeft":false,"Ctrl":false,"Jump":false,"isAnimRun":false,"isLeftDirect":false,"command":1,"rotateAnimation":"direct","isSitRun":false,"runAnimation":"run","gravity":60,"UpRelease":false,"Up":false,"Sit":false,"isStand":false},"user_id":"18","scenario":"send_keys","auth_token":"c0facde0aa585dc4b7e0a6d820bb3df41499141302"}</p>
  </blockquote>
  
  <p>Receive with error:  at end <strong>2"}</strong></p>
  
  <blockquote>
    <p>{"task":"JGameTask","data":{"isDirectRight":false,"Left":false,"blockMove":false,"fallSpeed":100,"LeftRelease":false,"LClickRelease":false,"DownRelease":false,"Right":false,"SitRelease":false,"camLeftZ":0.08650045,"camLeftY":-4.0163286E-9,"camLeftX":-0.18032658,"speed":1.0,"LClick":false,"camRotationW":-0.22177358,"camRotationX":-2.1658896E-4,"JumpRelease":false,"startLocation":{"x":-67.11953,"y":8.058952,"z":-55.18904},"camRotationY":0.9750977,"camRotationZ":-9.522566E-4**,**"levelId":"Gilkph036QbP","isRightDirect":false,"isSitRight":false,"Down":false,"camDirectionZ":-0.2704894,"RightRelease":false,"isDirectLeft":false,"camDirectionX":-0.12975043,"camDirectionY":-5.859462E-4,"CtrlRelease":false,"isSit":false,"isSitLeft":false,"Ctrl":false,"Jump":false,"isAnimRun":false,"isLeftDirect":false,"command":1,"rotateAnimation":"direct","isSitRun":false,"runAnimation":"run","gravity":60,"UpRelease":false,"Up":false,"Sit":false,"isStand":false},"user_id":"18","scenario":"send_keys","auth_token":"c0facde0aa585dc4b7e0a6d820bb3df41499141302"}2"}</p>
  </blockquote>
  
  <p>Where is the mistake?</p>