日期:2014-05-20 浏览次数:20926 次
import java.net.*; import java.util.*; /** *利用MulticastSocket实现多人聊天---linux端 */ public class MainClass5 extends Thread { private int port=8888; private InetAddress addr=null; private DatagramPacket inPacket=null;//接收数据包 private DatagramPacket outPacket=null;//发送数据包 private MulticastSocket socket=null; private Scanner sc=null; private byte[] inBuffer=null; private byte[] outBuffer=null; /** *初始化实例 , */ public void init() { try { addr=InetAddress.getByName("230.0.0.1"); socket=new MulticastSocket(port); socket.joinGroup(addr); socket.setNetworkInterface(NetworkInterface.getByName("wlan0"));//设置网络接口后同一台机器上多个客户端间不能通信,类似与一台机器不能登录多个帐号 System.out.println(socket.getNetworkInterface()); socket.setLoopbackMode(true); sc=new Scanner(System.in); inBuffer=new byte[1024]; inPacket =new DatagramPacket(inBuffer,inBuffer.length);//接收数据包 new Thread(this).start();//启动run()线程 如果是new MainClass5().start()则会抛出java.lang.NullPointerException String line; while(true) { line=sc.nextLine(); outBuffer=line.getBytes(); outPacket=new DatagramPacket(outBuffer,outBuffer.length,addr,port); socket.send(outPacket); if(line.equals("quit")) break; } socket.close(); } catch(Exception e) { System.out.println(e); } } /** *实现socket 接收广播数据并且打印到屏幕 */ public void run() { try { while(true) { socket.receive(inPacket); System.out.println("Form "+inPacket.getAddress()+" "+new String(inPacket.getData(),0,inPacket.getLength(),"gb2312")); } } catch(Exception e) { e.printStackTrace(); } } /** *main entru */ public static void main(String[] args) { new MainClass5().init(); } }
import java.net.*; import java.util.*; /** *利用MulticastSocket实现多人聊天--windows端 */ public class MainClass5 extends Thread { private int port=8888; private InetAddress addr=null; private DatagramPacket inPacket=null;//接收数据包 private DatagramPacket outPacket=null;//发送数据包 private MulticastSocket socket=null; private Scanner sc=null; private byte[] inBuffer=null; private byte[] outBuffer=null; /** *初始化实例 */ public void init() { try { addr=InetAddress.getByName("230.0.0.1"); socket=new MulticastSocket(port); socket.joinGroup(addr); socket.setNetworkInterface(NetworkInterface.getByName("eth0")); System.out.println("正在使用的网络接口 "+socket.getNetworkInterface()); socket.setLoopbackMode(true); sc=new Scanner(System.in); inBuffer=new byte[1024]; inPacket =new DatagramPacket(inBuffer,inBuffer.length);//接收数据包 new Thread(this).start();//启动run()线程 如果是new MainClass5().start()则会抛出java.lang.NullPointerException String line; while(true) { line=sc.nextLine(); outBuffer=line.getBytes(); outPacket=new DatagramPacket(outBuffer,outBuffer.length,addr,port); socket.send(outPacket); if(line.equals("quit")) break; } socket.close(); } catch(Exception e) { System.out.println(e); } } /** *实现socket 接收广播数据并且打印到屏幕 */ public void run() { //inBuffer=new byte[1024]; //inPacket =new DatagramPacket(inBuffer,inBuffer.length);//接收数据包 try { while(true) { socket.receive(inPacket); // System.out.println("Form "+inPacket.getAddress()+" "+new String(inPacket.getData(),0,inPacket.getLength(),"UTF-8")); } } catch(Exception e) { e.printStackTrace(); } } /** *main entru */ public static void main(String[] args) { new MainClass5().init(); } }