简体   繁体   English

在同一套接字连接中发送多个HL7消息

[英]Send multiple HL7 message in the same Socket connection

I searched about this issue before asking but I couldn't find something similar. 在询问之前我搜索了此问题,但找不到类似的东西。 I developed a client / server solution to send / receive HL7 message. 我开发了一个客户端/服务器解决方案来发送/接收HL7消息。 I am using socket to connect the client to the server and from this connection I am able to send just 1 HL7 message using OutputSteam object. 我正在使用套接字将客户端连接到服务器,并且通过此连接,我只能使用OutputSteam对象发送1条HL7消息。 How could I send multiple HL7 in the same socket connection? 如何在同一套接字连接中发送多个HL7? I tried different approaches but they didn't work properly. 我尝试了不同的方法,但是它们无法正常工作。

Here is the piece of my code from the client side: 这是我的客户端代码:

//Create socket that is connected to server on specified port
        System.out.println("Connecting to Server....");
        Socket socket  = new Socket(ipServer, serverPort);
        System.out.println("Connected to Server");

        StringBuffer HL7Message1 = new StringBuffer();

        //Message 1
        HL7Message1
        .append(START_BLOCK)
        .append("MSH|^~\\&|NES|NINTENDO|TESTSYSTEM|TESTFACILITY|20010101000000||ADT^A04|Q000000000000000001|P|2.3")
        .append(CARRIAGE_RETURN)
        .append("EVN|A04|20010101000000|||^KOOPA^BOWSER^^^^^^^CURRENT")
        .append(CARRIAGE_RETURN)
        .append("PID|1||123456789|0123456789^AA^^JP|BROS^MARIO^HELLO^WORLD^ONE^||19850101000000|M|||123 FAKE STREET^MARIO \\T\\ LUIGI BROS PLACE^TOADSTOOL KINGDOM^NES^A1B2C3^JP^HOME^^1234|1234|(555)555-0123^HOME^JP:1234567|||S|MSH|12345678|||||||0|||||N")
        .append(CARRIAGE_RETURN)
        .append("NK1|1|PEACH^PRINCESS^^^^|SO|ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-1234|(123)555-2345|NOK|||||||||||||")
        .append(CARRIAGE_RETURN)
        .append("NK1|2|TOADSTOOL^PRINCESS^^^^|SO|YET ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-3456|(123)555-4567|EMC|||||||||||||")
        .append(CARRIAGE_RETURN)
        .append("PV1|1|O|ABCD^EFGH^|||^^|123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|^DOG^DUCKHUNT^^^^^^^CURRENT||CRD|||||||123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|AO|0123456789|1|||||||||||||||||||MSH||A|||20010101000000"
                )
        .append(CARRIAGE_RETURN)
        .append("IN1|1|PAR^PARENT||||LUIGI")
        .append(CARRIAGE_RETURN)
        .append("IN1|2|FRI^FRIEND||||PRINCESS")
        .append(CARRIAGE_RETURN)
        .append(END_BLOCK)
        .append(CARRIAGE_RETURN);

        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();

        //Send the MLLP-wrapped HL7 message to the server
        out.write(HL7Message1.toString().getBytes());

        byte[] byteBuffer =  new byte[200];
        in.read(byteBuffer);
        System.out.println("Received from Server: " + new String(byteBuffer));

From the server side 从服务器端

public String getMessage(InputStream anInputStream) throws IOException  {

            boolean end_of_message = false;
            StringBuffer parsedMessage = new StringBuffer();

            int characterReceived = 0;

            try {
                characterReceived = anInputStream.read();
            } catch (SocketException e) {
                System.out
                .println("Unable to read from socket stream. "
                        + "Connection may have been closed: " + e.getMessage());
                return null;
            }

            if (characterReceived == END_OF_TRANSMISSION) {
                return null;
            }

            if (characterReceived != START_OF_BLOCK) {
                throw new RuntimeException(
                        "Start of block character has not been received");
            }

            while (!end_of_message) {
                characterReceived = anInputStream.read();

                if (characterReceived == END_OF_TRANSMISSION) {
                    throw new RuntimeException(
                            "Message terminated without end of message character");
                }

                if (characterReceived == END_OF_BLOCK) {
                    characterReceived = anInputStream.read();

                    if (characterReceived != CARRIAGE_RETURN) {
                        throw new RuntimeException(
                                "End of message character must be followed by a carriage return character");
                    }
                    end_of_message = true;
                } else {
                    parsedMessage.append((char) characterReceived);
                }
            }

How could I send more HL7 messages in the same socket connection? 如何在同一套接字连接中发送更多HL7消息?

What you really want to do is separate your sockets from your data. 您真正想要做的就是将套接字与数据分开。 I suggest using a TCP Client Server like this one to manage your sockets. 我建议使用像这样的TCP客户端服务器来管理套接字。 Then you simply, write a message to your Client. 然后,您只需向您的客户写一条消息。 I have a service that does this for me. 我有一项服务可以为我做这件事。 Basically, I package up any string of HL7, which it looks like you already know how to do, and then call the Send method of the TCP Server. 基本上,我打包了HL7的任何字符串,看起来好像您已经知道该怎么做,然后调用TCP Server的Send方法。

Mine looks like this, but I think the one in the link will look similar: 我的看起来像这样,但是我认为链接中的那个看起来像:

public int Send(string data)
{
    TcpClient sender = new TcpClient();
    IPEndPoint localEP = new IPEndPoint(IPAddress.Any, Port);
    try
        {
            TcpServerConnection cn;

            if (connections.Count == 0)
            {
                //if there are no connections then we end up here.
                ConnectSocket(sender, localEP);
            }

            cn = connections[0];

            if (cn.Socket.Connected && cn.verifyConnected())
            {
                cn.sendData(data);
                return connections.Count;
            }
            cn.Socket.Close();

            ConnectSocket(sender, localEP);
            if (cn.Socket.Connected && cn.verifyConnected())
            {
                cn.sendData(data);
                return connections.Count;
            }
            cn.Socket.Close();
            return -1;
        }
        catch (Exception e)
        {                   
        }
    }

The important part, as you can see is that the TCP Server figures out whether or not there is an available socket and opens one if necessary. 正如您所看到的那样,重要的部分是TCP Server确定是否有可用的套接字,并在必要时打开一个套接字。 Then it sends the data down the line. 然后它将数据向下发送。 I think you'll have a lot of difficulty if you don't separate the transmission/coding to TCP from the HL7 messaging. 我认为,如果不将HL7消息传递中的TCP传输/编码分开,将会遇到很多困难。 If you want to wait for an ACK for MLLP release 2, I personally pull the outbound message ID and the returning message ID in the ACK and compare them: 如果要等待MLLP第2版的ACK,我个人将ACK中的出站消息ID和返回消息ID进行比较,并进行比较:

if (lastOutMessageID == lastOutAckID || lastOutMessageID == "")
{
    lastOutMessageID = SendNextHL7Message;
}

And then I listen for the ACK, and raise an event on the data coming in: 然后,我监听ACK,并对传入的数据引发一个事件:

void _contractManager_OnAckReceived(string msgID, string ackStatus)
{
    lastOutAckID = msgID;
    lastOutStatus = ackStatus;
    lastAckTime = DateTime.Now;
    outRetries = 0;
    DoNextHL7();
}

But the short version is manage the transmissions and sockets in their own classes and just send and wait data from them. 但是简短的版本是在它们自己的类中管理传输和套接字,并且只是从中发送和等待数据。

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

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