您好,登錄后才能下訂單哦!
java 中模擬UDP傳輸?shù)陌l(fā)送端和接收端實例詳解
一、創(chuàng)建UDP傳輸?shù)陌l(fā)送端
1、建立UDP的Socket服務(wù);
2、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中;
3、通過UDP的Socket服務(wù)將數(shù)據(jù)包發(fā)送出去;
4、關(guān)閉Socket服務(wù)。
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) throws IOException { System.out.println("發(fā)送端啟動......"); // 1、創(chuàng)建UDP的Socket,使用DatagramSocket對象 DatagramSocket ds = new DatagramSocket(); // 2、將要發(fā)送的數(shù)據(jù)封裝到數(shù)據(jù)包中 String str = "UDP傳輸演示:I'm coming!"; byte[] buf = str.getBytes(); //使用DatagramPacket將數(shù)據(jù)封裝到該對象的包中 DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000); // 3、通過UDP的Socket服務(wù)將數(shù)據(jù)包發(fā)送出去,使用send方法 ds.send(dp); // 4、關(guān)閉Socket服務(wù) ds.close(); } }
二、創(chuàng)建UDP傳輸?shù)慕邮斩?/strong>
1、建立UDP的Socket服務(wù),因為要接收數(shù)據(jù),所以必須明確一個端口號;
2、創(chuàng)建數(shù)據(jù)包,用于存儲接收到的數(shù)據(jù),方便用數(shù)據(jù)包對象的方法解析這些數(shù)據(jù);
3、使用UDP的Socket服務(wù)的receive方法接收數(shù)據(jù)并存儲到數(shù)據(jù)包中;
4、通過數(shù)據(jù)包的方法解析這些數(shù)據(jù);
5、關(guān)閉Socket服務(wù)。
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) throws IOException { System.out.println("接收端啟動......"); // 1、建立UDP的Socket服務(wù) DatagramSocket ds = new DatagramSocket(10000); // 2、創(chuàng)建數(shù)據(jù)包 byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 3、使用接收方法將數(shù)據(jù)存儲到數(shù)據(jù)包中 ds.receive(dp); // 該方法為阻塞式的方法 // 4、通過數(shù)據(jù)包對象的方法解析這些數(shù)據(jù),例如:地址、端口、數(shù)據(jù)內(nèi)容等 String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":" + port + ":" + text); // 5、關(guān)閉Socket服務(wù) ds.close(); } }
三、優(yōu)化UDP傳輸?shù)陌l(fā)送端和接收端
由于在前兩部分中,我們一次只能發(fā)送(或接收)一條消息,然后就關(guān)閉服務(wù)啦!因此如果我們想要發(fā)送多條消息,則需要不斷的在發(fā)送端修改發(fā)送的內(nèi)容,并且還需要重新啟動服務(wù)器,比較麻煩。為了克服以上的缺點,我們可以對其進行優(yōu)化,即:
1、在發(fā)送端,創(chuàng)建BufferedReader,從鍵盤錄入內(nèi)容;
2、在接收端,添加while(ture)循環(huán),不斷的循環(huán)接收內(nèi)容。
/** *優(yōu)化UDP傳輸?shù)陌l(fā)送端 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) throws IOException { System.out.println("發(fā)送端啟動......"); // 創(chuàng)建UDP的Socket,使用DatagramSocket對象 DatagramSocket ds = new DatagramSocket(); BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = bufr.readLine()) != null) { // 使用DatagramPacket將數(shù)據(jù)封裝到該對象的包中 byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000); // 通過UDP的Socket服務(wù)將數(shù)據(jù)包發(fā)送出去,使用send方法 ds.send(dp); // 如果輸入信息為over,則結(jié)束循環(huán) if ("over".equals(line)) break; } // 關(guān)閉Socket服務(wù) ds.close(); } }
/** *優(yōu)化UDP傳輸?shù)慕邮斩?*/ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) throws IOException { System.out.println("接收端啟動......"); // 建立UDP的Socket服務(wù) DatagramSocket ds = new DatagramSocket(10000); while(true) { // 創(chuàng)建數(shù)據(jù)包 byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 使用接收方法將數(shù)據(jù)存儲到數(shù)據(jù)包中 ds.receive(dp); // 該方法為阻塞式的方法 // 通過數(shù)據(jù)包對象的方法解析這些數(shù)據(jù),例如:地址、端口、數(shù)據(jù)內(nèi)容等 String ip = dp.getAddress().getHostAddress(); int port = dp.getPort(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":" + port + ":" + text); } } }
四、創(chuàng)建聊天室
根據(jù)UDP(User Datagram Protocol, 用戶數(shù)據(jù)報協(xié)議)的相關(guān)性質(zhì),我們可以進一步創(chuàng)建一個簡單的基于UDP傳輸協(xié)議下的聊天室,實現(xiàn)互動聊天的功能。
/** *創(chuàng)建UDP傳輸下的聊天室發(fā)送端 */ package chat; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Send implements Runnable { private DatagramSocket ds; public Send(DatagramSocket ds) { this.ds = ds; } public void run() { try { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = bufr.readLine()) != null) { byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.255"), 10001); ds.send(dp); if ("886".equals(line)) break; } ds.close(); } catch (Exception e) { System.out.println("對不起,發(fā)生錯誤啦!"); } } }
/** *創(chuàng)建UDP傳輸下的聊天室接收端 */ package chat; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Rece implements Runnable { private DatagramSocket ds; public Rece(DatagramSocket ds) { this.ds = ds; } public void run() { try { while (true) { byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String text = new String(dp.getData(), 0, dp.getLength()); System.out.println(ip + ":::" + text); if(text.equals("886")){ System.out.println(ip+"......退出聊天室!"); } } } catch (Exception e) { System.out.println("對不起,發(fā)生錯誤啦!"); } } }
/** *創(chuàng)建UDP傳輸下的聊天室 */ package chat; import java.io.IOException; import java.net.DatagramSocket; public class ChatRoom { public static void main(String[] args) throws IOException { DatagramSocket send = new DatagramSocket(); DatagramSocket rece = new DatagramSocket(10001); new Thread(new Send(send)).start(); new Thread(new Rece(rece)).start(); } }
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。