聊天软件项目UDP升级版
1 import java.net.*; 2 import java.io.*; 3 class UdpSend2 4 { 5 public static void main(String[] args) throws Exception 6 { 7 DatagramSocket ds = new DatagramSocket(); 8 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); 9 String line = null; 10 while((line=bufr.readLine())!=null) 11 { 12 if("886".equals(line)) 13 break; 14 byte[] buf = line.getBytes(); 15 DatagramPacket dp = 16 new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10001); 17 ds.send(dp); 18 } 19 ds.close(); 20 } 21 } 22 class UdpRece2 23 { 24 public static void main(String[] args) throws Exception 25 { 26 DatagramSocket ds = new DatagramSocket(10001); 27 while(true) 28 { 29 byte[] buf = new byte[1024]; 30 DatagramPacket dp = new DatagramPacket(buf,buf.length); 31 ds.receive(dp); 32 String ip = dp.getAddress().getHostAddress(); 33 String data = new String(dp.getData(),0,dp.getLength()); 34 System.out.println(ip+"::"+data); 35 } 36 } 37 }
厚积薄发,行胜于言@飞鸟各投林