简体   繁体   English

如何通过同一个套接字连接发送多条消息?

[英]How to send multiple messages over same socket connection?

I am trying to send an array of messages through the same socket connection, but I get an error.我试图通过同一个套接字连接发送一组消息,但出现错误。

Here is my client code:这是我的客户代码:

def send_over_socket(hl7_msg_array):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((config.HOST, config.PORT))

    for single_hl7_msg in hl7_msg_array:
        sock.send(single_hl7_msg.to_mllp().encode('UTF-8'))
        received = sock.recv(1024*1024)
        print("Sent: ", received)

    sock.shutdown()
    sock.close()

While debugging the code, I see that the exception occurs when I call the sock.recv(1024*1024) for the second message.在调试代码时,我看到当我为第二条消息调用sock.recv(1024*1024)时发生了异常。

Here is the error:这是错误:

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

Server-side code:服务器端代码:

def run_mllp_server():

    class PDQHandler(AbstractHandler):
        def reply(self):
            msg = hl7.parse(self.incoming_message)
            msg_pid = msg[1][3]
            msg_key = msg[2][3][0][1]
            msg_value = msg[2][5]

            lock = RLock()
            lock.acquire()
            results_collection[str(msg_pid)][str(msg_key)] = str(msg_value)
            lock.release()
            print("Received: ", repr(self.incoming_message))
            return parse_message(self.incoming_message).to_mllp()

    # error handler
    class ErrorHandler(AbstractErrorHandler):
        def reply(self):
            if isinstance(self.exc, UnsupportedMessageType):
                print("Error handler success 1")
            else:
                print("Error handler else case")

    handlers = {
        'ORU^R01^ORU_R01': (PDQHandler,),
        'ERR': (ErrorHandler,)
    }

    server = MLLPServer(config.SOCKET_HOST, config.SOCKET_PORT, handlers)
    print("Running Socket on port ", config.SOCKET_PORT)
    server.serve_forever()

Here I am using MLLP protocol which has a TCP connection behind the scenes.这里我使用的是 MLLP 协议,它在后台有一个 TCP 连接。

Can you help me please figure out what is going on?你能帮我弄清楚发生了什么吗? Is it a problem of ACK?是ACK的问题吗?

I do not know python at all but...我根本不知道 python 但是......

I do not think multiple messages is your problem.我不认为多条消息是你的问题。 Looking at exception, I guess your first message is being sent correctly.查看异常,我猜您的第一条消息已正确发送。 Then, your client code waits for ACK to be received;然后,您的客户端代码等待收到 ACK; but server never sends it.但服务器从不发送它。 It instead closes the connection.相反,它会关闭连接。

Also, make sure that whether sendall should be used instead of send .另外,请确保是否应该使用sendall而不是send

After above issue is fixed, to send multiple messages on same connection, you have to follow MLLP (also called LLP) so that server can differentiate the message.上述问题修复后,要在同一连接上发送多条消息,您必须遵循MLLP (也称为 LLP),以便服务器可以区分消息。

Description                 HEX     ASCII   Symbol
Message starting character  0B      11      <VT>
Message ending characters   1C,0D   28,13   <FS>,<CR>

This way, when you send a message to Listener (TCP/MLLP server), it looks for Start and End Block in your incoming data.这样,当您向侦听器(TCP/MLLP 服务器)发送消息时,它会在您的传入数据中查找开始和结束块。 Based on it, it differentiates each message.在此基础上,它区分每条消息。

Please refer to this answer for more details.有关详细信息,请参阅答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM