udp 概述
user datagram protocol , 用户数据报协议,
无连接的协议,发送端无法得知发送结果,
特点:
高效,
稳定性 & 可靠性 差,
udp 相关类:SocketAddress,DatagramPacket,DatagramSocket
------
1
SocketAddress
表示 socket 端口,即 ip:port,
SocketAddress 是 虚类,其实现类为 InetSocketAddress,
如:
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 29191);
------
2
DatagramPacket
用于表示 数据包,
属性:
* data
byte[] , 用于存储数据,包括 接受 和 发送的数据,
* address & port
表示了其 socket 地址,
* ...
*
如:
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 29191);
byte[] bsSd = { 12, 43, 67 };
DatagramPacket dpSd = new DatagramPacket(bsSd, bsSd.length);
dataPacket.setSocketAddress(isa);
------
3
DatagramSocket
用于 发送 & 接收 数据,
DatagramSocket 中数据的存储
DatagramSocket 使用 DatagramPacket 存储数据,包括 发送 和 接收,
方法:
* send()
发送
* receive()
接收
*
如:
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 29191);
// sender
byte[] bsSd = { 12, 43, 67 };
DatagramPacket dpSd = new DatagramPacket(bsSd, bsSd.length);
DatagramSocket dsSd = new DatagramSocket();
dpSd.setSocketAddress(isa);
dsSd.send(dpSd);
System.out.println("send:" + dpSd.getData()[0] + " ... ," + new Date());
------
例子:
*
发送数据
package eric.udp.test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Date;
import java.util.Random;
public class UdpSender implements Runnable {
private SocketAddress target;
private DatagramPacket dataPacket;
private DatagramSocket dataSender;
public UdpSender() {}
public UdpSender(SocketAddress target, DatagramPacket dataPacket, DatagramSocket dataSender) {
this.target = target;
this.dataPacket = dataPacket;
this.dataSender = dataSender;
}
@Override
public void run() {
Random rd = new Random();
try {
while (true) {
Thread.sleep(1000);
dataPacket.getData()[0] = (byte) rd.nextInt(127);
dataPacket.setSocketAddress(target);
dataSender.send(dataPacket);
System.out.println("send:" + dataPacket.getData()[0] + " ... ," + new Date());
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
byte[] bs = { 12, 43, 67 };
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 29191);
DatagramPacket dp = new DatagramPacket(bs, bs.length);
DatagramSocket ds = new DatagramSocket();
UdpSender sender = new UdpSender(isa, dp, ds);
Thread t = new Thread(sender);
t.start();
} catch (SocketException e) {
e.printStackTrace();
}
}
public SocketAddress getTarget() {
return target;
}
public void setTarget(SocketAddress target) {
this.target = target;
}
public DatagramPacket getDataPacket() {
return dataPacket;
}
public void setDataPacket(DatagramPacket dataPacket) {
this.dataPacket = dataPacket;
}
public DatagramSocket getDataSender() {
return dataSender;
}
public void setDataSender(DatagramSocket dataSender) {
this.dataSender = dataSender;
}
}
*
接收数据
package eric.udp.test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Date;
public class UdpReceiver implements Runnable {
private SocketAddress source;
private DatagramPacket dataPacket;
private DatagramSocket dataReceiver;
public UdpReceiver() {}
public UdpReceiver(SocketAddress target, DatagramPacket dataPacket, DatagramSocket dataSender) {
this.source = target;
this.dataPacket = dataPacket;
this.dataReceiver = dataSender;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(50);
dataPacket.setSocketAddress(source);
// 该方法将阻塞线程,不断监听,直到收到数据
dataReceiver.receive(dataPacket);
System.out.println("receiver:" + dataPacket.getData()[0] + " ... ," + new Date());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
byte[] bs = new byte[100];
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 29191);
DatagramPacket dp = new DatagramPacket(bs, bs.length);
DatagramSocket ds = new DatagramSocket();
UdpReceiver receiver = new UdpReceiver(isa, dp, ds);
Thread t = new Thread(receiver);
t.start();
} catch (SocketException e) {
e.printStackTrace();
}
}
public SocketAddress getSource() {
return source;
}
public void setSource(SocketAddress source) {
this.source = source;
}
public DatagramPacket getDataPacket() {
return dataPacket;
}
public void setDataPacket(DatagramPacket dataPacket) {
this.dataPacket = dataPacket;
}
public DatagramSocket getDataReceiver() {
return dataReceiver;
}
public void setDataReceiver(DatagramSocket dataReceiver) {
this.dataReceiver = dataReceiver;
}
}
------