服务器端程序,利用DatagramSocket负责监听端口,当客户端发过来消息时,服务器端就会响应,并将消息内容保存到Datagrampacket对象中,并且!每一次while循环必须重新创建DatagramPacket对象用于保存消息数据。并将socket,packet对象发送给子线程,由子线程完成后面的事务,主线程将进行下一次循环,在receive(packet)处阻塞监听客户端的响应。
package UDPSocketTell;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServerThreadText {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DatagramSocket socket = new DatagramSocket(8899);
byte[] infoBytes = new byte[1024];
int num = 0;
System.out.println("服务器端启动了·········");
while (true) {
DatagramPacket packet = new DatagramPacket(infoBytes, infoBytes.length);
socket.receive(packet);
UdpServerThread thread = new UdpServerThread(packet, socket, infoBytes);
thread.start();
System.out.println(thread);
System.out.println("访问的客户端数量:" + (num++));
}
}
}
在子线程中,由传过来的socket和packet对象获取到客户端传过来的消息数据、端口号和InetAddress地址,将消息数据进行输出,并对客户端进行响应。
package UDPSocketTell;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpServerThread extends Thread {
private String info = null;
private DatagramSocket socket = null;
private int port = 0;
private InetAddress address = null;
public UdpServerThread(DatagramPacket packet2, DatagramSocket socket2, byte[] infoBytes2) {
// TODO Auto-generated constructor stub
socket = socket2;
info = new String(infoBytes2, 0, packet2.getLength());
port = packet2.getPort();
address = packet2.getAddress();
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("客户端说:" + info);
byte[] infoBytes = "你好!我是服务器~".getBytes();
DatagramPacket packet = new DatagramPacket(infoBytes, infoBytes.length, address, port);
try {
socket.send(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码,只需要用DatagramPacket packet = new DatagramPacket(infoBytes, infoBytes.length, address, 8899);保存要发送的数据,指出要发送的目标服务器的地址InetAddress,并用socket.send(packet);发出消息即可。新建一个packet对象,使用socket.receive(packet2);接收服务器的响应。
package UDPSocketTell;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* 客户端,基于UDP实现Socket通信
*
* @author 杰仔
*
*/
public class UDPclient {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] infoBytes = "用户名2:吴俊杰;密码2:123456".getBytes();// 创建发送数据的字节数组
InetAddress address;
try {
/*
* 发送数据包
*/
address = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(infoBytes, infoBytes.length, address, 8899);
DatagramSocket socket = new DatagramSocket();
System.out.println("客户端准备发送数据了!");
socket.send(packet);
/*
* 接收数据包响应
*/
byte[] Bytes = new byte[1024];
DatagramPacket packet2 = new DatagramPacket(Bytes, Bytes.length);
System.out.println("**********客户端已经启动************");
socket.receive(packet2);
String info = new String(Bytes, 0, packet2.getLength());
System.out.println("自己的packeg的端口号:" + packet.getPort());
System.out.println("客户端发来消息长度为 " + packet2.getLength() + " 的消息:" + info + " 消息端口号为:" + packet2.getPort());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}