UDP编程
【简介】UDP:User Datagram Protocol 用户数据报协议。是一种不可靠,面向无连接的传输层 通信协议; 【特点】
<1>面向无连接 <2>不可靠传输 :UDP仅仅在数据报的头部,添加了复用和数据校验; <3>每次内容不能超过64KB <4>性能高效
【底层】
<1>Ftp协议底层udp; <2>Http协议 底层tcp;
【java实现UDP】 Java支持udp协议,对UDP进行类的封装 ①DatagramPacket 数据报包 :主要是包含要发送或者接收的数据;和对应数据的IP地址和端口号;数据报包用来实现无连接包投递服务;每条报文仅仅根据该包中包含的信息从一台机器路由到另一台机器;从一台机器发送到另一台机器的多个包可能选择不同的路由;也可能按不同的顺序到达;不对包投递做出保证;
构造函数:
<1>接收数据的数据报包:DatagramPacket(byte[] buf,int length); <2>发送数据的数据报包:DatagramPacket(byte[] buf,int length,netAddress address,int port);
<3>获取内容:getData(); <4>设置内容:setData(byte[] buf); <5>获取长度:getLength();
②DatagramSocket 用来发送和接收数据报包的套接字; 可以实现给予UDP协议进行通信,可以发送或者接收对应的数据报包;
<1>构造函数:DatagramSocket(int port); <2>接收数据报包:receive(DatagramPacket p); <3>发送数据报包:send(DatagramPacket p); <4>关闭:close();
UDP开发步骤:
<1>实例化套接字对象; <2>实例化数据报包 接收报包和发送报包; <3>发送或接收内容; <4>关闭销毁;
class TalkServer{
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(8881);
byte[] arr = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(arr,arr.length);
datagramSocket.receive(datagramPacket);
System.out.println("接收消息:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
datagramSocket.close();
}
}
class TaleClient{
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8882);
byte[] arr = "内涵段子".getBytes();
DatagramPacket datagramPacket = new DatagramPacket(arr,arr.length,InetAddress.getLocalHost(),8881);
socket.send(datagramPacket);
socket.close();
}
}
基于UDP协议实现网络聊天室
class TalkServer{
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(10010);
HashSet<Integer> hashSet = new HashSet<>();
byte[] sarr = new byte[1024];
byte[] rarr = new byte[1024];
DatagramPacket sdata = new DatagramPacket(sarr,sarr.length);
DatagramPacket rdata = new DatagramPacket(rarr,rarr.length);
String message;
System.out.println("聊天室");
while(true){
socket.receive(rdata);
if(!hashSet.contains(rdata.getPort())){
hashSet.add(rdata.getPort());
}
message = new String(rarr,0,rdata.getLength());
System.out.println("聊天室:"+message);
for(Integer port:hashSet){
sdata.setAddress(InetAddress.getLocalHost());
sdata.setPort(port);
sdata.setData(message.getBytes());
socket.send(sdata);
}
}
}
}
class TaleClient{
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(10010);
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,4,10, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(10));
tpe.execute(new Runnable() {
@Override
public void run() {
byte[] arr = new byte[1024];
DatagramPacket sdata;
try {
sdata = new DatagramPacket(arr,arr.length,InetAddress.getLocalHost(),10010);
String m;
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("我说:");
m=scanner.nextLine();
sdata.setData(m.getBytes("UTF-8"));
socket.send(sdata);
}
} catch (UnknownHostException | UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
tpe.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
byte[] arr = new byte[1024];
DatagramPacket rdata = new DatagramPacket(arr,arr.length);
while(true){
socket.receive(rdata);
System.out.println("别人说:"+new String(arr,0,rdata.getLength()));
}
}
});
}
}
class TalkClient02{
public static void main(String[] args) throws SocketException {
DatagramSocket socket = new DatagramSocket(9002);
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2,4,10,TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
tpe.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
byte[] arr = new byte[1024];
DatagramPacket sdata;
sdata = new DatagramPacket(arr,arr.length,InetAddress.getLocalHost(),10010)
String m;
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("我说:");
m=scanner.nextLine();
sdata.setData(m.getBytes("Utf-8"));
socket.send(sdata);
}
}
});
tpe.execute(new Runnable() {
@Override
public void run() {
byte[] arr = new byte[1024];
DatagramPacket rdata = new DatagramPacket(arr,arr.length);
while(true){
try {
socket.receive(rdata);
System.out.println("别人说"+new String(arr,0,rdata.getLength()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
|