import java.net.*;
class UdpSent
{
public static void main(String[] args) throws Exception
{
//1,创建udp服务,通过DatagramSocket对象。
DatagramSocket ds = new DatagramSocket(8888);
//2,确定数据,并封装成数据包,
//DatagramPacket(byte[] buf,int length,InetAddress address,int port)
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.101"),10000);
// 3,通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//4,关闭资源。
ds.close();
}
}
/*
需求:
定义一个应用程序,用于接收udp协议传输的数据并处理
思路;
1,定义udpsocket服务
2,定义一个数据包,因为要存储接收到的字节数据。
因为数据包对象中有更多功能可以提取字节数据中的不同数据信息,
3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中。
4,通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上。
5,关闭资源。
*/
class UdpRece
{
public static void main(String[] args) throws Exception
{
//1,创建udp socket,建立端点。监听10000端口
DatagramSocket ds = new DatagramSocket(10000);
//服务不用循环建立,会出现绑定异常BindException,Address already in use
while(true)
{
//2,定义数据包,用于存储数据。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3,通过服务的receive方法将收到数据存入数据包中
//阻塞式方法
ds.receive(dp);
//4,通过数据包的方法获取其中的数据。
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
}
//5,关闭资源
//ds.close();
}
}
class UdpSent
{
public static void main(String[] args) throws Exception
{
//1,创建udp服务,通过DatagramSocket对象。
DatagramSocket ds = new DatagramSocket(8888);
//2,确定数据,并封装成数据包,
//DatagramPacket(byte[] buf,int length,InetAddress address,int port)
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp =
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.101"),10000);
// 3,通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//4,关闭资源。
ds.close();
}
}
/*
需求:
定义一个应用程序,用于接收udp协议传输的数据并处理
思路;
1,定义udpsocket服务
2,定义一个数据包,因为要存储接收到的字节数据。
因为数据包对象中有更多功能可以提取字节数据中的不同数据信息,
3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中。
4,通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上。
5,关闭资源。
*/
class UdpRece
{
public static void main(String[] args) throws Exception
{
//1,创建udp socket,建立端点。监听10000端口
DatagramSocket ds = new DatagramSocket(10000);
//服务不用循环建立,会出现绑定异常BindException,Address already in use
while(true)
{
//2,定义数据包,用于存储数据。
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3,通过服务的receive方法将收到数据存入数据包中
//阻塞式方法
ds.receive(dp);
//4,通过数据包的方法获取其中的数据。
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
}
//5,关闭资源
//ds.close();
}
}