概念:用户数据报传输协议
特点:传输数据量小(64K),不一定传输成功,没有明确的客户端与服务端的概念,数据不会通过输入输出流传输,而是会通过打包成数据报,,再传输入通道
DatagramSocket
一 、构造方法
(1)DatagramSocket()
绑定到本地地址和一个随机的端口号
(2)DatagramSocket(int port)
绑定本地地址和一个特定的端口号
(3)DatagramSocket(int port, InetAddress iad)
绑定到特定的端口号及指定地址
(4)DatagramSocket(SocketAddress sad)
绑定指定地址和随机端口号
二、基本方法
(1)close()
关闭套接字
(2)recevie(DatagramPacket dp)
接受数据报
(3)send(DatagramPacket dp)
发送数据报
DatagramPacket
一、接受类型
(1)DatagramPacket(byte[] buf, int length)
用来接受长度为length的buf数据(即数据存于字节数组buf中)
二、发送类型
(1)DatagramPacket(byte[] buf, int length, InetAddress address, int port)
将length长的buf数据发送到指定的地址的端口号处
(2)DatagramPacket(byte[] buf, int length, SocketAddress address)
将length长的buf数据发送到指定的套接字地址处
以下多线程下UDP通信代码示例
发送类:
package com.example.demo.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket datagramSocket =null;
BufferedReader bufferedReader =null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort,String toIP,int toPort){
this.fromPort=fromPort;
this.toIP=toIP;
this.toPort=toPort;
try {
datagramSocket = new DatagramSocket(this.fromPort);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
@Override
public void run() {
try {
//控制台读取数据
while (true){
String data = bufferedReader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toIP,this.toPort));
datagramSocket.send(datagramPacket);
}
} catch (SocketException e) {
throw new RuntimeException("发送错误");
} catch (IOException e) {
throw new RuntimeException("信息读取错误");
}
}
}
接受类:
package com.example.demo.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
DatagramSocket datagramSocket =null;
private int formPort;
private String user;
public TalkReceive(int formPort,String user){
this.formPort=formPort;
this.user=user;
try {
datagramSocket=new DatagramSocket(this.formPort);
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
@Override
public void run() {
while (true){
try {
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData();
String s = new String(data, 0, datagramPacket.getLength());
System.out.println(this.user+":"+s.trim());
if (s.contains("bye")){
break;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
datagramSocket.close();
}
}
以下定义两个用户:
老师:
package com.example.demo.chat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(7777,"学生")).start();
}
}
学生:
package com.example.demo.chat;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(6666,"localhost",7777)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
以上测试均为控制台输入;文章来源:https://www.toymoban.com/news/detail-642805.html
完结撒花!文章来源地址https://www.toymoban.com/news/detail-642805.html
到了这里,关于JAVA中的UDP通信的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!