java UDP通信客戶端與服務器端實例分析
本文實例講述了java UDP通信客戶端與服務器端。分享給大家供大家參考,具體如下:
最初Udp是以字節(jié)為單位進行傳輸?shù)?,所以有很大的限?/p>
服務器端:
import java.net.*;
public class TestUdpServer {
public static void main(String[] args) throws Exception {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// try {
DatagramSocket ds = new DatagramSocket(2345);
while(true) {
ds.receive(dp);
System.out.println(new String(buf,0,dp.getLength()));
// }
// } catch (Exception e) {
// e.printStackTrace();
}
}
}
用戶端:
import java.net.*;
public class TestUdpClient {
public static void main(String[] args) throws Exception {
byte[] buf = new byte[1024];
buf = (new String("hello")).getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",2345));
// try {
DatagramSocket ds = new DatagramSocket(5679);
ds.send(dp);
ds.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
注:由于必須以字節(jié)為單位進行傳輸,Udp的傳輸用了一個容器類的東西,用來接收字節(jié)
先建一個字節(jié)數(shù)組,然后以這個數(shù)組創(chuàng)建容器。用來傳輸數(shù)據(jù)。
實例:傳輸一個Long類型的數(shù)據(jù)
服務器端:
import java.io.*;
import java.net.*;
public class UdpServer {
public static void main(String[] args) throws Exception {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
DatagramSocket ds = new DatagramSocket(2345);
while(true) {
ByteArrayInputStream is = new ByteArrayInputStream(buf);
DataInputStream dis = new DataInputStream(is);
ds.receive(dp);
System.out.println(dis.readLong());
}
}
}
用戶端:
import java.io.*;
import java.net.*;
public class UdpClient {
public static void main(String[] args) throws Exception {
Long n = 10000L;
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeLong(n);
byte[] buf = new byte[1024];
buf = os.toByteArray();
System.out.println(buf.length);
DatagramPacket dp = new DatagramPacket(buf,buf.length,
new InetSocketAddress("127.0.0.1",2345));
DatagramSocket ds = new DatagramSocket(5679);
ds.send(dp);
ds.close();
}
}
注:由于Udp是以字節(jié)為單位進行傳輸?shù)模砸玫紹yteArray的輸入和輸出流用來進行數(shù)據(jù)的轉(zhuǎn)換。
另外,相較于Output流,Input流在構(gòu)建的時候需要一個數(shù)組作為參數(shù),用來存放數(shù)據(jù)。
在基本的Udp傳輸?shù)幕A(chǔ)上,代碼分為兩部分,一部分是把傳輸或接受的Long類型數(shù)據(jù)轉(zhuǎn)換為byte類型的數(shù)據(jù),然后是基本的數(shù)據(jù)傳輸。
另一方面,直接的字節(jié)流不能轉(zhuǎn)換為Long類型,同理,剛接收的數(shù)據(jù)是字節(jié)類型,直接打?。⊿ystem.out.println)是以字符串類型輸出的,都需要通過Data的數(shù)據(jù)流進行轉(zhuǎn)換。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關(guān)文章
Java將網(wǎng)絡圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問題
這篇文章主要介紹了Java將網(wǎng)絡圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Java源碼解析之HashMap的put、resize方法詳解
這篇文章主要介紹了Java源碼解析之HashMap的put、resize方法詳解,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04
MyBatis-Plus中Service接口的lambdaUpdate用法及實例分析
本文將詳細講解MyBatis-Plus中的lambdaUpdate用法,并提供豐富的案例來幫助讀者更好地理解和應用該特性,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
關(guān)于Spring的AnnotationAwareAspectJAutoProxyCreator類解析
這篇文章主要介紹了關(guān)于Spring的AnnotationAwareAspectJAutoProxyCreator類解析,Spring是一個開源免費的框架 , 容器,是一個輕量級的框架 ,需要的朋友可以參考下2023-05-05
解決IDEA Gradle構(gòu)建報錯''Cause: zip END header not found''
這篇文章主要介紹了解決IDEA Gradle構(gòu)建報錯"Cause: zip END header not found"的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

