资源简介
局域网广播,含有客户端和服务器端。
计算机网络。
代码片段和文件信息
package client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.*;
public class Receive extends Jframe implements RunnableActionListener{
private static final long serialVersionUID = 1L;
JMenuBar menu=new JMenuBar();
JMenu m1=new JMenu(“开始“);
JMenu m2=new JMenu(“帮助“);
JMenuItem itemStart=new JMenuItem(“开始收听“);
JMenuItem itemStop=new JMenuItem(“停止收听“);
JMenuItem exit=new JMenuItem(“退出“);
JMenuItem help=new JMenuItem(“关于“);
JTextArea info=new JTextArea(3020);
JButton start=new JButton(“开始收听“);
JButton stop=new JButton(“停止收听“);
JPanel northPane=new JPanel();
JScrollPane centerPane=new JScrollPane(info);
Container con=this.getContentPane();
String line=““;
private static final int PORT=2012;
InetAddress group;
MulticastSocket socket;
boolean ting=false;
Thread thread;
public Receive(){
thread=new Thread(this);
con.setLayout(new BorderLayout());
con.add(northPaneBorderLayout.NORTH);
con.add(centerPaneBorderLayout.CENTER);
menu.add(m1);
menu.add(m2);
m1.add(itemStart);
m1.add(itemStop);
m1.add(exit);
m2.add(help);
northPane.add(new JLabel(“收听广播“));
northPane.add(start);
northPane.add(stop);
info.setEditable(false);
exit.addActionListener(this);
help.addActionListener(this);
start.addActionListener(this);
itemStart.addActionListener(this);
stop.addActionListener(this);
itemStop.addActionListener(this);
validate();
try{
group=InetAddress.getByName(“224.1.0.0“);
socket=new MulticastSocket(PORT);
socket.joinGroup(group);
}
catch(Exception e){
}
setSize(470 500);
setJMenuBar(menu);
setResizable(false);
this.setLocation(400100);
settitle(“局域网广播“);
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit){
this.dispose();
}
if(e.getSource()==help){
String me=“局域网广播“;
JOptionPane.showMessageDialog(thisme);
}
if(e.getSource()==start){
System.out.println(“&&&“);
if(!(thread.isAlive())){
thread=new Thread(this);
}
try{
thread.start();
ting =false;
}catch(Exception ee){
String info=“接收异常“;
JOptionPane.showMessageDialog(null info “提示 “JOptionPane.INFORMATION_MESSAGE);
return;
}
}
if(e.getSource()==stop){
System.out.println(“$$$$“);
thread.interrupt();
ting=true;
}
}
public void run() {
info.setText(“接收的内容:\n“);
while(true){
byte data[]=new byte[8192];
System.out.println (“xxxxx“);
DatagramPacket packet=new DatagramPacket(datadata.lengthgroupPORT);
try{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3526 2009-12-11 22:04 client\Receive.java
文件 4957 2009-12-11 22:46 server\BroadCast.java
目录 0 2009-12-08 22:05 client
目录 0 2009-12-08 22:06 server
----------- --------- ---------- ----- ----
8483 4
- 上一篇:struts2 论坛系统
- 下一篇:rpmrebuild工具包亲测可用
评论
共有 条评论