效果图:
多线程服务器
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPMutiThreadServer implements Runnable {
DatagramPacket datagramPacket; //数据报包
byte[] data1; //存放数据的变量
public UDPMutiThreadServer(DatagramPacket datagramPacket,byte[] data1) {
this.datagramPacket = datagramPacket;
this.data1=data1;
}
public static void main(String[] args) throws SocketException {
int count=0; //用于统计客户端数量
DatagramSocket datagramSocket = new DatagramSocket(1234);
System.out.println("```````服务器已经启动,等待客户端发送数据````````");
while (true) {
/**
* 创建数据报包,用于保存收发的数据,需要定义数据包的大小和长度
*/
byte[] data = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(data,data.length);
try {
datagramSocket.receive(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
}
if (datagramPacket.getLength() > 0) {
new Thread(new UDPMutiThreadServer(datagramPacket,data)).start();
count++;
System.out.println("客户端的数量为:" + count);
}
}
}
@Override
public void run() {
// byte[] data=new byte[1024];
String info = new String(data1,0,datagramPacket.getLength());
System.out.println("我是服务器,客户端发送的信息是:"+ info);
/**
* 响应客户端数据
*
* 从接收的数据包中获取客户端的地址和端口
*/
InetAddress address = datagramPacket.getAddress();
int port = datagramPacket.getPort();
//定义响应数据
byte[] data2 = "服务器已经收到你的信息,谢谢!".getBytes();
//用DatagramPacket把要发送的数据进行打包
DatagramPacket datagramPacket1 = new DatagramPacket(data2,data2.length,address,port);
//用datagramSocket把数据包发送给客户端
try {
DatagramSocket datagramSocket= new DatagramSocket();
datagramSocket.send(datagramPacket1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
import java.io.IOException;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException {
/**
* 向服务器发送数据
*
* 定义服务器地址,端口号和数据
*/
InetAddress address = InetAddress.getByName("localhost");
int port=1234;
byte[] data ="我是:admin123,密码:123456546".getBytes();
/**
* 定义一个DatagramPacket,把要发送的数据时行打包
*/
DatagramPacket datagramPacket = new DatagramPacket(data,data.length,address,port);
/**
* 定义一个DatagramSocket,用于发送和接收数据
*/
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(datagramPacket);
/**
* 接收服务器发送的数据
*/
//定义数据包的容量
byte[] daaa= new byte[1024];
DatagramPacket datagramPacket1 = new DatagramPacket(daaa,daaa.length);
//用DatagramSocket接收数据
datagramSocket.receive(datagramPacket1);
//读取数据包的内容
String info = new String(daaa,0,datagramPacket1.getLength());
System.out.println("我是客户端,你说:"+info);
datagramSocket.close();
}
}
单线程服务器
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) throws IOException {
/**
* 接收客户端数据
*
* 创建服务器端的Socket,用于收发数据,需要指定端口
*/
DatagramSocket datagramSocket = new DatagramSocket(1234);
/**
* 创建数据报包,用于保存收发的数据,需要定义数据包的大小和长度
*/
byte[] data = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(data,data.length);
System.out.println("```````服务器已经启动,等待客户端发送数据````````");
/**
* 用Socket收取从客户端发来的数据,并保存在Packet数据报包中
*/
datagramSocket.receive(datagramPacket);
String info = new String(data,0,datagramPacket.getLength());
System.out.println("我是服务器,客户端发送的信息是:"+ info);
/**
* 响应客户端数据
*
* 从接收的数据包中获取客户端的地址和端口
*/
InetAddress address = datagramPacket.getAddress();
int port = datagramPacket.getPort();
//定义响应数据
byte[] data2 = "服务器已经收到你的信息,谢谢!".getBytes();
//用DatagramPacket把要发送的数据进行打包
DatagramPacket datagramPacket1 = new DatagramPacket(data2,data2.length,address,port);
//用datagramSocket把数据包发送给客户端
datagramSocket.send(datagramPacket1);
}
}