NodeJs - TCP/IP 套接字串行发送/接收? [英] NodeJs - TCP/IP Socket send/receive serially?

查看:38
本文介绍了NodeJs - TCP/IP 套接字串行发送/接收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在访问的 TCP 服务器(尝试使用内置节点 TLS 套接字)需要按特定顺序发送/接收的握手过程(发送,接收成功,发送更多消息,成功时,发送更多,等等).接收消息没有任何东西可以让我知道它正在响应哪个发送,因此我无法轻松使用内置 TCP 节点库的流式传输特性.

The TCP server I am hitting (trying to use the built in node TLS Socket) expects a handshaking process of send/receives in a certain order (send, on receive of success, send more messages, on success, send more, etc). The receive messages does not have anything to let me know which send it is responding to, so I am not able to easily use the streaming nature of the built in TCP Node library.

关于在 Node 中处理这种情况的最佳方法有什么想法吗?

Any ideas of what the best way to handle this case in Node?

示例(python),这是该过程的示例:

example (python), and this is example of the process:

    s.send("hello")
    s.send("send this 1")
    reply = s.recv()
    message = reply[0]
    if message == OK:
        print('Got OK for hello')
        s.send("send this 2")
        reply = s.recv()
        message = reply[0]
        if message == OK:
          print('Got it')
    else:
        raise Exception('Failed to send hello')

推荐答案

当你有非阻塞 I/O 并且你想做一些事情,比如发送数据,从发送中读取特定的响应,你需要设置一些适当的状态,以便在下一组数据进入时,您确切地知道它属于什么,因此您知道如何处理它.

When you have non-blocking I/O and you want to do something such as send data, read specific response from that send you need to set up some appropriate state so that when the next set of data come in, you know exactly what it belongs to and therefore you know what to do with it.

我能想到的方法有很多:

There are a number of ways to do that I can think of:

  1. 创建一个通用状态机,您可以在其中发送数据和读取数据,并且无论何时读取数据,您都可以知道套接字处于什么状态,因此您应该如何处理读取的数据.

  1. Create a general purpose state machine where you send data and read data and whenever you read data, you can tell what state the socket is in and therefore what you are supposed to do with the data you read.

在您发送数据的地方创建一组时态侦听器,然后为传入数据添加一个时态侦听器(您可以使用 .once()),专门设计用于以这种方式对其进行处理你期待这个回应.当数据到达时,您确保移除监听器.

Create a temporal set of listeners where you send data, then add a temporal listener (you can use .once()) for incoming data that is specially designed to process it the way you are expecting this response to be. When the data arrives, you make sure that listener is removed.

您的伪代码示例没有显示足够的信息供任何人提出更具体的建议.TCP,就其本质而言是流驱动的.它没有任何内置的消息或数据包意义.因此,您展示的内容甚至没有显示任何 TCP 协议的最基本级别,即如何知道何时收到了完整的响应.

Your pseudo-code example does not show enough info for anyone to make a more concrete suggestion. TCP, by its very nature is stream driven. It doesn't have any built-in sense of a message or a packet. So, what you show doesn't even show the most basic level of any TCP protocol which is how to know when you've received an entire response.

即使你用其他语言显示的 reply = s.recv() 在 TCP 中也不实用(无论语言如何),因为 s.recv() 需要知道什么时候收到完整的消息/块/你正在等待接收的任何内容.TCP 按发送的顺序传递数据,但对特定的信息包没有任何意义.您必须在 TCP 层之上提供它.用于描述消息的常用技术有:

Even your reply = s.recv() shown in some other language isn't practical in TCP (no matter the language) because s.recv() needs to know when it's got a complete message/chunk/whatever it is that you're waiting to receive. TCP delivers data in order sent, but does not have any sense of a particular packet of information that goes together. You have to supply that on top of the TCP layer. Common techniques used for delineating messages are :

  1. 某些消息定界符(例如回车或换行符或零字节或其他标记 - 所有这些都已知不会出现在消息本身内部)
  2. 首先发送一个长度,以便读者确切知道要读取的字节数.
  3. 在某种容器中包装消息,其中容器的开始和结束由容器的结构明确(注意上面的选项 1 和 2 只是此类容器的特定实现).例如,webSocket 协议使用非常具体的容器模型,其中包含一些长度数据和其他信息.

我想向您展示一个使用 socket.once('data', ...) 来监听特定响应的示例,但即使这样也不知道如何正常工作描述传入的消息,以便人们知道您何时收到了完整的传入消息.

I was thinking of showing you an example using socket.once('data', ...) to listen for the specific response, but even that won't work properly without knowing how to delineate an incoming message so one knows when you've received a complete incoming message.

因此,您的第一步是在 TCP 之上实现一个层,该层读取数据并知道如何将其分解为离散消息(知道完整消息何时到达以及如何分解多个可能到达),然后在整个消息到达时在套接字上发出您自己的事件.然后,只有这样,您才能开始使用上述技术实现状态机的其余部分.

So, one of your first steps would be to implement a layer on top of TCP that reads data and knows how to break it into discrete messages (knows both when a complete message has arrived and how to break up multiple messages that might be arriving) and then emits your own event on the socket when a whole message has arrived. Then, and only then, can you start to implement the rest of your state machine using the above techniques.

这篇关于NodeJs - TCP/IP 套接字串行发送/接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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