TCP socket和web socket的区别

栏目: 服务器 · 发布时间: 7年前

内容简介:小编先习惯性的看了下某中文百科网站对Web Socket的介绍,觉得很囧。如果大家按照这个答案去参加BAT等互联网公司的前端开发面试,估计会被鄙视。还是让我们阅读一些英文材料吧。

小编先习惯性的看了下某中文百科网站对Web Socket的介绍,觉得很囧。如果大家按照这个答案去参加BAT等互联网公司的前端开发面试,估计会被鄙视。

TCP socket和web socket的区别

还是让我们阅读一些英文材料吧。

让我们直接看stackoverflow上的原文,然后翻译:

TCP socket和web socket的区别

原文地址:

https://stackoverflow.com/que...

这个讨论有超过8万的阅读量。

TCP socket和web socket的区别

首先我们来阅读这段有166个赞的回答:

TCP socket和web socket的区别

When you send bytes from a buffer with a normal TCP socket, the send function returns the number of bytes of the buffer that were sent.

当我们向一个通常的TCP套接字发送一段来自内存buffer中的字节数据时,send系统调用返回的是实际发送的字节数。

If it is a non-blocking socket or a non-blocking send then the number of bytes sent may be less than the size of the buffer.

如果发送数据的目的方套接字是一个非阻塞套接字或者是对写操作非阻塞的套接字,那么send返回的已发送字节数可能小于buffer中待发送字节数。

If it is a blocking socket or blocking send, then the number returned will match the size of the buffer but the call may block.

如果是阻塞套接字,两者会相等,因为顾名思义,如果send系统调用没有把所有待发送数据全部发送,则API调用不会返回。

With WebSockets, the data that is passed to the send method is always either sent as a whole "message" or not at all. Also, browser WebSocket implementations do not block on the send call.

而Web socket和TCP socket的区别,从发送的数据来看,不再是一系列字节,而是按照一个完整的"消息体"发送出去的,这个"消息体"无法进一步再分割,要么全部发送成功,要么压根就不发送,不存在像TCP套接字非阻塞操作那样出现部分发送的情况。换言之,Web Socket里对套接字的操作是非阻塞操作。

TCP socket和web socket的区别

这个区别在维基百科上也有清晰阐述:

Websocket differs from TCP in that it enables a stream of messages instead of a stream of bytes

再来看接收方的区别。

原文:

But there are more important differences on the receiving side of things. When the receiver does a recv (or read) on a TCP socket, there is no guarantee that the number of bytes returned correspond to a single send (or write) on the sender side. It might be the same, it may be less (or zero) and it might even be more (in which case bytes from multiple send/writes are received). With WebSockets, the receipt of a message is event driven (you generally register a message handler routine), and the data in the event is always the entire message that the other side sent.

同理,在TCP套接字的场景下,接收方从TCP套接字读取的字节数,并不一定等于发送方调用send所发送的字节数。而WebSocket呢?WebSocket的接收方从套接字读取数据,根本不是像TCP 套接字那样直接用recv/read来读取, 而是采取事件驱动机制。即应用程序注册一个事件处理函数,当web socket的发送方发送的数据在接收方应用从内核缓冲区拷贝到应用程序层已经处于可用状态时 ,应用程序注册的事件处理函数以回调(callback)的方式被调用。

看个例子:

我通过WebSocket发送一个消息“汪子熙”:

TCP socket和web socket的区别

在调试器里看到的这个字符串作为回调函数的输入参数注入到函数体内:

TCP socket和web socket的区别

Chrome开发者 工具 里观察到的WebSocket消息体:

TCP socket和web socket的区别

下次面试被面试官问到TCP和WebSocket套接字的区别,相信大家应该能够知道如何回答了。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

TCP socket和web socket的区别


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

推荐系统与深度学习

推荐系统与深度学习

黄昕、赵伟、王本友、吕慧伟、杨敏 / 清华大学出版社 / 2019-1-1 / 65.00元

本书的内容设置由浅入深,从传统的推荐算法过渡到近年兴起的深度学习技术。不管是初学者,还是有一定经验的从业人员,相信都能从本书的不同章节中有所收获。 区别于其他推荐算法书籍,本书引入了已被实践证明效果较好的深度学习推荐技术,包括Word2Vec、Wide & Deep、DeepFM、GAN 等技术应用,并给出了相关的实践代码;除了在算法层面讲解推荐系统的实现,还从工程层面详细阐述推荐系统如何搭建.一起来看看 《推荐系统与深度学习》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具