zoukankan      html  css  js  c++  java
  • Scoket 局域网广播收发

    Scoket 局域网广播收发

    public class SocketGroup {
        public static final int MSG_SEND_SUCCESS = 0;
        public static final int MSG_SEND_FAILE = 1;
        public static final int MSG_RECEIVE_SUCCESS = 2;
        public static final int MSG_RECEIVE_FAILE = 3;
        private  String ip; // 服务端ip
        private static int BROADCAST_PORT = 3535;
        private static String BROADCAST_IP = "224.0.0.1";
        private static String BROADCAST_IP_RE = "192.168.8.0";
        private InetAddress inetAddress = null;
        /* 发送广播端的socket */
        private MulticastSocket multicastSocket = null;
        private Handler handlerSocket;
        private int TIME_SEND = 10*1000;
        private int TIME_RECEIVE = 10*1000;
        private boolean isStopReceive = false;
        private boolean isStopSend = false;
        private Thread threadReciveGroup;
        private Thread threadSendGroup;
    
        public SocketGroup(Handler handler) {
            handlerSocket = handler;
        }
    
        public void joinGroup() {
            try {
                inetAddress = InetAddress.getByName(BROADCAST_IP);
                if(multicastSocket==null){
                    multicastSocket = new MulticastSocket(BROADCAST_PORT);
                    multicastSocket.setTimeToLive(1);
                    multicastSocket.joinGroup(inetAddress);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void leaveGroup() {
            if(multicastSocket!=null){
                try {
                    multicastSocket.leaveGroup(inetAddress);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public void startSendGroupMsg(String message){
            isStopSend = false;
            threadSendGroup = new Thread(new runSendGroupMsg(message));
            joinGroup();
            threadSendGroup.start();
        }
    
        private void sendGroupMsg(String message) {
            try {
                while (!isStopSend) {
                    isStopSend = true;
                    DatagramPacket dataPacket = null;
                    // 将本机的IP(这里可以写动态获取的IP)地址放到数据包里,其实server端接收到数据包后也能获取到发包方的IP的
                    byte[] data = message.getBytes();
                    dataPacket = new DatagramPacket(data, data.length, inetAddress, BROADCAST_PORT);
                    multicastSocket.send(dataPacket);
                    handlerSocket.sendEmptyMessage(MSG_SEND_SUCCESS);
                    Thread.sleep(TIME_SEND);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void startReciveGroupMsg() {
            isStopReceive = false;
            threadReciveGroup = new Thread(runReceiveGroupMsg);
            joinGroup();
            threadReciveGroup.start();
        }
        
        class runSendGroupMsg implements Runnable{
            private String message= "";
            public runSendGroupMsg(String msg) {
                message = msg;
            }
            
            @Override
            public void run() {
                sendGroupMsg(message);
                Log.d("", "------>stop");
            }
        }
    
        Runnable runReceiveGroupMsg = new Runnable() {
    
            @Override
            public void run() {
                reciveGroupMsg();
            }
        };
        
        private void reciveGroupMsg() {
            try {
                byte buf[] = new byte[1024];
                DatagramPacket dp = null;
                dp = new DatagramPacket(buf, buf.length, inetAddress, BROADCAST_PORT);
                Message msg = null;
                while (!isStopReceive) {
                    msg = new Message();
                    msg.what = MSG_RECEIVE_SUCCESS;
                    multicastSocket.receive(dp);
                    ip = new String(buf, 0, dp.getLength());
                    msg.obj = ip;
                    handlerSocket.sendMessage(msg);
                    System.out.println("检测到服务端IP : " + ip);
                    //Thread.sleep(TIME_RECEIVE);
                }
                Log.d("","----------->stop receive");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public void stopReceiveGroupMsg(){
            isStopReceive = true;
        }
        
        public void stopSendGroupMsg(){
            isStopReceive = true;
        }
        
    }
  • 相关阅读:
    [C#][Newtonsoft.Json] Newtonsoft.Json 序列化时的一些其它用法
    [C#] 获取计算机内部信息
    [Bug] 解决 Sql Server 数据库死锁问题
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    [svn] TortoiseSVN 图文操作
    [js] 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
    Redis 小白指南(三)- 事务、过期、消息通知、管道和优化内存空间
    Redis 小白指南(二)- 聊聊五大类型:字符串、散列、列表、集合和有序集合
    Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍
    Redis 小白指南(四)- 数据的持久化保存(草稿)
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/4482155.html
Copyright © 2011-2022 走看看