WebSockets 和纯 TCP 之间的根本区别是什么? [英] What is the fundamental difference between WebSockets and pure TCP?

查看:43
本文介绍了WebSockets 和纯 TCP 之间的根本区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关 WebSockets 的文章,我想知道为什么浏览器不能简单地打开简单的 TCP像任何其他桌面应用程序一样连接和与服务器通信.为什么这种通信可以通过 websockets 实现?

解决方案

当您在内网边界内工作时,通过 TCP 套接字进行通信会更容易,因为您可能可以控制该网络上的机器并且可以打开合适的端口用于建立 TCP 连接.

通过互联网,您正在与另一端的其他人的服务器进行通信.他们不太可能为连接打开任何旧的套接字.通常它们只有几个标准端口,例如用于 HTTP 的端口 80 或用于 HTTPS 的 443 端口.因此,要与服务器通信,您必须使用这些端口之一进行连接.

鉴于这些是通常使用 HTTP 的 Web 服务器的标准端口,因此您必须遵守 HTTP 协议,否则服务器不会与您交谈.Web 套接字的目的是允许您通过 HTTP 发起连接,然后协商使用 Web 套接字协议(假设服务器能够这样做)以允许更像TCP 套接字"的通信流.

I've read about WebSockets and I wonder why browser couldn't simply open trivial TCP connection and communicate with server like any other desktop application. And why this communication is possible via websockets?

解决方案

It's easier to communicate via TCP sockets when you're working within an intranet boundary, since you likely have control over the machines on that network and can open ports suitable for making the TCP connections.

Over the internet, you're communicating with someone else's server on the other end. They are extremely unlikely to have any old socket open for connections. Usually they will have only a few standard ones such as port 80 for HTTP or 443 for HTTPS. So, to communicate with the server you are obliged to connect using one of those ports.

Given that these are standard ports for web servers that generally speak HTTP, you're therefore obliged to conform to the HTTP protocol, otherwise the server won't talk to you. The purpose of web sockets is to allow you to initiate a connection via HTTP, but then negotiate to use the web sockets protocol (assuming the server is capable of doing so) to allow a more "TCP socket"-like communication stream.

这篇关于WebSockets 和纯 TCP 之间的根本区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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