关于 UDP 编程我应该知道什么?UDP

由网友(潮男情歌)分享简介:我不是指如何连接到套接字.关于 UDP 编程我应该知道什么?I don't mean how to connect to a socket. What should I know about UDP programming?我需要担心我的套接字中的错误数据吗?我应该假设如果我发送 200 字节,我可能会分别得到 1...

我不是指如何连接到套接字.关于 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.

我应该担心另一个连接向我发送错误数据同一个端口?"

的udp的接收端如何看速率 java网络编程之TCP和UDP协议

"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.

阅读全文

相关推荐

最新文章