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上,代碼分為兩部分,一部分是把傳輸或接受的Long類型數(shù)據(jù)轉(zhuǎn)換為byte類型的數(shù)據(jù),然后是基本的數(shù)據(jù)傳輸。
另一方面,直接的字節(jié)流不能轉(zhuǎn)換為Long類型,同理,剛接收的數(shù)據(jù)是字節(jié)類型,直接打印(System.out.println)是以字符串類型輸出的,都需要通過Data的數(shù)據(jù)流進行轉(zhuǎn)換。
更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
Java數(shù)據(jù)結(jié)構(gòu)順序表從零基礎到精通進階
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要創(chuàng)建這種元素組,用變量記錄它們,傳進傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲區(qū)里,元素間的順序關系由它們的存儲順序自然表示2022-03-03
java8列表中通過stream流根據(jù)對象屬性去重的三種方式
這篇文章主要介紹了java8列表中通過stream流根據(jù)對象屬性去重的三種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring注解@Configuration和@Component區(qū)別詳解
@Component和@Configuration都可以作為配置類,之前一直都沒覺得這兩個用起來有什么差別,可能有時程序跑的和自己想的有所區(qū)別也沒注意到,下面這篇文章主要給大家介紹了關于Spring注解@Configuration和@Component區(qū)別的相關資料,需要的朋友可以參考下2023-04-04

