What should i know about UDP programming?(关于 UDP 编程我应该知道什么?)
问题描述
我不是指如何连接到套接字.关于 UDP 编程我应该知道什么?
I don't mean how to connect to a socket. What should I know about UDP programming?
- 我需要担心我的套接字中的错误数据吗?
- 我应该假设如果我发送 200 字节,我可能会分别得到 120 和 60 字节?
- 我应该担心另一个连接在同一端口上向我发送错误数据吗?
- 如果数据通常没有到达,我可能(通常)在多长时间内看不到数据(250 毫秒?1 秒?1.75 秒?)
我真正需要知道什么?
推荐答案
"我应该假设如果我发送 200 字节我可以分别得到 120 和 60 字节吗?"
"i should assume if i send 200bytes i may get 120 and 60bytes separately?"
当您发送 UDP 数据报时,您的读取大小将等于您的写入大小.这是因为 UDP 是 datagram 协议,而不是 TCP 的 stream 协议.但是,在数据包可能被路由器分段或丢弃之前,您只能写入最大 MTU 大小的数据.对于一般互联网使用,安全 MTU 为 576 字节,包括标头.
When you're sending UDP datagrams your read size will equal your write size. This is because UDP is a datagram protocol, vs TCP's stream protocol. However, you can only write data up to the size of the MTU before the packet could be fragmented or dropped by a router. For general internet use, the safe MTU is 576 bytes including headers.
我应该担心另一个连接向我发送错误数据同一个端口?"
"i should worry about another connection sending me bad data on the same port?"
你没有连接,你有一个端口.无论数据来自何处,您都会收到发送到该端口的任何数据.由您决定是否来自正确的地址.
You don't have a connection, you have a port. You will receive any data sent to that port, regardless of where it's from. It's up to you to determine if it's from the right address.
如果数据没有到达通常如何很长一段时间我(通常)可能看不到数据为(250 毫秒?1 秒?1.75 秒?)
If data doesnt arrive typically how long may i (typically) not see data for (250ms? 1 second? 1.75sec?)
数据可能永远丢失,数据可能会延迟,数据可能会乱序到达.如果其中任何一个问题困扰您,请使用 TCP. 在 UDP 之上编写可靠的协议是一项非常重要的任务,几乎所有应用程序都没有理由这样做.
Data can be lost forever, data can be delayed, and data can arrive out of order. If any of those things bother you, use TCP. Writing a reliable protocol on top of UDP is a very non trivial task and there is no reason to do so for almost all applications.
这篇关于关于 UDP 编程我应该知道什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:关于 UDP 编程我应该知道什么?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09