Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù)
本文實(shí)例為大家分享了Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
UDP協(xié)議發(fā)送數(shù)據(jù)步驟A:創(chuàng)建發(fā)送端socket對(duì)象;
B:創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包;
C:調(diào)用socket對(duì)象的發(fā)送方法發(fā)送數(shù)據(jù)包;
D:釋放資源
package net; import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress; public class SendDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(); //B byte[] by = 'Hello,UDP'.getBytes(); int length = by.length; InetAddress addr = InetAddress.getByName('192.168.1.22'); int port = 10010; DatagramPacket dp = new DatagramPacket(by, length, addr, port); //C ds.send(dp); //D ds.close(); }}UDP協(xié)議接收數(shù)據(jù)步驟
A:創(chuàng)建接收端socket對(duì)象;
B:創(chuàng)建一個(gè)數(shù)據(jù)包(接收容器);
C:調(diào)用socket對(duì)象的接收方法接收數(shù)據(jù);
D:解析數(shù)據(jù),顯示到控制臺(tái);
E:釋放資源
package net; import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress; public class ReceiveDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(10010); //B byte[] by = new byte[1024]; int length = by.length; DatagramPacket dp = new DatagramPacket(by, length); //C ds.receive(dp); //D //獲取對(duì)方ip InetAddress addr = dp.getAddress(); String ip = addr.getHostAddress(); byte[] by2 = dp.getData(); int len = by2.length; String s = new String(by2, 0, len); System.out.println(ip+'發(fā)送的數(shù)據(jù)是:'+s); //E ds.close(); }}
先運(yùn)行接收端代碼,再運(yùn)行發(fā)送端代碼。
多次從鍵盤接收發(fā)送數(shù)據(jù)版本package net; 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 SendDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(); //數(shù)據(jù)來(lái)自鍵盤錄入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = br.readLine()) != null){ //當(dāng)輸入jieshu時(shí),結(jié)束 if('jieshu'.equals(line)){ break; } //B byte[] by = line.getBytes(); int length = by.length; InetAddress addr = InetAddress.getByName('192.168.1.22'); int port = 10010; DatagramPacket dp = new DatagramPacket(by, length, addr, port); //C ds.send(dp); } //D ds.close(); }}
package net; import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress; public class ReceiveDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(10010); //多次接受版本 while(true){ //B byte[] by = new byte[1024]; int length = by.length; DatagramPacket dp = new DatagramPacket(by, length); //C ds.receive(dp); //D //獲取對(duì)方ip InetAddress addr = dp.getAddress(); String ip = addr.getHostAddress(); byte[] by2 = dp.getData(); int len = by2.length; String s = new String(by2, 0, len); System.out.println(ip+'發(fā)送的數(shù)據(jù)是:'+s); } //E //ds.close(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何基于python3和Vue實(shí)現(xiàn)AES數(shù)據(jù)加密2. 10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站3. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)4. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】5. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送6. Python編寫nmap掃描工具7. Java向Runnable線程傳遞參數(shù)方法實(shí)例解析8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. php5.6不能擴(kuò)展redis.so的解決方法
