溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

TCP socket和web socket的區(qū)別是什么

發(fā)布時間:2021-12-24 16:32:11 來源:億速云 閱讀:221 作者:柒染 欄目:互聯(lián)網(wǎng)科技

TCP socket和web socket的區(qū)別是什么,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。


TCP socket和web socket的區(qū)別是什么

首先我們來閱讀這段有166個贊的回答:

TCP socket和web socket的區(qū)別是什么

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.
當(dāng)我們向一個通常的TCP套接字發(fā)送一段來自內(nèi)存buffer中的字節(jié)數(shù)據(jù)時,send系統(tǒng)調(diào)用返回的是實際發(fā)送的字節(jié)數(shù)。
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.

如果發(fā)送數(shù)據(jù)的目的方套接字是一個非阻塞套接字或者是對寫操作非阻塞的套接字,那么send返回的已發(fā)送字節(jié)數(shù)可能小于buffer中待發(fā)送字節(jié)數(shù)。

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系統(tǒng)調(diào)用沒有把所有待發(fā)送數(shù)據(jù)全部發(fā)送,則API調(diào)用不會返回。

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的區(qū)別,從發(fā)送的數(shù)據(jù)來看,不再是一系列字節(jié),而是按照一個完整的"消息體"發(fā)送出去的,這個"消息體"無法進一步再分割,要么全部發(fā)送成功,要么壓根就不發(fā)送,不存在像TCP套接字非阻塞操作那樣出現(xiàn)部分發(fā)送的情況。換言之,Web Socket里對套接字的操作是非阻塞操作。

TCP socket和web socket的區(qū)別是什么

這個區(qū)別在維基百科上也有清晰闡述:
Websocket differs from TCP in that it enables a stream of messages instead of a stream of bytes

再來看接收方的區(qū)別。
原文:
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套接字讀取的字節(jié)數(shù),并不一定等于發(fā)送方調(diào)用send所發(fā)送的字節(jié)數(shù)。而WebSocket呢?WebSocket的接收方從套接字讀取數(shù)據(jù),根本不是像TCP 套接字那樣直接用recv/read來讀取, 而是采取事件驅(qū)動機制。即應(yīng)用程序注冊一個事件處理函數(shù),當(dāng)web socket的發(fā)送方發(fā)送的數(shù)據(jù)在接收方應(yīng)用從內(nèi)核緩沖區(qū)拷貝到應(yīng)用程序?qū)右呀?jīng)處于可用狀態(tài)時 ,應(yīng)用程序注冊的事件處理函數(shù)以回調(diào)(callback)的方式被調(diào)用。

看個例子:

我通過WebSocket發(fā)送一個消息“汪子熙”:

TCP socket和web socket的區(qū)別是什么

在調(diào)試器里看到的這個字符串作為回調(diào)函數(shù)的輸入?yún)?shù)注入到函數(shù)體內(nèi):

TCP socket和web socket的區(qū)別是什么

Chrome開發(fā)者工具里觀察到的WebSocket消息體:

TCP socket和web socket的區(qū)別是什么

下次面試被面試官問到TCP和WebSocket套接字的區(qū)別,相信大家應(yīng)該能夠知道如何回答了。

關(guān)于TCP socket和web socket的區(qū)別是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI