| | |
| | |
|
| | | 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;
|
| | |
| | | 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;
|
| | |
|
| | |
| | |
|
| | | private final byte[] receiveByte;
|
| | |
|
| | | private final String ip;
|
| | | // private final String ip;
|
| | |
|
| | | private final int port;
|
| | |
|
| | |
| | | */
|
| | | 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];
|
| | | }
|
| | |
|
| | |
| | | return;
|
| | | }
|
| | | try {
|
| | | mSocket.receive(receivePacket);
|
| | | mSocket.receive(receivePacket);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | isConnect.set(false);
|
| | |
| | | }
|
| | | //排除自己发出去的
|
| | | 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) {
|
| | |
| | | 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();
|