import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Scanner;
public class Multicast implements Runnable
{private static final String BROADCAST_IP
= "230.0.0.1";public static final int BROADCAST_PORT = 30000;private static final int DATA_LEN = 4096;private MulticastSocket socket = null;
private InetAddress broadcastAddress = null;
private Scanner scan = null;byte[] inBuff = new byte[DATA_LEN];private DatagramPacket inPacket = 
new DatagramPacket(inBuff , inBuff.length);private DatagramPacket outPacket = null;
public void init()throws IOException
{
try
{socket = new MulticastSocket(BROADCAST_PORT);
broadcastAddress = InetAddress.getByName(BROADCAST_IP);socket.joinGroup(broadcastAddress);socket.setLoopbackMode(false);outPacket = new DatagramPacket(new byte[0] , 0 ,
broadcastAddress , BROADCAST_PORT);new Thread(this).start();scan = new Scanner(System.in);while(scan.hasNextLine())
{byte[] buff = scan.nextLine().getBytes();outPacket.setData(buff);socket.send(outPacket);
}
}
finally
{
socket.close();
}
}
public void run()
{
try
{
while(true)
{socket.receive(inPacket);System.out.println("聊天信息:" + new String(inBuff , 0 , 
inPacket.getLength()));
}
}catch (IOException ex)
{
ex.printStackTrace();
try
{
if (socket != null)
{socket.leaveGroup(broadcastAddress);socket.close();
}
System.exit(1); 
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args) 
throws IOException
{
new Multicast().init();
}
}
这个,我刚刚自己在自己电脑上开了两个,双方能进行通信。
但是,我和我同学的电脑同时连上一个路由器,并且把外网关了,就不能传递信息,为什么呀?
急~~~在线等。求教