【问题标题】:Is possible to serve http from telnet server in node.js? [closed]是否可以从 node.js 中的 telnet 服务器提供 http 服务? [关闭]
【发布时间】:2014-01-20 17:09:16
【问题描述】:

我了解 Http 协议不同于 Telnet 协议。两者都是 node.js 中网络模块之上的一层。那么如果我创建一个 Telnet 服务器聊天应用程序,这是否意味着我不能向浏览器提供 http 服务?我想要一个连接到 telnet 聊天服务器的 Web 客户端。

是否可以编写两个应用程序:一个 Web 服务器和一个 telnet 服务器。然后将web服务器连接到telnet服务器?这是为 telnet 服务器创建 Web 客户端的合乎逻辑的方式吗?

【问题讨论】:

  • 有可能,你可以在不同的端口上运行它们,例如在默认的 :80 上运行网络服务器,在 :3001 上运行聊天系统。让页面连接到:3001 有点像这样net.tutsplus.com/tutorials/javascript-ajax/…

标签: node.js protocols telnet


【解决方案1】:

要在 node.js 中编写 telnet 聊天应用程序,您需要使用原始套接字。 (http://nodejs.org/api/net.html#net_class_net_socket)

Http 通过 TCP(套接字)运行。

两个都可以吗?嗯,是的,但并不容易。问题是 http 和 telnet 都会创建套接字连接,但是 telnet 客户端在被告知之前不会做任何其他事情,而 http 会发送 http 请求。

你可以用一段时间内没有请求来作为telnet客户端的一个指标,但第二个问题是它意味着你必须手动接收和回复http请求。

【讨论】:

    【解决方案2】:

    使用 node.js 聊天服务器?你的意思是像socket.io这样的东西?

    不管怎样,让我们​​看看...

    $ curl http://localhost
    <html><body><h1>It works!</h1>
    <p>This is the default web page for this server.</p>
    <p>The web server software is running but no content has been added, yet.</p>
    </body></html>
    $ telnet localhost 80
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    GET
    <html><body><h1>It works!</h1>
    <p>This is the default web page for this server.</p>
    <p>The web server software is running but no content has been added, yet.</p>
    </body></html>
    Connection closed by foreign host.
    

    所以我猜你可以,问题是你为什么会? :)

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 2014-08-08
      • 2023-03-16
      • 2023-02-14
      • 2013-05-03
      • 1970-01-01
      • 2016-12-23
      相关资源
      最近更新 更多