Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù)
本文實例為大家分享了Java網(wǎng)絡(luò)編程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){
//當(dāng)輸入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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中常用輸出方式(print() println() printf())
這篇文章主要介紹了Java中常用輸出方式(print() println() printf()),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring boot 整合 Okhttp3 并封裝請求工具實例 詳解
OkHttp作為一款成熟、穩(wěn)定、易用的HTTP客戶端庫,擁有較高的性能和多樣化的功能,已被廣泛應(yīng)用于移動應(yīng)用開發(fā)、Web服務(wù)端開發(fā)等領(lǐng)域,這篇文章主要介紹了Spring boot 整合 Okhttp3 并封裝請求工具,需要的朋友可以參考下2023-08-08
Spring boot搭建web應(yīng)用集成thymeleaf模板實現(xiàn)登陸
這篇文章主要介紹了Spring boot搭建web應(yīng)用集成thymeleaf模板實現(xiàn)登陸,頁面使用bootstrap,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

