In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is the difference between TCP socket and web socket? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
First of all, let's read this passage with 166 likes:
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.
When we send a piece of byte data from in-memory buffer to a normal TCP socket, the send system call returns the actual number of bytes 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 the destination socket for sending data is a non-blocking socket or a non-blocking socket for write operations, the number of bytes sent returned by send may be less than the number of bytes to be sent in 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.
If it is a blocking socket, the two will be equal, because as the name implies, if the send system call does not send all the data to be sent, the API call will not return.
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.
The difference between Web socket and TCP socket, from the data sent, is no longer a series of bytes, but according to a complete "message body" sent out, this "message body" can not be further divided, either sent successfully, or not sent at all, there is no partial transmission like the TCP socket non-blocking operation. In other words, operations on sockets in Web Socket are non-blocking operations.
This distinction is also clearly stated on Wikipedia:
Websocket differs from TCP in that it enables a stream of messages instead of a stream of bytes
Let's take a look at the difference between the recipients.
Original text:
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.
Similarly, in the case of TCP sockets, the number of bytes read from the TCP socket by the receiver is not necessarily equal to the number of bytes sent by the sender calling the send. And WebSocket? The receiver of WebSocket reads data from sockets, not directly using recv/read as TCP sockets do, but using event-driven mechanism. That is, the application registers an event handler function. When the data sent by the sender of the web socket is already available in the receiver application from the kernel buffer to the application layer, the event handler function registered by the application is called in the way of callback.
Look at an example:
I sent a message "Wang Zixi" via WebSocket:
The string seen in the debugger is injected into the function as an input parameter to the callback function:
The WebSocket message body observed in the Chrome developer tool:
The next interview is asked by the interviewer about the difference between TCP and WebSocket sockets. I believe you should be able to know how to answer.
This is the answer to the question about what is the difference between TCP socket and web socket. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.