损坏的UDP数据包java

时间:2017-07-04 04:28:03

标签: java udp

发件人

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);
            }
        }
    }
}

Recivier

/*
 * 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();
        }
    }
}

发送数据包

  

{ “任务”: “JGameTask”, “数据”:{ “isDirectRight”:假, “左”:假 “blockMove”:假 “fallSpeed”:100, “LeftRelease”:假 “LClickRelease” :假 “DownRelease”:假, “右”:假 “SitRelease”:假 “camLeftZ”:0.08650045, “camLeftY”: - 4.0163286E-9, “camLeftX”: - 0.18032658, “速度”:1.0, “L单击”:假 “camRotationW”: - 0.22177358, “camRotationX”: - 2.1658896E-4, “JumpRelease”:假 “startLocation”:{ “×”: - 67.11953, “Y”:8.058952, “z” 的:-55.18904}, “camRotationY”:0.9750977, “camRotationZ”: - 9.522566E-4, “levelId”: “Gilkph036QbP”, “isRightDirect”:假的, “isSitRight”:假的, “下”:假的, “camDirectionZ” :-0.2704894, “RightRelease”:假的, “isDirectLeft”:假的, “camDirectionX”: - 0.12975043, “camDirectionY”: - 5.859462E-4, “CtrlRelease”:假的, “isSit”:假的, “isSitLeft”:假的, “Ctrl” 键:假, “跳转”:假 “isAnimRun”:假 “isLeftDirect”:假, “命令”:1, “rotateAnimation”: “直接”, “isSitRun”:假 “runAnimation”:”跑”, “万有引力”:60, “UpRelease”:假, “向上”:假的, “坐”:假的, “isStand”:假}, “USER_ID”: “18”, “情景”: “send_keys” “AUTH_TOKEN” : “c0facde0aa585dc4b7e0a6d820bb3df41499141302”}

收到错误:结束 2“}

  

{ “任务”: “JGameTask”, “数据”:{ “isDirectRight”:假, “左”:假 “blockMove”:假 “fallSpeed”:100, “LeftRelease”:假 “LClickRelease” :假 “DownRelease”:假, “右”:假 “SitRelease”:假 “camLeftZ”:0.08650045, “camLeftY”: - 4.0163286E-9, “camLeftX”: - 0.18032658, “速度”:1.0, “L单击”:假 “camRotationW”: - 0.22177358, “camRotationX”: - 2.1658896E-4, “JumpRelease”:假 “startLocation”:{ “×”: - 67.11953, “Y”:8.058952, “z” 的:-55.18904}, “camRotationY”:0.9750977, “camRotationZ”: - 9.522566E-4 **,** “levelId”: “Gilkph036QbP”, “isRightDirect”:假的, “isSitRight”:假的, “下”:假的“camDirectionZ”: - 0.2704894, “RightRelease”:假的, “isDirectLeft”:假的, “camDirectionX”: - 0.12975043, “camDirectionY”: - 5.859462E-4, “CtrlRelease”:假的, “isSit”:假” isSitLeft “:假,” Ctrl “键:假,” 跳转 “:假” isAnimRun “:假” isLeftDirect “:假,” 命令 “:1,” rotateAnimation “:” 直接 “ ”isSitRun“:假” runAnimation “:” 跑”, “万有引力”:60, “UpRelease”:假, “向上”:假的, “坐”:假的, “isStand”:假}, “USER_ID”: “18”, “情景”: “send_keys”,“auth_to县 “:” c0facde0aa585dc4b7e0a6d820bb3df41499141302 “} 2”}

错误在哪里?

1 个答案:

答案 0 :(得分:0)

固定 而(JUDPRecivier.isRunning){                 receiveData = new byte [JConfig.socket_buffer_size];

            DatagramPacket receivePacket = new DatagramPacket ( receiveData, receiveData.length );
            serverSocket.receive ( receivePacket );

            executor.execute(
                new JUDPRecivierHandler(serverSocket,receivePacket)
            );

            receiveData = null;
        }