udp协议有单播、组播、广播的形式。
接收端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// 服务台。接收端 public class MainService { public static void main(String[] args) { int port_target = 9000; // 本机电话号码-本地绑定的端口号 byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data, data.length); // 收件包,可持续使用 try { /* 组播接收端 */ //MulticastSocket service=new MulticastSocket(port_target); //// socket.joinGroup(InetAddress.getByName("224.0.1.1")); // 方法1。加入一个组播 //service.joinGroup( // new InetSocketAddress(InetAddress.getByName("224.0.1.1"),port_target), // NetworkInterface.getByInetAddress(InetAddress.getLocalHost())); // 方法2 /* 如果发送端是广播形式,这里会收到 */ /* 如果发送端是单播形式,那么这个接收端的ip须是发送端的目标ip才能收到 */ InetAddress local_address = InetAddress.getLocalHost(); DatagramSocket service = new DatagramSocket(port_target); // 绑定9000端口在本地作为接收号码 while (true) { // 持续服务 System.out.println("客服等待消息,阻塞..."); service.receive(packet); // 等待接收,阻塞 String client_ip = packet.getAddress().getHostAddress(); // 获取发送端的IP地址对象 int client_port = packet.getPort(); int length = packet.getLength(); // 获取接收到的字节个数 String msg = new String(data, 0, length); System.out.println("顾客消息:" + msg + ",顾客房号 " + client_ip + ":" + client_port); // Thread.sleep(2000); byte buf[]; buf = ("这里是服务台,你的房号是 " + client_port + ",马上处理。").getBytes(); if ("bye".equals(msg) || (msg.contains("总部") && msg.contains("下班吧"))) { buf = "exit".getBytes(); } DatagramPacket pkt = new DatagramPacket(buf, buf.length, local_address, client_port); // 发回给 client_port service.send(pkt); // 发送,无阻塞 System.out.println("回复了"); if (msg.contains("总部") && msg.contains("下班吧")){ service.close(); break; } } System.out.println("服务台下班了"); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /*catch (InterruptedException e) { e.printStackTrace(); }*/ } } |
发送端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
// 顾客。发送端 public class MainClient { public static void main(String[] args){ String user = args[0]; int target_port = 9000; // 目标端口,对方的号码。本地的号码是随机的 byte buf[] = ("我是" + user).getBytes(); try { InetAddress target_address = InetAddress.getLocalHost(); // 单播。当前发送端发给指定IP和端口的1个接收端。本例目标接收端的ip是localhost即和发送端相同的ip:本机地址。 DatagramPacket pkt_client = new DatagramPacket(buf, buf.length, target_address, target_port); // 广播。当前发送端发给"一批"接收端,这些接收端都是同一个端口。广播地址固定使用"255.255.255.255" // pkt_client=new DatagramPacket(buf, buf.length, InetAddress.getByName("255.255.255.255"), target_port); // 组播。当前发送端发给"一批"接收端,接收端必须绑定该组IP。组ip可用范围:224.0.0.0(不含) ~ 239.255.255.255(含)。 // pkt_client=new DatagramPacket(buf, buf.length, InetAddress.getByName("224.0.1.1"),target_port); DatagramSocket client = new DatagramSocket(); client.send(pkt_client); while (true) { byte[] data = new byte[1024]; DatagramPacket p_service = new DatagramPacket(data, data.length); System.out.println("顾客等待,阻塞..."); client.receive(p_service); String ip_service = p_service.getAddress().getHostAddress(); // 获取发送端的IP地址对象 int port_service = p_service.getPort(); int length = p_service.getLength(); // 获取接收到的字节个数 String msg = new String(data, 0, length); System.out.println("服务台消息:" + msg + ",对方地址 " + ip_service + ":" + port_service); if ("exit".equals(msg)){ client.close(); break; } buf = "bye".getBytes(); if (user.contains("总部")){ buf = "我是总部,下班吧".getBytes(); } pkt_client = new DatagramPacket(buf, buf.length, target_address, target_port); client.send(pkt_client); System.out.println("回复了"); } System.out.println("客户端关闭"); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/4139.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设