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

网络编程-UDP2

时间:2016-01-19 19:24:52      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.yyq;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 import java.net.DatagramPacket;
 6 import java.net.DatagramSocket;
 7 import java.net.InetAddress;
 8 import java.net.SocketException;
 9 
10 public class UdpSend2 {
11     public static void main(String[] args) throws Exception {
12         //1. 创建服务
13         DatagramSocket ds = new DatagramSocket(5555);
14         //2.从键盘中录入数据
15         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
16         String line = null;
17         while((line = bufr.readLine())!=null){
18             if("88".equals(line)){
19                 break;
20             }
21             byte[] buf = line.getBytes();
22             DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),10001);
23             ds.send(dp);
24             //ds.close();
25         }
26     }
27 }
28 
29 
30 
31 
32 package com.yyq;
33 
34 import java.io.IOException;
35 import java.net.DatagramPacket;
36 import java.net.DatagramSocket;
37 import java.net.SocketException;
38 
39 public class UdpRece2 {
40     public static void main(String[] args) throws Exception {
41         DatagramSocket ds = new DatagramSocket(10001);
42         while(true){
43             byte[] buf = new byte[1024];
44             DatagramPacket dp = new DatagramPacket(buf,buf.length);
45             ds.receive(dp);
46             String ip = dp.getAddress().getHostAddress();
47             String data = new String(dp.getData(),0,dp.getLength());
48             System.out.println(ip + ":" + data +":"+dp.getPort());
49             System.out.println("haode");
50         }
51     }
52 }

 

网络编程-UDP2

标签:

原文地址:http://www.cnblogs.com/yangyongqian/p/5142948.html

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