java编程实现基于UDP协议传输数据的方法
作者:awj3584 时间:2022-11-14 04:22:22
本文实例讲述了java编程实现基于UDP协议传输数据的方法。分享给大家供大家参考,具体如下:
UDP协议(User Datagram Protocol,用户数据报协议)不同于TCP协议,它是不可能靠的,但是它比TCP协议具有更快的传输速度,UDP发送的数据单元称为数据报,当网络传输UDP传输UDP数据报是无法保证数据能够到达目的地,也无法保证按发送的顺序到达目的地,也就是说先发送了“hello”,再发送了“world”,但接收方可能会先收到“world”,再收到“hello”,也有可能收不到数据,为什么呢?因为它是不可能靠的,在传输途中可能丢失了。但UDP比TCP跟适合与传输实时音频。下面是一个简单的UPD传输数据报的例子
服务器端:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class EchoServer {
private DatagramSocket datagramSocket;
private final int port = 8088;
public static void main(String[] args) throws SocketException {
new EchoServer().service();
}
public EchoServer() throws SocketException{
datagramSocket = new DatagramSocket(port);
System.out.println("服务器启动");
}
public String echo(String msg){
return "echo:"+msg;
}
public void service(){
while (true) {
try {
DatagramPacket packet = new DatagramPacket(new byte[512], 512);
datagramSocket.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
System.out.println(packet.getAddress()+"/"+packet.getPort()+" msg:"+msg);
packet.setData(echo(msg).getBytes());
datagramSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class EchoClient {
private String remoteHost="localhost";
private int remotePort=8088;
private DatagramSocket datagramSocket;
public EchoClient() throws SocketException{
datagramSocket = new DatagramSocket();
}
public static void main(String[] args) throws SocketException {
new EchoClient().talk();
}
public void talk(){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
InetAddress address = InetAddress.getByName(remoteHost);
while ((msg=reader.readLine())!=null) {
//发送数据报
byte [] buffer = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buffer,buffer.length, address, remotePort);
datagramSocket.send(packet);
//接收数据报
DatagramPacket inputPacket = new DatagramPacket(new byte[512], 512);
datagramSocket.receive(inputPacket);
System.out.println(new String(inputPacket.getData(), 0 , inputPacket.getLength()));
if("bye".equals(msg)){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
datagramSocket.close();
}
}
希望本文所述对大家Java程序设计有所帮助。
标签:java,UDP,传输
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA2022版本创建maven web项目的两种方式详解
2022-04-24 19:34:54
![](https://img.aspxhome.com/file/2023/5/84785_0s.jpg)
Spring AOP如何整合redis(注解方式)实现缓存统一管理详解
2023-11-19 06:09:27
Android实现WebView删除缓存的方法
2023-02-19 08:38:22
C#算法之无重复字符的最长子串
2021-05-24 21:56:59
JDK源码分析之String、StringBuilder和StringBuffer
2022-01-23 00:10:20
![](https://img.aspxhome.com/file/2023/0/68160_0s.jpg)
C#面向对象特征的具体实现及作用详解
2023-11-14 06:43:39
Android调试出现The selected device is incompatible问题解决
2023-08-11 12:58:34
![](https://img.aspxhome.com/file/2023/5/137935_0s.jpg)
浅谈Action+Service +Dao 功能
2023-01-04 13:44:20
![](https://img.aspxhome.com/file/2023/5/113405_0s.png)
SpringBoot实现埋点监控
2022-11-27 06:32:07
![](https://img.aspxhome.com/file/2023/5/116215_0s.png)
java绘制五子棋棋盘
2022-05-10 09:37:36
![](https://img.aspxhome.com/file/2023/9/81189_0s.jpg)
Android实现定时任务及闹钟
2023-05-20 13:34:45
spring-redis-session 自定义 key 和过期时间
2022-03-29 14:34:37
Android客户端post请求服务器端实例
2022-10-10 23:49:43
![](https://img.aspxhome.com/file/2023/8/93308_0s.png)
详解Java中方法重写和方法重载的6个区别
2023-11-28 13:42:18
![](https://img.aspxhome.com/file/2023/2/60072_0s.jpg)
SpringBoot配置shiro安全框架的实现
2023-09-07 19:55:38
gson对象序列化的示例
2023-11-25 08:54:28
![](https://img.aspxhome.com/file/2023/6/59956_0s.png)
Android编程设计模式之原型模式实例详解
2021-09-14 02:20:47
![](https://img.aspxhome.com/file/2023/1/128951_0s.png)
C#缩略图多路径多格式保存的实例
2021-10-29 18:12:36
java实现简易超市管理系统 附源码下载
2021-11-05 18:58:30
SpringBoot使用Spring Security实现登录注销功能
2023-01-05 13:19:54
![](https://img.aspxhome.com/file/2023/8/92538_0s.png)