| | |
| | | import java.net.DatagramSocket;
|
| | | import java.net.InetAddress;
|
| | | import java.net.InetSocketAddress;
|
| | | import java.net.SocketAddress;
|
| | | import java.util.concurrent.atomic.AtomicBoolean;
|
| | |
|
| | |
|
| | |
| | | return;
|
| | | }
|
| | | if (TextUtils.isEmpty(ipAddress)) {
|
| | | mSocket = SocketPool.getInstance().getUdpSocket1(new InetSocketAddress(port));
|
| | | mSocket = SocketPool.getInstance().getUdpSocket(new InetSocketAddress(port));
|
| | | }
|
| | | mSocket.setBroadcast(true);
|
| | | mSocket.setReuseAddress(true);
|
| | |
| | |
|
| | | @Override
|
| | | public UdpSocketOptions getOptions() {
|
| | | return null;
|
| | | return this.socketOptions;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void onHandleResponse() throws Exception {
|
| | | if (mSocket == null) {
|
| | | return;
|
| | | }
|
| | | try {
|
| | | mSocket.receive(receivePacket);
|
| | | mSocket.receive(receivePacket);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
| | | }
|
| | | //排除自己发出去的
|
| | | try {
|
| | | if (IpUtils.isLocalIpAddress( receivePacket.getAddress().getHostAddress()))
|
| | | if (IpUtils.isLocalIpAddress(receivePacket.getAddress().getHostAddress()))
|
| | | return;
|
| | | } catch (Exception ignored) {
|
| | |
|
| | | }
|
| | |
|
| | | try {
|
| | | LogUtils.i( "接收到Udp数据包,网络地址:" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
|
| | | LogUtils.i("接收到Udp数据包,网络地址:" + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort());
|
| | |
|
| | | IHandleMessage handleMessage = getOptions().getHandleMessage();
|
| | | if (handleMessage != null) {
|
| | | byte[] data = new byte[receivePacket.getLength()];
|
| | | System.arraycopy(receivePacket.getData(), 0, data, 0, data.length);
|
| | | handleMessage.read(data);
|
| | | handleMessage.read(data,receivePacket.getAddress().getHostAddress());
|
| | | }
|
| | |
|
| | | }catch (Exception e){
|
| | | } catch (Exception e) {
|
| | |
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | @Override
|
| | | public void sendMsg(InetSocketAddress inetSocketAddress, byte[] msg) throws Exception {
|
| | | public void sendMsg(String ipAddress,int port, byte[] msg) throws Exception {
|
| | | if (msg == null) {
|
| | | return;
|
| | | }
|
| | | final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, inetSocketAddress);
|
| | | final DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, InetAddress.getByName(ipAddress), port);
|
| | | mSocket.send(sendPacket);
|
| | | }
|
| | | }
|