为何广播程序不能真正广播?请教高手!
本人用的是Win2K+Jcreator Pro:
本机IP: 10.3.8.132
client:
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 start,end;
TextArea displayInfoing,displayInfoed;
Thread thread;
boolean flag=false;
//constuctor function
public Receive(){
super("Receive Information Timer");
thread=new Thread(this);
start=new Button("Strart Re");
end=new Button("End Re");
start.addActionListener(this);
end.addActionListener(this);
displayInfoing=new TextArea(10,10);
displayInfoing.setForeground(Color.blue);
displayInfoed=new TextArea(10,10);
Panel north=new Panel();
north.add(start);
north.add(end);
add(north,BorderLayout.NORTH);
Panel center=new Panel();
center.setLayout(new GridLayout(1,2));
center.add(displayInfoing);
center.add(displayInfoed);
add(center,BorderLayout.CENTER);
validate();
port=5000;
try{
group=InetAddress.getByName("10.3.0.0");
}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()==start) {
start.setBackground(Color.blue);
end.setBackground(Color.gray);
if(!(thread.isAlive())){
thread=new Thread(this);
}
try{
thread.start();
flag=false;
}
catch(Exception ee){
}
}
if(e.getSource()==end) {
start.setBackground(Color.gray);
end.setBackground(Color.blue);
flag=false;
thread.interrupt();
}
}
//*******************************************************************************
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());
displayInfoing.setText("Receiving the Word is:\n"+message);
displayInfoed.setText(message+"\n");
}catch(Exception e){
}
if(flag==true){
break;
}
}
}
//*******************************************************************************
public static void main(String args[]){
new Receive();
}
}
server:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.net.*;
public class BroadCastWord extends Frame implements ActionListener{
int port;
MulticastSocket socket=null;
InetAddress group=null;
Timer time=null;
FileDialog open=null;
Button select,start,end;
File file=null;
FileReader in=null;
BufferedReader bufferIn=null;
String FileDir=null,fileName=null;
int token=0;
TextArea displayInfoing,displayInfoed;
public BroadCastWord(){
super("Word Broad Server") ;
select=new Button("Select File");
start=new Button("Strat BC");
end=new Button("End BC");
start.setEnabled(false);
select.addActionListener(this);
start.addActionListener(this);
end.addActionListener(this);
time=new Timer(1000,this);
open=new FileDialog(this,"Select File",FileDialog.LOAD);
displayInfoing=new TextArea(10,10);
displayInfoing.setBackground(Color.blue);
displayInfoed=new TextArea(10,10);
Panel north=new Panel();
north.add(select);
north.add(start);
north.add(end);
add(north,BorderLayout.NORTH);
Panel center=new Panel();
center.setLayout(new GridLayout(1,2));
center.add(displayInfoing);
center.add(displayInfoed);
add(center,BorderLayout.CENTER);
validate();
try{
port=5000;
group=InetAddress.getByName("10.3.0.0");
socket=new MulticastSocket(port);
socket.setTimeToLive(1);
socket.joinGroup(group);
}
catch(Exception ee){
System.out.println("Error"+ee);
}
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) {
displayInfoed.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);
start.setEnabled(true);
}
catch(Exception ex){
}
}
}
else if(e.getSource()==start) {
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;
displayInfoing.setText("Borasd the:\n"+s);
displayInfoed.append(s+"\n");
DatagramPacket packet=null;
byte data[]=s.getBytes();
packet=new DatagramPacket(data,data.length,group,port);
socket.send(packet);
}
else{
token=-1;
}
}
catch(IOException ioE){
}
}
else if(e.getSource()==end){
time.stop();
}
}
public static void main(String args[]){
BroadCastWord broad=new BroadCastWord();
}
}
请高手指点!