您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > IICverilog
多点广播原理:若要使用多点广播时,则需要让一个数据报标有一组目标主机地址,当数据报发出后,整个组的所有主机都能收到该数据报。IP多点广播(或多点发送)实现了将单一信息发送到多个接收者的广播,其思想是设置一组特殊网络地址作为多点广播地址,每一个多点广播地址都被看做一个组,当客户端需要发送、接收广播信息时,加入到该组即可。定义Weather类packagecom.han;importjava.net.*;publicclassWeatherextendsThread{Stringweather=节目预告:八点有大型晚会,请收听;intport=9898;//定义端口号InetAddressiaddress;//没法初始化,这里只能声明。因为初始化new对象时要抛出异常所以在成员变量区域是语法通不过的。MulticastSocketsocket;//DatagramSocket只允许数据报发送给指定的目标地址,而MulticastSocket可以将数据报以广播方式发送到数量不等的多个客户端。//在构造方法中初始化成员变量Weather(){try{iaddress=InetAddress.getByName(233.0.0.0);//初始ipsocket=newMulticastSocket(port);//根据端口号创建广播socketsocket.setTimeToLive(1);//设置socket生存周期socket.joinGroup(iaddress);//将该地址加入广播地址组}catch(Exceptione){e.printStackTrace();}}@Override//最简单的方法也就是建立一个线程来运行publicvoidrun(){while(true){byte[]data=weather.getBytes();//获取weather字节流//构造数据报包,用来将长度为length偏移量为offset的包发送到指定主机上的指定端口号。length参数必须小于等于data.length。DatagramPacketpacket=newDatagramPacket(data,data.length,iaddress,port);System.out.println(newString(data));try{socket.send(packet);//发送数据包sleep(3000);//线程休眠3秒}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}publicstaticvoidmain(String[]args){Weatherw=newWeather();w.start();}}定义Receive类[java]packagecom.han;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.event.*;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.InetAddress;importjava.net.MulticastSocket;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;publicclassReceiveextendsJFrameimplementsRunnable,ActionListener{privatestaticfinallongserialVersionUID=3362377947503474102L;intport=9898;//指定端口号InetAddressgroup;//初始多播地址组MulticastSocketsocket;//创建socket接收和发送数据包DatagramPacketpacket;//定义数据包JButtonince=newJButton(开始接收);//接收按钮JButtonstop=newJButton(停止接收);//停止按钮JTextAreainceAr=newJTextArea(10,20);JTextAreainced=newJTextArea(10,20);Threadthread;/创建新线程booleanb=false;byte[]buf=newbyte[30];.//在构造方法中设置具体参数特性,也就是初始化publicReceive(){super(广播数据报);thread=newThread(this);ince.addActionListener(this);//绑定按钮事件stop.addActionListener(this);inceAr.setForeground(Color.blue);JPanelnorth=newJPanel();north.add(ince);north.add(stop);add(north,BorderLayout.NORTH);JPanelcenter=newJPanel();JScrollPanesp=newJScrollPane(inceAr);JScrollPanesp2=newJScrollPane(inced);inceAr.setLineWrap(true);inceAr.setEditable(false);inced.setLineWrap(true);inced.setEditable(false);center.add(sp);center.add(sp2);add(center,BorderLayout.CENTER);pack();//JFrameinheritedfromWindowvalidate();setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);try{socket=newMulticastSocket(port);group=InetAddress.getByName(233.0.0.0);socket.joinGroup(group);packet=newDatagramPacket(buf,buf.length);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}@Overridepublicvoidrun(){while(true){try{socket.receive(packet);//socket接收数据包}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}Stringmessage=newString(packet.getData(),0,packet.getLength());//取出数据包中的内容//System.out.println(message.length());inceAr.setText(正在接受的内容:\n+message);inced.append(message+\n);if(b==true){break;}}}//自定义按钮点击事件publicvoidactionPerformed(ActionEvente){Objectobject=e.getSource();if(object==ince){//Ifthisthreadisalreadyonthestaterun,donothing.if(!thread.isAlive()){thread=newThread(this);thread.start();ince.setBackground(Color.red);stop.setBackground(Color.yellow);b=false;}}if(object==stop){ince.setBackground(Color.yellow);stop.setBackground(Color.red);b=true;}}publicstaticvoidmain(String[]args){@SuppressWarnings(unused)Receiverec=newReceive();//rec.setSize(460,200);//提供了额外设置窗体大小的方式}}程序截图:
本文标题:IICverilog
链接地址:https://www.777doc.com/doc-2878564 .html