TCP 套接字和 Web 套接字之间的区别,再来一遍 [英] Differences between TCP sockets and web sockets, one more time

查看:29
本文介绍了TCP 套接字和 Web 套接字之间的区别,再来一遍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了尽可能地理解 TCP socket 和 websocket 之间的区别,我已经在这些问题中找到了很多有用的信息:

Trying to understand as best as I can the differences between TCP socket and websocket, I've already found a lot of useful information within these questions:

等等...

在我的调查中,我在维基百科上浏览了这句话:

In my investigations, I went through this sentence on wikipedia:

Websocket 与 TCP 的不同之处在于它启用消息流而不是字节流

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

我不完全确定它的确切含义.你的解释是什么?

I'm not totally sure about what it means exactly. What are your interpretations?

推荐答案

当您使用普通 TCP 套接字从缓冲区发送字节时,send 函数返回已发送的缓冲区的字节数.如果是非阻塞套接字或非阻塞发送,则发送的字节数可能小于缓冲区的大小.如果是阻塞套接字或阻塞发送,则返回的数字将匹配缓冲区的大小,但调用可能会阻塞.使用 WebSockets,传递给 send 方法的数据总是作为一个完整的消息"发送或根本不发送.此外,浏览器 WebSocket 实现不会阻止发送调用.

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

但是在事物的接收方有更重要的区别.当接收方在 TCP 套接字上执行 recv(或 read)时,不能保证返回的字节数对应于在 TCP 套接字上的单个发送(或写入).发送方.它可能相同,可能更少(或零),甚至可能更多(在这种情况下,接收来自多个发送/写入的字节).使用 WebSockets 时,消息的接收方是事件驱动的(您通常会注册一个消息处理程序例程),并且事件中的数据始终是对方发送的整个消息.

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 corresponds 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 recipient 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 套接字进行基于消息的通信,但是您需要一些额外的层/封装,将帧/消息边界数据添加到消息中,以便可以从各个部分重新组装原始消息.事实上,WebSockets 建立在普通的 TCP 套接字上,并使用包含每个帧大小的帧头并指示哪些帧是消息的一部分.WebSocket API 将 TCP 数据块重新组装成帧,然后在为每个消息调用一次消息事件处理程序之前组装成消息.

Note that you can do message based communication using TCP sockets, but you need some extra layer/encapsulation that is adding framing/message boundary data to the messages so that the original messages can be re-assembled from the pieces. In fact, WebSockets is built on normal TCP sockets and uses frame headers that contains the size of each frame and indicate which frames are part of a message. The WebSocket API re-assembles the TCP chunks of data into frames which are assembled into messages before invoking the message event handler once per message.

这篇关于TCP 套接字和 Web 套接字之间的区别,再来一遍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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