UDP是面向无连接,只追求快速,不追求稳定的通信协议
这里我们模拟一下UDP传输
一、基础版
1.发送端
import java.net.*;
public class Send {
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket();
byte[] buf = "you are my superstar".getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"),10000);
ds.send(dp);
ds.close();
}
}
2、接收端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Rece {
public static void main(String[] args) throws Exception {
// 1. 创建upd socket 建立端点
DatagramSocket ds = new DatagramSocket(10000); // 接受端口10000
// 2. 定义数据包,存储数据
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length); // 接受发送数据长度的数据
// 3.通过服务的receive方法接受数据,在未接收到数据时会等待
ds.receive(dp); //接收数据包
// 4.通过数据包方法获取其中的数据
String ip = dp.getAddress().getHostAddress(); // getAddress返回的是address对象
String Data = new String(dp.getData(), 0 , dp.getLength()); //封装字符串,0-length,不然把bytes的1024都封装进去了
int port = dp.getPort();
System.out.println("ip:"+ip+" Data:"+Data+" PORT:"+port);
// 5.关闭资源
ds.close();
}
}
通信成功
二、进阶版(连续通信)
1.发送
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Scanner;
public class Send {
public static void main(String[] args) throws Exception{
DatagramSocket ds = new DatagramSocket();
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=bufr.readLine())!=null){
if(line.equals("exit")){
break;
}else {
byte[] buf = line.getBytes();
//172.20.1.34 为我的IP地址,读者需进行更改,,此处可以设置172.20.1.255广播地址,接受同网段的主机发送信息
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("172.20.1.34"),10000);
ds.send(dp);
}
}
ds.close();
}
}
2.接收
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Rece {
public static void main(String[] args) throws Exception {
// 1. 创建upd socket 建立端点
DatagramSocket ds = new DatagramSocket(10000); // 接受端口10000
while(true){
// 2. 定义数据包,存储数据
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length); // 接受发送数据长度的数据
// 3.通过服务的receive方法接受数据,在未接收到数据时会等待
ds.receive(dp); //接收数据包
// 4.通过数据包方法获取其中的数据
String ip = dp.getAddress().getHostAddress(); // getAddress返回的是address对象
String Data = new String(dp.getData(), 0 , dp.getLength()); //封装字符串,0-length,不然把bytes的1024都封装进去了
int port = dp.getPort();
System.out.println("ip:"+ip+" Data:"+Data+" PORT:"+port);
}
// 5.不关闭资源,因为服务器24小时接受数据
}
}
三、终极版(双线程)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class send3 implements Runnable{
private DatagramSocket ds;
public send3(DatagramSocket d){
this.ds = d;
}
@Override
public void run() {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
if (line.equals("exit")) {
break;
} else {
byte[] buf = line.getBytes();
//172.20.1.34 为我的IP地址,读者需进行更改,,此处可以设置172.20.1.255广播地址,接受同网段的主机发送信息
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("172.20.1.34"), 10000);
this.ds.send(dp);
}
}
this.ds.close();
}catch (Exception e){
System.out.println("发送失败");
}
}
}
class rece3 implements Runnable{
private DatagramSocket ds;
public rece3(DatagramSocket d){
this.ds = d;
}
@Override
public void run() {
try {
while(true){
// 2. 定义数据包,存储数据
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length); // 接受发送数据长度的数据
// 3.通过服务的receive方法接受数据,在未接收到数据时会等待
this.ds.receive(dp); //接收数据包
// 4.通过数据包方法获取其中的数据
String ip = dp.getAddress().getHostAddress(); // getAddress返回的是address对象
String Data = new String(dp.getData(), 0 , dp.getLength()); //封装字符串,0-length,不然把bytes的1024都封装进去了
int port = dp.getPort();
System.out.println("ip:"+ip+" Data:"+Data+" PORT:"+port);
}
}catch (Exception e){
}
}
}
public class Chat {
public static void main(String[] args) throws Exception{
DatagramSocket send = new DatagramSocket();
DatagramSocket rece = new DatagramSocket(10000);
new Thread(new rece3(rece)).start();
new Thread(new send3(send)).start();
}
}