目录
基于UDP的Socket通信
编码
SocketServerUDP
SocketThreadUDP
SocketClientUDP
演示
基于UDP的Socket通信
Socket除了支持TCP协议,也支持UDP协议。
编码
SocketServerUDP
package com.ispeasant.socket.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class SocketServerUDP {
public static void main(String[] args) {
try {
// UDP方式,采用的是DatagramSocket,与TCP是有点不一样
DatagramSocket udpSocket = new DatagramSocket(8888);
System.out.println("----UDP服务端启动----");
// UDP方式,接收客户端发送过来的数据,也是有点不一样
DatagramPacket packetClient = null;
byte[] dataClient = null;
// 为了更方便测试,我们让服务端程序一直启动
while (true) {
// 设定数据包的大小
dataClient = new byte[1024];
packetClient = new DatagramPacket(dataClient, dataClient.length);
// 接收数据
udpSocket.receive(packetClient);
// 采用多线程来处理数据
Thread thread = new Thread(new SocketThreadUDP(udpSocket, packetClient));
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
SocketThreadUDP
package com.ispeasant.socket.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SocketThreadUDP implements Runnable {
DatagramSocket socketUDP = null;
DatagramPacket packetUDP = null;
public SocketThreadUDP(DatagramSocket socketUDP, DatagramPacket packetUDP) {
this.socketUDP = socketUDP;
this.packetUDP = packetUDP;
}
@Override
public void run() {
// 客户端发过来的信息,是通过从packet里获取
String info = new String(packetUDP.getData(), 0, packetUDP.getLength());
// 加个时间戳,更好区分
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(date + "客户端:" + info);
// 因为采用的是UDP方式,不像TCP那样有握手建立通信通道,所以我们向客户端返回信息时,是需要制定ip和端口
// 而这ip和端口信息,是可以通过packet来获取
// 向客户端发送信息
String server2Client = "我是服务端" + date;
byte[] data = server2Client.getBytes();
try {
socketUDP.send(new DatagramPacket(data, data.length, packetUDP.getAddress(), packetUDP.getPort()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
SocketClientUDP
package com.ispeasant.socket.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SocketClientUDP {
public static void main(String[] args) {
// 发送给服务端的信息
String info = "你好,我是客户端";
byte[] data = info.getBytes();
try {
// 通过packet来打包数据发送给服务端
DatagramPacket clientPacket = new DatagramPacket(data, data.length, InetAddress.getByName("localhost"), 8888);
// 通过DatagramSocket负责与服务端通信
DatagramSocket socket = new DatagramSocket();
socket.send(clientPacket);
// 向服务端发送完数据后,我们可以看看服务端有没有信息返回给我们
byte[] serverData = new byte[1024];
DatagramPacket serverPacket = new DatagramPacket(serverData, serverData.length);
socket.receive(serverPacket);
// 接收到的服务端信息
String server2Client = new String(serverData,0,serverPacket.getLength());
// 加个时间戳,更好区分
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(date + ",服务端:" + server2Client);
// 关闭sokcet
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
演示
先启动SocketServerUDP程序:
接着启动SocketClientUDP程序:
我们再回到SocketServerUDP程序的控制台,看看有没有收到客户端的信息:
因此,Socket的UDP方式通信,也是成功的。
|