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; } }