import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
public static void main(String[] args) throws IOException {
System.out.println("发送端启动......");
// 1、创建UDP的Socket,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
// 2、将要发送的数据封装到数据包中
String str = "UDP传输演示:I'm coming!";
byte[] buf = str.getBytes(); //使用DatagramPacket将数据封装到该对象的包中
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);
// 3、通过UDP的Socket服务将数据包发送出去,使用send方法
ds.send(dp);
// 4、关闭Socket服务
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String[] args) throws IOException {
System.out.println("接收端启动......");
// 1、建立UDP的Socket服务
DatagramSocket ds = new DatagramSocket(10000);
// 2、创建数据包
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 3、使用接收方法将数据存储到数据包中
ds.receive(dp); // 该方法为阻塞式的方法
// 4、通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
// 5、关闭Socket服务
ds.close();
}
}
/**
*优化UDP传输的发送端
*/
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 UDPSend {
public static void main(String[] args) throws IOException {
System.out.println("发送端启动......");
// 创建UDP的Socket,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
// 使用DatagramPacket将数据封装到该对象的包中
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);
// 通过UDP的Socket服务将数据包发送出去,使用send方法
ds.send(dp);
// 如果输入信息为over,则结束循环
if ("over".equals(line))
break;
}
// 关闭Socket服务
ds.close();
}
}
/**
*优化UDP传输的接收端
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String[] args) throws IOException {
System.out.println("接收端启动......");
// 建立UDP的Socket服务
DatagramSocket ds = new DatagramSocket(10000);
while(true) {
// 创建数据包
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 使用接收方法将数据存储到数据包中
ds.receive(dp); // 该方法为阻塞式的方法
// 通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
}
}
}
/**
*创建UDP传输下的聊天室发送端
*/
package chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this.ds = ds;
}
public void run() {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.255"), 10001);
ds.send(dp);
if ("886".equals(line))
break;
}
ds.close();
} catch (Exception e) {
System.out.println("对不起,发生错误啦!");
}
}
}
/**
*创建UDP传输下的聊天室接收端
*/
package chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {
this.ds = ds;
}
public void run() {
try {
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + ":::" + text);
if(text.equals("886")){
System.out.println(ip+"......退出聊天室!");
}
}
} catch (Exception e) {
System.out.println("对不起,发生错误啦!");
}
}
}
/**
*创建UDP传输下的聊天室
*/
package chat;
import java.io.IOException;
import java.net.DatagramSocket;
public class ChatRoom {
public static void main(String[] args) throws IOException {
DatagramSocket send = new DatagramSocket();
DatagramSocket rece = new DatagramSocket(10001);
new Thread(new Send(send)).start();
new Thread(new Rece(rece)).start();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有