File was renamed from HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/UdpClient.java |
| | |
| | | package com.hdl.sdk.socket.client;
|
| | | package com.hdl.sdk.link.socket.client;
|
| | |
|
| | | import com.hdl.sdk.common.utils.IpUtils;
|
| | | import com.hdl.sdk.common.utils.LogUtils;
|
| | | import com.hdl.sdk.common.utils.TextUtils;
|
| | | import com.hdl.sdk.socket.SocketBoot;
|
| | | import com.hdl.sdk.socket.SocketOptions;
|
| | | import com.hdl.sdk.socket.SocketPool;
|
| | | import com.hdl.sdk.socket.udp.UdpSocketBoot;
|
| | | import com.hdl.sdk.socket.udp.UdpSocketOptions;
|
| | | import com.hdl.sdk.socket.codec.IHandleMessage;
|
| | |
|
| | |
|
| | | import com.hdl.sdk.link.common.utils.LogUtils;
|
| | | import com.hdl.sdk.link.common.utils.TextUtils;
|
| | | import com.hdl.sdk.link.socket.SocketPool;
|
| | | import com.hdl.sdk.link.socket.bean.Packet;
|
| | | import com.hdl.sdk.link.socket.udp.UdpSocketBoot;
|
| | | import com.hdl.sdk.link.socket.udp.UdpSocketOptions;
|
| | | import com.hdl.sdk.link.socket.codec.IHandleMessage;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.net.DatagramPacket;
|
| | | import java.net.DatagramSocket;
|
| | | import java.net.Inet4Address;
|
| | | import java.net.InetAddress;
|
| | | import java.net.InetSocketAddress;
|
| | | import java.net.SocketAddress;
|
| | | import java.util.concurrent.atomic.AtomicBoolean;
|
| | |
|
| | |
|
| | | /**
|
| | |
| | | * @return
|
| | | */
|
| | | public static UdpSocketBoot init(int port, UdpSocketOptions options) {
|
| | | return init(null, port, options);
|
| | | return init("0.0.0.0", port, options);
|
| | | }
|
| | |
|
| | |
|
| | | @Override
|
| | | public void bind() throws Exception {
|
| | | public synchronized void bind() throws Exception {
|
| | | try {
|
| | | //已经绑定过就不用再绑定
|
| | | if (null != mSocket) {
|
| | | return;
|
| | | }
|
| | |
|
| | | if (TextUtils.isEmpty(ipAddress)) {
|
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
|
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
|
| | | }
|
| | | else{
|
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(Inet4Address.getByName(ipAddress), port));
|
| | | }
|
| | | mSocket.setBroadcast(true);
|
| | | mSocket.setReuseAddress(true);
|
| | | // mSocket.setReuseAddress(true);
|
| | |
|
| | | } catch (Exception e) {
|
| | | LogUtils.e("初始化Socket 失败:" + e.getMessage());
|
| | | throw e;
|
| | | }
|
| | | }
|
| | |
| | | }
|
| | | try {
|
| | | mSocket.receive(receivePacket);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | } catch (Exception e) {
|
| | | LogUtils.e("接收到Udp数据包异常,异常信息:" + e.getMessage());
|
| | | return;
|
| | | }
|
| | | if (receivePacket.getLength() == 0) {
|
| | | LogUtils.i("接收到Udp数据包,数据包长度为0:" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
|
| | | return;
|
| | | }
|
| | | //排除自己发出去的
|
| | | try {
|
| | | if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
|
| | | return;
|
| | | } catch (Exception ignored) {
|
| | |
|
| | | }
|
| | | // try {
|
| | | // if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
|
| | | // return;
|
| | | // } catch (Exception ignored) {
|
| | | //
|
| | | // }
|
| | |
|
| | | try {
|
| | | LogUtils.i("接收到Udp数据包,网络地址:" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
|
| | |
| | | if (handleMessage != null) {
|
| | | byte[] data = new byte[receivePacket.getLength()];
|
| | | System.arraycopy(receivePacket.getData(), 0, data, 0, data.length);
|
| | | handleMessage.read(data,receivePacket.getAddress().getHostAddress());
|
| | | handleMessage.read(new Packet(receivePacket.getAddress().getHostAddress(), data,null));
|
| | | }
|
| | |
|
| | | } catch (Exception e) {
|
| | |
|
| | | LogUtils.i("接收到Udp数据包,处理异常:" + e.getMessage());
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | return;
|
| | | }
|
| | | final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, InetAddress.getByName(ipAddress), port);
|
| | | mSocket.send(sendPacket);
|
| | | if (mSocket != null) {
|
| | | mSocket.send(sendPacket);
|
| | | } else {
|
| | | DatagramSocket datagramSocket = new DatagramSocket();
|
| | | datagramSocket.send(sendPacket);
|
| | | datagramSocket.close();
|
| | | }
|
| | | }
|
| | | }
|