UDP通訊的實(shí)現(xiàn)
1.DatagramSocket:用于發(fā)送或接收數(shù)據(jù)包
當(dāng)服務(wù)器要向客戶端發(fā)送數(shù)據(jù)時(shí)妨马,需要在服務(wù)器端產(chǎn)生一個(gè)DatagramSocket對(duì)象,在客戶端產(chǎn)生一個(gè)DatagramSocket對(duì)象。服務(wù)器端的DatagramSocket將DatagramPacket發(fā)送到網(wǎng)絡(luò)上圈浇,然后被客戶端的DatagramSocket接收切省。
DatagramSocket有兩種構(gòu)造函數(shù)。一種是無需任何參數(shù)的蛔六,常用于客戶端荆永。另一種需要指定端口,常用于服務(wù)器国章。
常用方法:send具钥、receive、 close
?
2.DatagramPacket:數(shù)據(jù)容器(封包)的作用
常用方法:構(gòu)造函數(shù)捉腥、getAddrress(獲取發(fā)送或接收方計(jì)算機(jī)的IP地址)氓拼、getData(獲取發(fā)送或接收的數(shù)據(jù))、setData(設(shè)置發(fā)送的數(shù)據(jù))
3.UDP通信編程基本步驟:
a)創(chuàng)建客戶端的DatagramSocket抵碟,創(chuàng)建時(shí)桃漾,定義客戶端的監(jiān)聽端口
b)創(chuàng)建服務(wù)器端的DatagramSocket,創(chuàng)建時(shí)拟逮,定義服務(wù)器端的監(jiān)聽端口
c)在服務(wù)器端定義DatagramPacket對(duì)象撬统,封裝待發(fā)送的數(shù)據(jù)包。
d)服務(wù)器端將數(shù)據(jù)包發(fā)送出去
e)客戶端接收數(shù)據(jù)包
【示例1】客戶端與服務(wù)器端單向通信之客戶端
import?java.net.DatagramPacket;
import?java.net.DatagramSocket;
import?java.net.InetSocketAddress;
public?class?Client {
????public?static?void?main(String[] args)?throws?? Exception {
???????byte[] b = "aaaa".getBytes();
??????? //必須告訴數(shù)據(jù)包要發(fā)到哪里去
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length,new?? InetSocketAddress("localhost",8999));
???????//我本身占用9000端口向外面機(jī)器發(fā)數(shù)據(jù)包
?????? DatagramSocket ds =?new?DatagramSocket(9000);
?????? ds.send(dp);
?????? ds.close();
??? }
}?
【示例2】客戶端與服務(wù)器端單向通信之服務(wù)器端
import?java.net.DatagramPacket;
import?java.net.DatagramSocket;
public?class?Server {
????public?static?void?main(String[] args)?throws?? Exception {
?????? DatagramSocket ds =?new?DatagramSocket(8999);
???????byte[] b =?new?byte[1024];
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length);
?????? ds.receive(dp);??//阻塞式方法
?????? String string =?new?? String(dp.getData(),0,dp.getLength());? ??//dp.getLength()返回實(shí)際收到的數(shù)據(jù)的字節(jié)數(shù)
?????? System.out.println(string);
?????? ds.close();
??? }
}
通過ByteArrayInputStream敦迄、ByteArrayOutputStream可以傳遞基本類型數(shù)據(jù)恋追。
【示例3】客戶端
public?class?Client {
????public?static?void?main(String[] args)?throws?? Exception {
???????long?n = 2000L;
??????? ByteArrayOutputStream bos = new ByteArrayOutputStream();
?????? DataOutputStream ? dos = new ? DataOutputStream(bos);
?????? dos.writeLong(n);
?????? byte[] b = bos.toByteArray();
?????? //必須告訴數(shù)據(jù)包要發(fā)到哪里去
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length,new?? InetSocketAddress("localhost",8999));
?????? //我本身占用9000端口向外面機(jī)器發(fā)數(shù)據(jù)包
?????? DatagramSocket ds =?new?DatagramSocket(9000);
?????? ds.send(dp);
?????? ds.close();
??? }
}?
【示例4】服務(wù)器端
public?class?Server {
????public?static?void?main(String[] args)?throws?? Exception {
?????? DatagramSocket ds =?new?DatagramSocket(8999);
???????byte[] b =?new?byte[1024];
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length);
?????? ds.receive(dp);??//阻塞式方法
???????ByteArrayInputStream ? bis = new ? ByteArrayInputStream(dp.getData());
?????? DataInputStream ? dis = new ? DataInputStream(bis);
?????? System.out.println(dis.readLong());
?????? ds.close();??
}
}
通過ByteArrayInputStream、ByteArrayOutputStream可以傳遞對(duì)象罚屋。
【示例5】Person類(客戶端與服務(wù)器端都需要存在Person類)
class?Person?implements?Serializable{
????int?age;
??? String name;
????public?Person(int?age, String name) {
???????super();
???????this.age = age;
???????this.name = name;
??? }
}
【示例6】客戶端
public?class?Client {
????public?static?void?main(String[] args)?throws?? Exception {
?????? Person person =?new?Person(20,"aa");
???????ByteArrayOutputStream ? bos = new ? ByteArrayOutputStream();
?????? ObjectOutputStream ? oos = new ? ObjectOutputStream(bos);
?????? oos.writeObject(person);
?????? byte[] b = bos.toByteArray();
?????? //必須告訴數(shù)據(jù)包要發(fā)到哪里去
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length,new?? InetSocketAddress("localhost",8999));
?????? //我本身占用9000端口向外面機(jī)器發(fā)數(shù)據(jù)包
?????? DatagramSocket ds =?new?DatagramSocket(9000);
?????? ds.send(dp);
?????? ds.close();
??? }
} ?
【示例7】服務(wù)器端
public?class?Server {
????public?static?void?main(String[] args)?throws?? Exception {
?????? DatagramSocket ds =?new?DatagramSocket(8999);
???????byte[] b =?new?byte[1024];
?????? DatagramPacket dp =?new?DatagramPacket(b,b.length);
?????? ds.receive(dp);? //阻塞式方法
???????ByteArrayInputStream ? bis = new ? ByteArrayInputStream(dp.getData());
?????? ObjectInputStream ? ois = new ? ObjectInputStream(bis);
?????? Person ? person = (Person) ois.readObject();
?????? System.out.println(person.name);
?????? ds.close();
??? }
}
「全棧Java筆記」是一部能幫大家從零到一成長(zhǎng)為全棧Java工程師系列筆記苦囱。筆者江湖人稱 Mr. G,10年Java研發(fā)經(jīng)驗(yàn)脾猛,曾在神州數(shù)碼撕彤、航天院某所研發(fā)中心從事軟件設(shè)計(jì)及研發(fā)工作,從小白逐漸做到工程師猛拴、高級(jí)工程師羹铅、架構(gòu)師。精通Java平臺(tái)軟件開發(fā)愉昆,精通JAVAEE职员,熟悉各種流行開發(fā)框架。
?筆記包含從淺入深的六大部分:
?A-Java入門階段
?B-數(shù)據(jù)庫從入門到精通
?C-手刃移動(dòng)前端和Web前端
?D-J2EE從了解到實(shí)戰(zhàn)
?E-Java高級(jí)框架精解
?F-Linux和Hadoop?