java udp 广播
原文链接: http://blog.csdn.net/yudajun/article/details/8477149
udp 是一种网络通信协议,不需要客户端和服务器端建立连接即可进行通讯功能。相对于Tcp协议它有着tcp用很多优点,例如广播功能。udp的广播功能可以指定特定网段进行广播内容,而无需知道接收者是谁,只有接受者在广播范围内即可接收广播内容。其实基于这个功能可以实现一个局域网群聊室的功能。
udp广播发送有两种形式,
方式一:通过DatagramSocket实现
方式二:通过MulticastSocket 实现
方式一:demo;
1,数据发送端:
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- public class SendIP {
- public static void main(String args[]) {
- new SendIP().lanchApp();
- }
- private void lanchApp(){
- SendThread th=new SendThread();
- th.start();
- }
- private class SendThread extends Thread{
- @Override
- public void run() {
- while(true){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- try {
- BroadcastIP();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private void BroadcastIP()throws Exception{
- DatagramSocket dgSocket=new DatagramSocket();
- byte b[]="你好,这是我发给你的消息".getBytes();
- DatagramPacket dgPacket=new DatagramPacket(b,b.length,InetAddress.getByName("255.255.255.255"),8989);
- dgSocket.send(dgPacket);
- dgSocket.close();
- System.out.println("send message is ok.");
- }
- }
- }
2,数据接收端:
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class SearchIP{
- public static void main(String args[])throws Exception{
- new SearchIP().lanchApp();
- }
- private void lanchApp(){
- receiveThread th=new receiveThread();
- th.start();
- }
- private class receiveThread extends Thread{
- @Override
- public void run() {
- while(true){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- try {
- receiveIP();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private void receiveIP() throws Exception{
- DatagramSocket dgSocket=new DatagramSocket(8989);
- byte[] by=new byte[1024];
- DatagramPacket packet=new DatagramPacket(by,by.length);
- dgSocket.receive(packet);
- String str=new String(packet.getData(),0,packet.getLength());
- System.out.println("接收到数据大小:"+str.length());
- System.out.println("接收到的数据为:"+str);
- dgSocket.close();
- System.out.println("recevied message is ok.");
- }
- }
- }
方式二demo;
1,数据发送端:
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.InetAddress;
- import java.net.MulticastSocket;
- public class MulBroadcast extends Thread{
- String info ="节目预告: 恭喜您中500w彩票了";
- int port =9898;
- InetAddress address;
- MulticastSocket socket;
- public MulBroadcast(){
- try{
- address=InetAddress.getByName("233.0.0.0");
- socket=new MulticastSocket(port);
- socket.setTimeToLive(1);
- socket.joinGroup(address);
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- @Override //最简单的方法也就是建立一个线程来运行
- public void run(){
- while(true){
- byte[] data=info.getBytes();
- DatagramPacket packet=new DatagramPacket(data,data.length,address,port);
- try {
- socket.send(packet);
- Thread.sleep(3000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("消息已发送:");
- }
- }
- public static void main(String[] args){
- new MulBroadcast().start();
- }
- }
2,数据接收端:
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.InetAddress;
- import java.net.MulticastSocket;
- public class MulReceiverIP extends Thread{
- int port=9898;
- InetAddress group;
- MulticastSocket socket; //socket sends and receives the packet.
- DatagramPacket packet;
- byte[] buf=new byte[30];// If the message is longer than the packet's length, the message is truncated.
- public MulReceiverIP(){
- try {
- socket=new MulticastSocket(port);
- group=InetAddress.getByName("233.0.0.0");
- socket.joinGroup(group); //加入广播组,加入group后,socket发送的数据报,可以被加入到group中的成员接收到。
- packet=new DatagramPacket(buf,buf.length);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void run(){
- while(true){
- try {
- socket.receive(packet);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // String message=new String(buf);
- String message=new String(packet.getData(),0,packet.getLength());//very important !!
- System.out.println("接受消息内容: "+message);
- }
- }
- public static void main(String[] args) {
- new MulReceiverIP().start();
- }
- }