首先先将UDP基本步骤列出
public class testUdpClient {
/*发送方:
* 1.使用DatagramSocket 指定端口,创建发送端
* 2.准备数据,转换为字节数组
* 2.封装成DatagramPacket包裹,指定目的地端口
* 3.发送包裹send(DatagramPacket p)
* 4.释放资源*/
public static void main(String[] args) throws IOException {
System.out.println("发送方启动");
DatagramSocket client=new DatagramSocket(8888);
String data="四毛傻曈";
byte[] datas=data.getBytes();
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999));
client.send(packet);
client.close();
}
}
public class testUdpServer {
/*接收方:
* 1.使用DatagramSocket 指定端口,创建接收端
* 2.准备容器 封装成DatagramPacket包裹
* 3.阻塞式接受包裹receive(DatagramPacket p)
* 4.分析数据 getData(),getLength()
* 数据都为byte【】
* 5.释放资源*/
public static void main(String[] args) throws IOException {
System.out.println("接收方启动");
DatagramSocket server=new DatagramSocket(9999);
byte[]container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
server.receive(packet);
byte[]datas=packet.getData();
System.out.println(new String(datas));
}
}
可以使用循环构建一个简单的聊天代码
发送方:
public class testUDPtalkCilent {
public static void main(String[] args) throws IOException {
System.out.println("发送方启动");
DatagramSocket client=new DatagramSocket(8888);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
while(true){//循环读取键盘输入!
String data=bf.readLine();
byte datas[]= data.getBytes();
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999));
client.send(packet);
if(data.equals("close")){
break;
}
}
client.close();//最后在关闭插座
}
}
接收方:
public class testUDPtalkServer {
public static void main(String[] args) throws IOException {
System.out.println("接收方启动");
DatagramSocket server=new DatagramSocket(9999);
while(true){
byte[]container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
server.receive(packet);
byte[]datas=packet.getData();
String d=new String(datas);
System.out.println(d);
if (d.equals("close")) {
break;}
}
server.close();
}
}
还可以改造成多线程实现类之间相互发送与接收:
发送类:(就是将上面的发送方代码封装)
public class send implements Runnable{
private DatagramSocket client;
private BufferedReader bf;
private int toport;
private int port;
public send(int port,int toport) throws SocketException {
this.port=port;
this.toport=toport;
client=new DatagramSocket(port);
bf=new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while(true){
String data= null;
try {
data = bf.readLine();
byte datas[]= data.getBytes();
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",toport));
client.send(packet);
if(data.equals("close")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
} client.close();
}
}
接收类:(将上面接收方封装)
public class receive implements Runnable{
DatagramSocket server;
public receive(int port) throws SocketException {
server =new DatagramSocket( port);
}
@Override
public void run() {
while(true){
byte[]container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
try {
server.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
byte[]datas=packet.getData();
String d=new String(datas);
System.out.println("对方:"+d);
if (d.equals("close")) {
break;}
}
server.close();
}
}
最后定义两个类学生老师实现多线程:
public class student {
public static void main(String[] args) throws SocketException {
new Thread(new send(8888,9999)).start();
new Thread(new receive(7777)).start();
}
}
public class teacher {
public static void main(String[] args) throws SocketException {
new Thread(new receive(9999)).start();
new Thread(new send(6666,7777)).start();
}
}