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 }
直接運行程序看效果!