简体   繁体   English

为什么在UDP套接字通信中不会出现错误?

[英]Why doesn't error occur in UDP socket communication?

I am a student studying computer networking. 我是一名学习计算机网络的学生。 I made very simple server-client socket communication program in C on Ubuntu, using UDP protocol. 我使用UDP协议在Ubuntu上的C中制作了非常简单的服务器-客户端套接字通信程序。 I read in the book that UDP is unreliable data transferring protocol while TCP is reliable data transferring protocol. 我在书中读到,UDP是不可靠的数据传输协议,而TCP是可靠的数据传输协议。 The reason is that UDP is just sending packets while TCP is sending and waiting for receiving corresponding ack message. 原因是UDP只是在发送数据包,而TCP正在发送并等待接收相应的ack消息。

Anyhow, I tried many times to send an image file (around 1Mb) from client to server using UDP socket. 无论如何,我尝试了很多次使用UDP套接字从客户端向服务器发送图像文件(大约1Mb)。 However, the image file was transmitted successfully without any one failure. 但是,图像文件已成功传输,没有任何失败。

Does socket library have error correction scheme although UDP setting? 尽管UDP设置,套接字库是否具有错误纠正方案?

ps) My program is almost the same as the common echo socket code in C. Just different thing is not echoing but copying. ps)我的程序与C中的通用回显套接字代码几乎相同。不同的是,不是回显而是复制。 That is, client transfers data and server receives and copies it. 即,客户端传输数据,服务器接收并复制数据。

For reference) I used sys/socket header for socket functions. 供参考)我将sys / socket标头用于套接字函数。 I used fopen, fread and fwrite for copying file. 我用fopen,fread和fwrite复制文件。

Anyhow, I tried many times to send an image file (around 1Mb) from client to server using UDP socket. 无论如何,我尝试了很多次使用UDP套接字从客户端向服务器发送图像文件(大约1Mb)。 However, the image file was transmitted successfully without any one failure. 但是,图像文件已成功传输,没有任何失败。

If done on a single host via loopback no packet will be lost (except if you enable some packet loss testing function). 如果通过环回在单个主机上完成,则不会丢失任何数据包(除非您启用了某些丢包测试功能)。 That is because modern OSs' network layer only passes around pointers to packet descriptors. 这是因为现代OS的网络层仅传递指向数据包描述符的指针。 So when you do a send() or write() on a socket a packet descriptor struct is constructed around your data; 因此,当您在套接字上执行send()write() ,便会围绕数据构造一个数据包描述符结构; that struct kind of "lives" in the writing process and only a pointer to it is passed to whoever is going to receive it. 这种结构在编写过程中“存在”,并且只有指向它的指针才传递给将要接收它的人。 If it's another process this results effectively in IPC (and if you use the right socket operations this goes even as far as a zero-copy data transfer). 如果这是另一个过程,则可以有效地实现IPC(如果使用正确的套接字操作,则甚至可以达到零复制数据传输)。 Only if that packet descriptor ends up within a network interface driver, more than just that pointer will be passed around. 仅当该数据包描述符在网络接口驱动程序中结束时,才传递比该指针更多的指针。

On a local network it's also next to impossible to loose a packet, because collision detection happens on the link layer and modern switches are store and forward architectures. 在本地网络上,几乎不可能丢失数据包,因为冲突检测发生在链路层,而现代交换机是存储转发结构。 It takes a huge amount of network load for noticing any form of packet loss back pressure. 这需要庞大的网络负载的量用于通知任何形式的分组丢失的背压。

For packet loss to become noticeable you must either use a highly contended shared medium network (say, W-LAN with a lot of clients contending for it) or go through at least one router close to its bandwidth capacity. 为了使数据包丢失变得明显,您必须使用竞争激烈的共享介质网络(例如,有很多客户端在争夺它的W-LAN)或通过至少一个接近其带宽容量的路由器。

Does socket library have error correction scheme although UDP setting? 尽管UDP设置,套接字库是否具有错误纠正方案?

No. 没有。

There is no error correction in UDP. UDP中没有错误纠正。 It is still unreliable, even though you succeeded many time to transfer a file using it. 即使您成功使用该文件传输文件很多次,它仍然是不可靠的。

The probability of error is very small in today's networks, especially in a LAN or on the same machine. 在当今的网络中,尤其是在LAN或同一台计算机上,错误的可能性非常小。 That is, you may need to try sending a file millions of times before you get an error. 也就是说,您可能需要尝试发送数百万次文件才能收到错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM