冲洗插座是什么意思? [英] What does it mean to flush a socket?

查看:93
本文介绍了冲洗插座是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了如何像对待文件一样对套接字进行读写外,我对套接字并不太了解.我对使用套接字选择器有所了解.我不明白为什么您必须冲洗套接字,那里实际上发生了什么?这些位只是挂在内存中的某个位置,直到被推下?我在网上阅读了一些有关套接字的内容,但这都是非常抽象和高级的.

I don't really know much about sockets except how to read and write to them as if they were files. I know a little about using socket selectors. I don't get why you have to flush a socket, what's actually happening there? The bits just hang out somewhere in memory until they get pushed off? I read some things online about sockets, but it's all very abstract and high level.

这里实际上发生了什么?

What's actually happening here?

推荐答案

写入网络套接字和发送数据涉及一定的开销.如果每当字节进入套接字时就发送数据,那么对于实际数据的每个字节,您最终都会得到40多个字节的TCP标头. (当然,假设您使用的是TCP套接字.其他套接字将具有不同的值).为了避免这种低效率,套接字会维护一个本地缓冲区,该缓冲区通常略微超过1000个字节.当缓冲区被填满时,将在数据周围包头,然后将数据包发送到其目的地.

There's a certain amount of overhead involved in writing to a network socket and sending data. If data were sent every time byte entered the socket, you'd end up with 40+ bytes of TCP header for every byte of actual data. (Assuming you're using a TCP socket, of course. Other sockets will have different values). In order to avoid such inefficiency, the socket maintains a local buffer, which is usually somewhat over 1000 bytes. When that buffer is filled, a header is wrapped around the data and the packet is sent off to its destination.

在许多情况下,您不需要立即发送每个数据包;如果您要传输文件,则没有文件的最终数据可能无法使用早期数据,因此效果很好.但是,如果需要强制立即发送数据,则刷新缓冲区将发送尚未发送的所有数据.

In many cases, you don't need each packet to be sent immediately; if you're transferring a file, early data may not be of any use without the final data of the file, so this works well. If you need to force data to be sent immediately, however, flushing the buffer will send any data which has not yet been sent.

请注意,当您关闭套接字时,套接字会自动刷新所有剩余的数据,因此无需在关闭前刷新.

Note that when you close a socket, it automatically flushes any remaning data, so there's no need to flush before you close.

这篇关于冲洗插座是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆