java 传输 udp_java UDP传输
①:只要是網(wǎng)絡(luò)傳輸,必須有socket 。
②:數(shù)據(jù)一定要封裝到數(shù)據(jù)包中,數(shù)據(jù)包中包括目的地址、端口、數(shù)據(jù)等信息。
直接操作udp不可能,對(duì)于java語(yǔ)言應(yīng)該將udp封裝成對(duì)象,易于我們的使用,這個(gè)對(duì)象就是DatagramSocket. 封裝了udp傳輸協(xié)議的socket對(duì)象。
因?yàn)閿?shù)據(jù)包中包含的信息較多,為了操作這些信息方便,也一樣會(huì)將其封裝成對(duì)象。這個(gè)數(shù)據(jù)包對(duì)象就是:DatagramPacket.通過(guò)這個(gè)對(duì)象中的方法,就可以獲取到數(shù)據(jù)包中的各種信息。
DatagramSocket具備發(fā)送和接受功能,在進(jìn)行udp傳輸時(shí),需要明確一個(gè)是發(fā)送端,一個(gè)是接收端。
udp的發(fā)送端:
①:建立udp的socket服務(wù),創(chuàng)建對(duì)象時(shí)如果沒(méi)有明確端口,系統(tǒng)會(huì)自動(dòng)分配一個(gè)未被使用的端口。
②:明確要發(fā)送的具體數(shù)據(jù)。
③:將數(shù)據(jù)封裝成了數(shù)據(jù)包。
④:用socket服務(wù)的send方法將數(shù)據(jù)包發(fā)送出去。
⑤:關(guān)閉資源。
udp的接收端:
①:創(chuàng)建udp的socket服務(wù),必須要明確一個(gè)端口,作用在于,只有發(fā)送到這個(gè)端口的數(shù)據(jù)才是這個(gè)接收端可以處理的數(shù)據(jù)。
②:定義數(shù)據(jù)包,用于存儲(chǔ)接收到數(shù)據(jù)。
③:通過(guò)socket服務(wù)的接收方法將收到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)包中。
④:通過(guò)數(shù)據(jù)包的方法獲取數(shù)據(jù)包中的具體數(shù)據(jù)內(nèi)容,比如ip、端口、數(shù)據(jù)等等。
⑤:關(guān)閉資源。
Eg:
發(fā)送端(客戶端)
import java.net.*;
class? UdpSend{
public static void main(String[] args)throws Exception {
// 1,建立udp的socket服務(wù)。
DatagramSocket ds = new DatagramSocket(8888);//指定發(fā)送端口,這個(gè)可以不指定,系統(tǒng)會(huì)隨機(jī)分配。
// 2,明確要發(fā)送的具體數(shù)據(jù)。
String text = "udp傳輸演示 哥們來(lái)了";
byte[] buf = text.getBytes();
// 3,將數(shù)據(jù)封裝成了數(shù)據(jù)包。
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4,用socket服務(wù)的send方法將數(shù)據(jù)包發(fā)送出去。
ds.send(dp);
// 5,關(guān)閉資源。
ds.close();
}
}
接收端(服務(wù)器端)
import java.net.*;
class UdpRece {
public static void main(String[] args) throws Exception{
// 1,創(chuàng)建udp的socket服務(wù)。
DatagramSocket ds = new DatagramSocket(10000);//必須指定,并且和上面的端口號(hào)一樣!
// 2,定義數(shù)據(jù)包,用于存儲(chǔ)接收到數(shù)據(jù)。先定義字節(jié)數(shù)組,數(shù)據(jù)包會(huì)把數(shù)據(jù)存儲(chǔ)到字節(jié)數(shù)組中。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3,通過(guò)socket服務(wù)的接收方法將收到的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)包中。
ds.receive(dp);//該方法是阻塞式方法。
// 4,通過(guò)數(shù)據(jù)包的方法獲取數(shù)據(jù)包中的具體數(shù)據(jù)內(nèi)容,比如ip,端口,數(shù)據(jù)等等。
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//將字節(jié)數(shù)組中的有效部分轉(zhuǎn)成字符串。
System.out.println(ip+":"+port+"--"+text);
// 5,關(guān)閉資源。
ds.close();
}
}
練習(xí):
通過(guò)鍵盤(pán)錄入獲取要發(fā)送的信息。
將發(fā)送和接收分別封裝到兩個(gè)線程中。
package july76net;
//一個(gè)聊天的例子,利用UDP傳輸協(xié)議
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//客戶端,發(fā)送端
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));//數(shù)據(jù)源是鍵盤(pán)錄入
String line;
while ((line = br.readLine()) != null) {
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("localhost"), 10225);
ds.send(dp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 服務(wù)器端,接收端
class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 0, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + "???? " + data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Demo6 {
public static void main(String[] args) throws Exception {
DatagramSocket sendDs = new DatagramSocket();
DatagramSocket receDs = new DatagramSocket(10225);
new Thread(new Send(sendDs)).start();
new Thread(new Rece(receDs)).start();
}
}
輸出:
你好
127.0.0.1???? 你好
你好
127.0.0.1???? 你好
總結(jié)
以上是生活随笔為你收集整理的java 传输 udp_java UDP传输的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [html] 你认为Html的术难点在
- 下一篇: [html] HTML5中新添加的表单