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 编程我应该知道什么?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
