Java網(wǎng)絡編程UDP協(xié)議發(fā)送接收數(shù)據(jù)
本文實例為大家分享了Java網(wǎng)絡編程UDP協(xié)議發(fā)送接收數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
UDP協(xié)議發(fā)送數(shù)據(jù)步驟
A:創(chuàng)建發(fā)送端socket對象;
B:創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包;
C:調(diào)用socket對象的發(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對象;
B:創(chuàng)建一個數(shù)據(jù)包(接收容器);
C:調(diào)用socket對象的接收方法接收數(shù)據(jù);
D:解析數(shù)據(jù),顯示到控制臺;
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
//獲取對方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();
}
}
先運行接收端代碼,再運行發(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ù)來自鍵盤錄入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
//當輸入jieshu時,結(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
//獲取對方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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Boot 安全 API 構(gòu)建之加密解密功能的實踐記錄
本文詳述了如何在SpringBoot3.3環(huán)境中實施API加密的最佳實踐,包括選擇合適的加密算法,密鑰管理,數(shù)據(jù)加密,防止加密漏洞,安全日志記錄,測試和監(jiān)控等方面,同時,文章也對RSA非對稱加密和AES對稱加密的實現(xiàn)步驟進行了詳細的解析2024-10-10
解決SpringCloud下spring-boot-maven-plugin插件的打包問題
這篇文章主要介紹了SpringCloud下spring-boot-maven-plugin插件的打包問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
SpringBoot Security整合JWT授權RestAPI的實現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權RestAPI的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Spring?代理?Bean?獲取不到原始?Bean?對象注解解決方法
這篇文章主要介紹了Spring?代理?Bean?獲取不到原始?Bean?對象注解解決方法,文章圍繞主題相關資料展開詳細介紹,需要的小伙伴可以參考一下2022-04-04
java解析XML Node與Element的區(qū)別(推薦)
下面小編就為大家分享一篇java解析XML Node與Element的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

