Java的socket服务UDP协议

时间:2023-03-08 21:51:09

练习1

接收类

package com.socket.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket; public class UDPReceiveDemo { public static void main(String[] args) throws IOException{
System.out.println("接收端启动…………");
/*
2、建立UDP的socket的服务,必须明白一个端口号
3、创建数据包。用于储存接收到的数据,方便用数据包对象的方法解析这些数据
4、使用DatagramSocket的receive方法将接收到的数据存储到数据包中
5、通过数据包的方法解析数据包中的数据
5、关闭socket服务
*/ //udpsocket服务,使用DatagramSocket对象
DatagramSocket ds=new DatagramSocket(10002); //使用DatagramPacket将数据封装到该对象中
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
//通过udp的socket服务将数据包发送出去,通过send方法
ds.receive(dp);
//通过数据包的方法解析数据包中的数据,比方。地址、端口、数据内容等
String ip=dp.getAddress().getHostAddress();
//String name=dp.getAddress().getHostName();
int port=dp.getPort();
String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text);
System.out.println("-----"+ip+"----------"+port+"-----"+text);
//关闭资源
ds.close();
} }

发送类

package com.socket.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException; public class UDPSendDemo { public static void main(String[] args) throws IOException{
System.out.println("发送端启动…………");
/*
* 1、创建udp传输的发送端
2、建立UDP的socket的服务
3、将要发送的数据封装到数据包中
4、通过udp的socket服务将数据包发送出去
5、关闭socket服务
*/ //udpsocket服务。使用DatagramSocket对象
DatagramSocket ds=new DatagramSocket(8888);//监听端口 //将要发送的数据封装到数据包中
String str="udp传输演示。go";
//使用DatagramPacket将数据封装到该对象中
byte[] buf=str.getBytes();
DatagramPacket dp=
new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10002);
//通过udp的socket服务将数据包发送出去,通过send方法
ds.send(dp); //关闭资源
ds.close();
} }

练习2

接收类:

package com.socket.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket; public class UDPReceiveDemo2 { public static void main(String[] args) throws IOException{
System.out.println("接收端启动…………");
/*
2、建立UDP的socket的服务,必须明白一个端口号
3、创建数据包,用于储存接收到的数据,方便用数据包对象的方法解析这些数据
4、使用DatagramSocket的receive方法将接收到的数据存储到数据包中
5、通过数据包的方法解析数据包中的数据
5、关闭socket服务
*/ //udpsocket服务。使用DatagramSocket对象
DatagramSocket ds=new DatagramSocket(10003); while(true){
//使用DatagramPacket将数据封装到该对象中
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
//通过udp的socket服务将数据包发送出去,通过send方法
ds.receive(dp);//堵塞式的。 //通过数据包的方法解析数据包中的数据,比方,地址、端口、数据内容等
String ip=dp.getAddress().getHostAddress();
//String name=dp.getAddress().getHostName();
int port=dp.getPort();
String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"-----"+name+"-----"+port+"-----"+text);
System.out.println("-----"+ip+"----------"+port+"-----"+text);
}
//关闭资源
//ds.close();
}
}

发送类:

package com.socket.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress; public class UDPSendDemo2 { public static void main(String[] args) throws IOException{
System.out.println("发送端启动…………");
/*
* 1、创建udp传输的发送端
2、建立UDP的socket的服务
3、将要发送的数据封装到数据包中
4、通过udp的socket服务将数据包发送出去
5、关闭socket服务
*/ //udpsocket服务,使用DatagramSocket对象
DatagramSocket ds=new DatagramSocket(9999);//监听端口 //将要发送的数据封装到数据包中
//String str="udp传输演示。go";
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));//键盘输入
String line=null;
//使用DatagramPacket将数据封装到该对象中
while((line=bufr.readLine())!=null){
byte[] buf=line.getBytes();//
DatagramPacket dp=
new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.100"),10003);
//通过udp的socket服务将数据包发送出去。通过send方法
ds.send(dp);
if("886".equals(line)){
break;
}
}
//关闭资源
ds.close();
}
}

执行效果图:

接收

Java的socket服务UDP协议

发送

Java的socket服务UDP协议