TCPsocket和websocket的區(qū)別是什么-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),臨洮網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:臨洮等地區(qū)。臨洮做網(wǎng)站價(jià)格咨詢:028-86922220

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

首先我們來(lái)閱讀這段有166個(gè)贊的回答:

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)我們向一個(gè)通常的TCP套接字發(fā)送一段來(lái)自內(nèi)存buffer中的字節(jié)數(shù)據(jù)時(shí),send系統(tǒng)調(diào)用返回的是實(shí)際發(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ù)的目的方套接字是一個(gè)非阻塞套接字或者是對(duì)寫操作非阻塞的套接字,那么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.
如果是阻塞套接字,兩者會(huì)相等,因?yàn)轭櫭剂x,如果send系統(tǒng)調(diào)用沒有把所有待發(fā)送數(shù)據(jù)全部發(fā)送,則API調(diào)用不會(huì)返回。

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

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

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

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

看個(gè)例子:

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

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

在調(diào)試器里看到的這個(gè)字符串作為回調(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)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前題目:TCPsocket和websocket的區(qū)別是什么-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article28/dhgcjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣網(wǎng)站營(yíng)銷、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名