JLChen
2021-12-10 04122050f9b2bf5612d26f8394cc2ead03b57258
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java
@@ -5,6 +5,7 @@
import com.hdl.sdk.common.HDLSdk;
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.SocketBoot;
import com.hdl.sdk.socket.SocketOptions;
@@ -18,6 +19,7 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -37,7 +39,7 @@
    private final byte[] receiveByte;
    private final String ip;
//    private final String ip;
    private final int port;
@@ -53,16 +55,10 @@
     */
    private UdpClient(String ip, int port, int monitorPort, int sendPort, SocketOptions socketOptions) {
        this.socketOptions = socketOptions;
        this.ip = ip;
        this.socketOptions.setIp(ip);
        this.port = port;
        this.sendPort = sendPort;
        this.monitorPort = monitorPort;
        this.receiveByte = new byte[BUFFER];
    }
    public UdpClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
        this.receiveByte = new byte[BUFFER];
    }
@@ -124,7 +120,7 @@
            return;
        }
        try {
            mSocket.receive(receivePacket);
           mSocket.receive(receivePacket);
        } catch (IOException e) {
            e.printStackTrace();
            isConnect.set(false);
@@ -134,21 +130,21 @@
        }
        //排除自己发出去的
        try {
            if (receivePacket.getAddress().getHostAddress()
                    .equals(IpUtils.getIP(HDLSdk.getInstance().getContext()))) {
            if (IpUtils.isLocalIpAddress( receivePacket.getAddress().getHostAddress()))
                return;
            }
        } catch (Exception ignored) {
        }
        IHandleMessage handleMessage = getOptions().getHandleMessage();
        if (handleMessage != null) {
            handleMessage.read(receivePacket.getData());
            byte[] data = new byte[receivePacket.getLength()];
            System.arraycopy(receivePacket.getData(), 0, data, 0, data.length);
            handleMessage.read(data);
        }
        final String receive = new String(receivePacket.getData(), 0, receivePacket.getLength());
        Log.d("---->", receive + " from " + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
        LogUtils.d("---->", receive + " from " + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
        //重置长度
        if (receivePacket != null) {
@@ -161,7 +157,8 @@
        if (msg == null) {
            msg = new byte[1];
        }
        InetAddress serverAddress = InetAddress.getByName(ip);
        InetAddress serverAddress = InetAddress.getByName(getOptions().getIp());
        LogUtils.e("发送目标IP地址:"+getOptions().getIp());
        final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, serverAddress, port);
        if (sendPort < 0) {
            final DatagramSocket sendSocket = new DatagramSocket();