import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;public class MulticastChat implements Runnable,WindowListener,ActionListener
{

protected Frame frame;
protected TextArea output;
protected TextField input;
protected InetAddress group;
protected int port;
protected Thread listener=null;
protected MulticastSocket socket;
protected MulticastSocket socket1;

protected DatagramPacket outgoing,incoming;
byte [] buffer1 = new byte [256];
byte [] buffer = new byte [256];

public MulticastChat(InetAddress group,int port)
{
this.group=group;
this.port=port;
initAWT();
} protected void initAWT()
{
frame=new Frame("MulticastChat["+group.getHostAddress()+":"+port+"]");
frame.addWindowListener(this);
output=new TextArea();
output.setEditable(false);
input=new TextField();
input.addActionListener(this);
frame.setLayout(new BorderLayout());
frame.add(output,"Center");
frame.add(input,"South");
frame.pack();//使大小正好能容纳控件
}



public synchronized void start1()throws IOException
{
if(listener==null)
{
initNet();
listener=new Thread(this);
listener.start();
frame.setVisible(true);
}
}


protected  void initNet()throws IOException
{
socket=new MulticastSocket ();
socket1=new MulticastSocket(port);
socket.setTimeToLive(1);
                  socket1.setTimeToLive(1);
socket.joinGroup(group);
socket1.joinGroup(group);
outgoing=new DatagramPacket(buffer,buffer.length,group,port);
incoming =new DatagramPacket (buffer1,buffer1.length);
}

public synchronized void stop()throws IOException
{
frame.setVisible(false);
if(listener!=null)
{
listener.interrupt();
listener=null;
try
{
socket.leaveGroup(group);socket1.leaveGroup(group); }
finally
{
socket.close();
socket1.close();
}
}
}

public void windowOpened (WindowEvent event)
{
input.requestFocus();
}

public void windowClosing (WindowEvent event)
{
try
{
stop();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void actionPerformed (ActionEvent event)
{
try
{
byte[]utf=event.getActionCommand().getBytes("UTF8");
outgoing.setData(utf);
outgoing.setLength(utf.length);
socket.send(outgoing);
input.setText(" ");
}
catch (IOException ex)
{
handleIOException (ex);
}
}

protected synchronized void handleIOException (IOException ex)
{
if(listener!=null)
{
output.append(ex+"\n");
input.setVisible(false);
frame.validate();
if(listener!=Thread.currentThread())
{
listener.interrupt();
}
listener=null;
try
{
socket.leaveGroup(group);
socket1.leaveGroup(group);
}
catch(IOException ignored)
{

}
socket.close();
socket1.close();
}
}
public void run()
{
try
{ if(listener!=null)
{
incoming.setLength(incoming.getData().length);
socket1.receive(incoming);
System.out.println(new String(incoming.getData()));
String message=new String(incoming.getData(),0,incoming.getLength(),"UTF8");
output.append(message+"\n");
}
}
catch (IOException ex)
{
handleIOException(ex);
}
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public static void main(String[] args)throws IOException
{
String groupStr = "239.1.2.3";
InetAddress group = InetAddress.getByName (groupStr);//把参数转换成主机地址
MulticastChat chat=new MulticastChat(group,10000);
chat.start1();
}
}
运行多个程序,为什么不能接受到信息呢?求懂得人帮忙看下。谢谢。