MulticastSocket的简单使用

我们这里使用MulticastSocket以及组播锁的形式实时监控对应的ip和端口是否实时存在!

下面我们来看代码:

public Thread getListenThread(){
		Thread thread = null;
		try {
			thread = new Thread() {
				private static final String TAG = "ReceiverThread";

				private static final String MULTICAST_IP = "239.255.255.250";
				private static final int MULTICAST_PORT = 1900;
				private WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
				private MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
				private InetAddress groupInetAddress = InetAddress.getByName(MULTICAST_IP);
				private boolean joinedGroup = false;

				@Override
				public void run() {

					while (!this.isInterrupted()) {

						if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {

							if (!joinedGroup)
								try {
									multicastSocket.joinGroup(groupInetAddress);
									wifiManager.createMulticastLock(TAG).acquire();
									joinedGroup = true;
								} catch (IOException ex) {
									Log.i(TAG, "Failed to join Multicast group:" + ex.getMessage());
								}
							try {
								byte[] buffer = new byte[1024];
								DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
								while(true){
									multicastSocket.receive(packet);
									String text = new String(buffer, 0, packet.getLength());;
									//									LogUtil.i(TAG, "Message received: /n" + text+"RECEIVED END");

									Message msg = new Message();
									msg.what = 4;
									Bundle bundle = new Bundle();
									bundle.putString("text", text);
									msg.setData(bundle);
									handlerdishes.sendMessageDelayed(msg,500);
								}
							} catch (SocketTimeoutException e) {
								e.printStackTrace();
							} catch (IOException ex) {
								multicastSocket = null;
								ex.printStackTrace();
							}  
						} else
							joinedGroup = false;
					}
				}
			};
		} catch (IOException e) {
			e.printStackTrace();
		}
		return thread;
	}

如上我们可知是使用线程的方式启动MulticastSocket,activity中启动的代码很简单:

/*
		 * 监听盒子状态信息
		 */
		Thread boxListener_thread  = getListenThread();
		boxListener_thread.start();

同时使用handler处理相对应的事件,就OK了!

需要的权限有如下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值