一、Udp通信基础
1、客户端
//不需要连接服务器
public class UdpClientDemo1 {
public static void main(String[] args) throws IOException {
//1、创建套接字,用于通信
DatagramSocket socket = new DatagramSocket();
//定义包的内容
String msg="Udp练习";
InetAddress localhost= InetAddress.getByName("localhost");
int port=8010;
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, localhost,port);
//2、发送包
socket.send(datagramPacket);
socket.close();
}
}
2、服务端
//仍然需要等待客户端的连接
public class UdpServerDemo1 {
public static void main(String[] args) throws IOException {
//1、创建套接字,开放端口
DatagramSocket socket = new DatagramSocket(8010);
//创建用于接收发送包的包
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
//2、接收数据包
socket.receive(datagramPacket);//阻塞接收
System.out.println(new String(bytes));
socket.close();
}
}
3、总结
- Udp方式中,客户端不需要连接服务端,只需要把想发送的东西放到一个packet中,丢给对应ip及端口就ok
- 服务端任然需要在客户端之前开启,其receive()方法仍然是一个阻塞式监听
- 客户端创建一个packet用于发送,服务端创建一个packet用于接收
- 关于packet的构建方法说明:
public DatagramPacket(byte buf[], int offset, int length) {
setData(buf, offset, length);
this.address = null;
this.port = -1;
}
public DatagramPacket(byte buf[], int length) {
this (buf, 0, length);
}
public DatagramPacket(byte buf[], int offset, int length,
InetAddress address, int port) {
setData(buf, offset, length);
setAddress(address);
setPort(port);
}
public DatagramPacket(byte buf[], int offset, int length, SocketAddress address) {
setData(buf, offset, length);
setSocketAddress(address);
}
public DatagramPacket(byte buf[], int length,
InetAddress address, int port) {
this(buf, 0, length, address, port);
}
public DatagramPacket(byte buf[], int length, SocketAddress address) {
this(buf, 0, length, address);
}
DatageamPacket的构造方法有很多,只需分清楚一点:
如果构造方法中参数包含ip和端口,则这个包就是用于发送的包,其中的byte[]是用于发送的数据
如果构造方法中参数没有ip和端口,则这个包是用于接收的包,其中的byte[]是用于接收数据
二、Udp实现聊天
1、发送端
public class UdpSenderDemo2 {
public static void main(String[] args) throws IOException {
//创建套接字
DatagramSocket datagramSocket = new DatagramSocket();
while (true) {
//创建一个从控制台输入的字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String sendString = bufferedReader.readLine();
byte[] sendByte = sendString.getBytes();
//创建Udp的发送包
DatagramPacket datagramPacket = new DatagramPacket(sendByte, 0, sendByte.length, InetAddress.getByName("localhost"), 6666);
//发送包
datagramSocket.send(datagramPacket);
//输入"bye"时,停止发送
if(sendString.equals("bye")){
break;
}
}
datagramSocket.close();
}
}
2、接收端
public class UdpReceiveDemo2 {
public static void main(String[] args) throws IOException {
//创建套接字和监测端口
DatagramSocket datagramSocket = new DatagramSocket(6666);
while (true) {
//创建用于接收的包
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
//接收套接字中包含的包,数据写入接收包中的byte数组
datagramSocket.receive(datagramPacket);
//转为字符串
String st = new String(bytes,0,bytes.length).trim();
//停止监测条件
if (st.equals("bye")) {
break;
}else{
System.out.println(st);
}
}
datagramSocket.close();
}
}
3、总结
- 在发送端和接收端均加了while(true)循环,能够循环发送和接收
- 调试代码过程中遇到的String.equals()方法的坑:
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
//接收套接字中包含的包,数据写入接收包中的byte数组
datagramSocket.receive(datagramPacket);
//错误:
String st = new String(bytes,0,bytes.length);
st.equlas("bye");//flase
//正确:
String st = new String(bytes,0,bytes.length).trim();
st.equlas("bye");//true
- equals()结果分析:
- String类中方法比较先比较长度,如果长度不一致,则return false
- 接收端接收信息,使用了一个1024长度的byte数组,当一个“bye”输入进来时,前三位赋值,而后面均没有赋值,虽然打印后仍然是“bye”,但此时的长度仍然为1024,因此st.equals(“bye”)为false
- String.trim()方法可以去除字符串前后存在的空字符,将长度减为3,因此返回值为true