break; } }
else if(s.startsWith(\聊天内容\
{ String message=s.substring(s.indexOf(\
Enumeration chatPersonList=peopleList.elements(); while(chatPersonList.hasMoreElements())
{ ((Server_thread)chatPersonList.nextElement()).out.writeUTF(\聊天内容:\
message);
} } }
catch(IOException ee)
{ Enumeration chatPersonList=peopleList.elements(); while(chatPersonList.hasMoreElements()) { try
{ Server_thread th=(Server_thread)chatPersonList.nextElement(); if(th!=this&&th.isAlive())
{ th.out.writeUTF(\客户离线:\ } }
catch(IOException eee){} }
peopleList.remove(name); try { socket.close();
} catch(IOException eee){}
System.out.println(name+\客户离开了\ break; } } } }
3.BroadCastWord.java
import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*;
import javax.swing.Timer;
public class BroadCastWord extends Frame implements ActionListener { int port;
InetAddress group=null; MulticastSocket socket=null; Timer time=null; FileDialog open=null;
Button select,开始广播,停止广播; File file=null;
String FileDir=null,fileName=null; FileReader in=null; BufferedReader bufferIn=null; int token=0;
TextArea 显示正在播放内容,显示已播放的内容; public BroadCastWord() { super(\单词广播系统\
select=new Button(\选择要广播的文件\ 开始广播=new Button(\开始广播\ 停止广播=new Button(\停止广播\ select.addActionListener(this); 开始广播.addActionListener(this); 停止广播.addActionListener(this); time=new Timer(2000,this);
open=new FileDialog(this,\选择要广播的文件\ 显示正在播放内容=new TextArea(10,10); 显示正在播放内容.setForeground(Color.blue); 显示已播放的内容=new TextArea(10,10); Panel north=new Panel(); north.add(select); 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(); try
{ port=5000; group=InetAddress.getByName(\ socket=new MulticastSocket(port); socket.setTimeToLive(1); socket.joinGroup(group); }
catch(Exception e)
{ System.out.println(\ }
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()==select)
{显示已播放的内容.setText(null); open.setVisible(true); fileName=open.getFile(); FileDir=open.getDirectory(); if(fileName!=null) { time.stop();
file=new File(FileDir,fileName); try
{ file=new File(FileDir,fileName);
in=new FileReader(file); bufferIn=new BufferedReader(in); }
catch(IOException ee) { } } }
else if(e.getSource()==开始广播) { time.start(); }
else if(e.getSource()==time) { String s=null;
} });
try { if(token==-1)
{ file=new File(FileDir,fileName);
in=new FileReader(file); bufferIn=new BufferedReader(in); }
s=bufferIn.readLine(); if(s!=null) { token=0;
显示正在播放内容.setText(\正在广播的内容:\\n\ 显示已播放的内容.append(s+\ DatagramPacket packet=null; byte data[]=s.getBytes();
packet=new DatagramPacket(data,data.length,group,port); socket.send(packet); } else
{ token=-1; } }
catch(IOException ee) { } }
else if(e.getSource()==停止广播) { time.stop(); } }
public static void main(String[] args)
{ BroadCastWord broad=new BroadCastWord(); } }
Receive.java
import java.net.*; import java.awt.*; import java.awt.event.*;
public 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=5000; try{ group=InetAddress.getByName(\ socket=new MulticastSocket(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)
} });