TCP socket和web socket的區(qū)別是什么,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設計、網(wǎng)站建設、外貿(mào)網(wǎng)站建設與策劃設計,臨洮網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:臨洮等地區(qū)。臨洮做網(wǎng)站價格咨詢:028-86922220首先我們來閱讀這段有166個贊的回答:
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套接字發(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里對套接字的操作是非阻塞操作。
這個區(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ū)動機制。即應用程序注冊一個事件處理函數(shù),當web socket的發(fā)送方發(fā)送的數(shù)據(jù)在接收方應用從內(nèi)核緩沖區(qū)拷貝到應用程序?qū)右呀?jīng)處于可用狀態(tài)時 ,應用程序注冊的事件處理函數(shù)以回調(diào)(callback)的方式被調(diào)用。
看個例子:
我通過WebSocket發(fā)送一個消息“汪子熙”:
在調(diào)試器里看到的這個字符串作為回調(diào)函數(shù)的輸入?yún)?shù)注入到函數(shù)體內(nèi):
Chrome開發(fā)者工具里觀察到的WebSocket消息體:
下次面試被面試官問到TCP和WebSocket套接字的區(qū)別,相信大家應該能夠知道如何回答了。
關于TCP socket和web socket的區(qū)別是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道了解更多相關知識。
當前題目:TCPsocket和websocket的區(qū)別是什么-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article28/dhgcjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、網(wǎng)站維護、網(wǎng)頁設計公司、全網(wǎng)營銷推廣、網(wǎng)站營銷、網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容