⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 broadcast.java

📁 网络编程中关于组播的一段代码 希望对大家有用
💻 JAVA
字号:

import java.net.*;
public class BroadCast extends Thread                                      
{   String s="天气预报,最高温度32度,最低温度25度"; 
    int port=5858;                                    //组播的端口 
    InetAddress group=null;                          //组播组的地址
    MulticastSocket socket=null;                     //组播套接字  
   BroadCast() 
   {try 
      {
    group=InetAddress.getByName("239.255.8.0");  //设置组播组的地址为239.255.8.0
    socket=new MulticastSocket(port);        //组播套接字将在port端口广播
    socket.setTimeToLive(1);               //组播套接字发送数据报范围为本地网络
    socket.joinGroup(group);            //加入广播组,加入group后,socket发送的数据报
      }                                  //加入到组播组
    catch(Exception e)
      { System.out.println("Error: "+ e);          
      }
   }
   public void run()
   { while(true) 
      { try{  DatagramPacket packet=null;               //待播的数据包
              byte data[]=s.getBytes(); 
              packet=new DatagramPacket(data,data.length,group,port); 
              System.out.println(new String(data)); 
              socket.send(packet);                     //播出数据包
              sleep(200);
           }
        catch(Exception e)
           {  System.out.println("Error: "+ e);          
           }
      }
   }
   public static void main(String args[])
   {  new BroadCast().start();
   }
}

Receive.java 
import java.net.*;
import java.awt.*; 
import java.awt.event.*;
class Receive extends Frame implements Runnable,ActionListener
{ int port;                                        //组播的端口. 
  InetAddress group=null;                          //组播组的地址.
  MulticastSocket socket=null;                     //组播套接字. 
  Button 开始接收,停止接收;   
  TextArea 显示正在接收内容,显示已接收的内容;  
  Thread thread;                                   //负责接收信息的线程.
  boolean 停止=false;
  public Receive()
   { super("定时接收信息");
     thread=new Thread(this);
     开始接收=new Button("开始接收");
     停止接收=new Button("停止接收");
     停止接收.addActionListener(this); 
     开始接收.addActionListener(this); 
     显示正在接收内容=new TextArea(10,10);
     显示正在接收内容.setForeground(Color.blue); 
     显示已接收的内容=new TextArea(10,10);
     Panel north=new Panel();
     north.add(开始接收);
     north.add(停止接收);
     add(north,BorderLayout.NORTH);
     Panel center=new Panel();
     center.setLayout(new GridLayout(1,2)); 
     center.add(显示正在接收内容);
     center.add(显示已接收的内容);
     add(center,BorderLayout.CENTER);
     validate();
     port=5858;                                      //设置组播组的监听端口
     try{
     group=InetAddress.getByName("239.255.8.0");  //设置组播组的地址为239.255.8.0
     socket=new MulticastSocket(port);          //将在port端口组播
     socket.joinGroup(group);        //加入组播组
        }                
   catch(Exception e){} 
   setBounds(100,50,360,380);   
   setVisible(true);
   addWindowListener(new WindowAdapter()
                     { public void windowClosing(WindowEvent e)
                       { System.exit(0);
                       }
                     });
                            
   }
  public void actionPerformed(ActionEvent e)
   {  if(e.getSource()==开始接收)
      { 开始接收.setBackground(Color.blue);
        停止接收.setBackground(Color.gray);
        if(!(thread.isAlive()))
           {  thread=new Thread(this);
           }
        try{  thread.start(); 
              停止=false;        
           }
        catch(Exception ee) {}
      }
    if(e.getSource()==停止接收)
      { 开始接收.setBackground(Color.gray);
        停止接收.setBackground(Color.blue);
        停止=true; 
      }
   }
   public void run()
   {  while(true)   
      {byte data[]=new byte[8192];
       DatagramPacket packet=null;
       packet=new DatagramPacket(data,data.length,group,port);  //待接收的数据包。
       try { socket.receive(packet);
             String message=new String(packet.getData(),0,packet.getLength());
             显示正在接收内容.setText("正在接收的内容:\n"+message);
             显示已接收的内容.append(message+"\n");
           }
      catch(Exception e) {}
      if(停止==true)
           {  break;
           } 
      } 
   }
   public static void main(String args[])
   { new Receive();
   }
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -