UDP的理論不再多說,我這里直接給出一個(gè)關(guān)于UDP的HelloWorld程序,代碼明了,希望對(duì)剛?cè)腴T的學(xué)生有所幫助!
當(dāng)然,實(shí)際上,在這塊我也剛?cè)腴T!
首先寫服務(wù)端代碼,服務(wù)端邦定本地的IP和端口來監(jiān)聽訪問:
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* UDP服務(wù)類
*/
public class UdpServerSocket {
private byte[] buffer = new byte[1024];
private static DatagramSocket ds = null;
private DatagramPacket packet = null;
private InetSocketAddress socketAddress = null;
/**
* 測(cè)試方法
*/
public static void main(String[] args) throws Exception {
String serverHost = "127.0.0.1";
int serverPort = 3344;
UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost,
serverPort);
while (true) {
udpServerSocket.receive();
udpServerSocket.response("你好,吃了嗎!");
}
}
/**
* 構(gòu)造函數(shù),綁定主機(jī)和端口
*/
public UdpServerSocket(String host, int port) throws Exception {
socketAddress = new InetSocketAddress(host, port);
ds = new DatagramSocket(socketAddress);
System.out.println("服務(wù)端啟動(dòng)!");
}
/**
* 接收數(shù)據(jù)包,該方法會(huì)造成線程阻塞
*/
public final String receive() throws IOException {
packet = new DatagramPacket(buffer, buffer.length);
ds.receive(packet);
String info = new String(packet.getData(), 0, packet.getLength());
System.out.println("接收信息:" + info);
return info;
}
/**
* 將響應(yīng)包發(fā)送給請(qǐng)求端
*/
public final void response(String info) throws IOException {
System.out.println("客戶端地址 : " + packet.getAddress().getHostAddress()
+ ",端口:" + packet.getPort());
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet
.getAddress(), packet.getPort());
dp.setData(info.getBytes());
ds.send(dp);
}
}
運(yùn)行后提示服務(wù)端運(yùn)行成功,程序開始監(jiān)聽端口,接收方法堵塞,當(dāng)有訪問時(shí)才會(huì)向下進(jìn)行!
我們寫客戶端進(jìn)行訪問,看到網(wǎng)上的例子都是直接創(chuàng)建了 DatagramSocket 對(duì)象,而其實(shí)自己都不知道自己使用的端口是那個(gè),這里我創(chuàng)建時(shí)會(huì)指定自己邦定的端口,其實(shí)很簡(jiǎn)單,就是初始化該對(duì)象時(shí)傳遞一個(gè)端口參數(shù)。
這里你訪問客戶端時(shí)客戶端會(huì)打印你的IP和端口!
看一看客戶端代碼:
package udp;
import java.io.*;
import java.net.*;
/**
* UDP客戶端程序,用于對(duì)服務(wù)端發(fā)送數(shù)據(jù),并接收服務(wù)端的回應(yīng)信息
*/
public class UdpClientSocket {
private byte[] buffer = new byte[1024];
private static DatagramSocket ds = null;
/**
* 測(cè)試客戶端發(fā)包和接收回應(yīng)信息的方法
*/
public static void main(String[] args) throws Exception {
UdpClientSocket client = new UdpClientSocket();
String serverHost = "127.0.0.1";
int serverPort = 3344;
client.send(serverHost, serverPort, ("你好,親愛的!").getBytes());
byte[] bt = client.receive();
System.out.println("服務(wù)端回應(yīng)數(shù)據(jù):" + new String(bt));
// 關(guān)閉連接
try {
ds.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 構(gòu)造函數(shù),創(chuàng)建UDP客戶端
*/
public UdpClientSocket() throws Exception {
ds = new DatagramSocket(8899); // 邦定本地端口作為客戶端
}
/**
* 向指定的服務(wù)端發(fā)送數(shù)據(jù)信息
*/
public final void send(final String host, final int port,
final byte[] bytes) throws IOException {
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);
ds.send(dp);
}
/**
* 接收從指定的服務(wù)端發(fā)回的數(shù)據(jù)
*/
public final byte[] receive()
throws Exception {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
byte[] data = new byte[dp.getLength()];
System.arraycopy(dp.getData(), 0, data, 0, dp.getLength());
return data;
}
}
直接運(yùn)行程序看效果!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。