码迷,mamicode.com
首页 > 其他好文 > 详细

聊天软件项目UDP升级版

时间:2016-04-10 01:03:06      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

聊天软件项目UDP升级版

标签:

原文地址:http://www.cnblogs.com/hellochennan/p/5373199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!