1

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.

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.

Can you help me please figure out what is going on? Is it a problem of ACK?

Amit Joshi
16.6k23 gold badges88 silver badges154 bronze badges
asked Nov 11, 2019 at 15:31

1 Answer 1

2

I do not know python at all but...

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; but server never sends it. It instead closes the connection.

Also, make sure that whether sendall should be used instead of 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.

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. Based on it, it differentiates each message.

Please refer to this answer for more details.

answered Nov 12, 2019 at 15:18
Sign up to request clarification or add additional context in comments.

3 Comments

It seems that the server closes the connection after receiving the first message, and I am not able to solve this problem for a long time. If in the client side I put these 2 lines of code inside the for loop, everything works: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((config.HOST, config.PORT))
but I don't want to open array.size() sockets, do you have any ideas how to solve this issue?
@AndrewT: Sorry; I do not know python. Here is some C# code you may refer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.