简单的UDP服务端和客户端示例

UDP的理论不再多说,我这里直接给出一个关于UDP的HelloWorld程序,代码明了,希望对刚入门的学生有所帮助!

当然,实际上,在这块我也刚入门!

 

首先写服务端代码,服务端邦定本地的IP和端口来监听访问:

  

 1 package udp;  
 2   
 3 import java.io.IOException;  
 4 import java.net.DatagramPacket;  
 5 import java.net.DatagramSocket;  
 6 import java.net.InetSocketAddress;  
 7   
 8 /** 
 9  * UDP服务类 
10  */  
11 public class UdpServerSocket {  
12       
13     private byte[] buffer = new byte[1024];  
14     private static DatagramSocket ds = null;  
15     private DatagramPacket packet = null;  
16     private InetSocketAddress socketAddress = null;  
17       
18     /** 
19      * 测试方法 
20      */  
21     public static void main(String[] args) throws Exception {  
22         String serverHost = "127.0.0.1";  
23         int serverPort = 3344;  
24         UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost,  
25                 serverPort);  
26         while (true) {  
27             udpServerSocket.receive();  
28             udpServerSocket.response("你好,吃了吗!");  
29         }         
30     }  
31   
32     /** 
33      * 构造函数,绑定主机和端口 
34      */  
35     public UdpServerSocket(String host, int port) throws Exception {  
36         socketAddress = new InetSocketAddress(host, port);  
37         ds = new DatagramSocket(socketAddress);  
38         System.out.println("服务端启动!");  
39     }  
40   
41     /** 
42      * 接收数据包,该方法会造成线程阻塞 
43      */  
44     public final String receive() throws IOException {  
45         packet = new DatagramPacket(buffer, buffer.length);  
46         ds.receive(packet);  
47         String info = new String(packet.getData(), 0, packet.getLength());  
48         System.out.println("接收信息:" + info);  
49         return info;  
50     }  
51   
52     /** 
53      * 将响应包发送给请求端 
54      */  
55     public final void response(String info) throws IOException {  
56         System.out.println("客户端地址 : " + packet.getAddress().getHostAddress()  
57                 + ",端口:" + packet.getPort());  
58         DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet  
59                 .getAddress(), packet.getPort());  
60         dp.setData(info.getBytes());  
61         ds.send(dp);  
62     }  
63 }  

 

运行后提示服务端运行成功,程序开始监听端口,接收方法堵塞,当有访问时才会向下进行!

 

我们写客户端进行访问,看到网上的例子都是直接创建了 DatagramSocket 对象,而其实自己都不知道自己使用的端口是那个,这里我创建时会指定自己邦定的端口,其实很简单,就是初始化该对象时传递一个端口参数。

这里你访问客户端时客户端会打印你的IP和端口!

看一看客户端代码:

 1 package udp;  
 2   
 3 import java.io.*;  
 4 import java.net.*;  
 5   
 6 /** 
 7  * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息 
 8  */  
 9 public class UdpClientSocket {  
10     private byte[] buffer = new byte[1024];  
11   
12     private static DatagramSocket ds = null;  
13       
14     /** 
15      * 测试客户端发包和接收回应信息的方法 
16      */  
17     public static void main(String[] args) throws Exception {  
18         UdpClientSocket client = new UdpClientSocket();  
19         String serverHost = "127.0.0.1";  
20         int serverPort = 3344;  
21         client.send(serverHost, serverPort, ("你好,亲爱的!").getBytes());  
22         byte[] bt = client.receive();  
23         System.out.println("服务端回应数据:" + new String(bt));  
24         // 关闭连接  
25         try {  
26             ds.close();  
27         } catch (Exception ex) {  
28             ex.printStackTrace();  
29         }  
30     }  
31   
32     /** 
33      * 构造函数,创建UDP客户端 
34      */  
35     public UdpClientSocket() throws Exception {  
36         ds = new DatagramSocket(8899); // 邦定本地端口作为客户端  
37     }  
38       
39     /** 
40      * 向指定的服务端发送数据信息 
41      */  
42     public final void send(final String host, final int port,  
43             final byte[] bytes) throws IOException {  
44         DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);  
45         ds.send(dp);  
46     }  
47   
48     /** 
49      * 接收从指定的服务端发回的数据 
50      */  
51     public final byte[] receive()  
52             throws Exception {  
53         DatagramPacket dp = new DatagramPacket(buffer, buffer.length);  
54         ds.receive(dp);       
55         byte[] data = new byte[dp.getLength()];  
56         System.arraycopy(dp.getData(), 0, data, 0, dp.getLength());       
57         return data;  
58     }  
59 }  

 直接运行程序看效果!

posted @ 2016-10-11 09:35  流浪小妮子  阅读(5290)  评论(0编辑  收藏  举报