簡體   English   中英

如何在ssh連接中在.NET中實現發送和接收hl7數據

[英]How to implement send and receive hl7 data in .NET in ssh connection

我正在.Net中實現一個應用程序。 我必須通過SSH創建有效的連接,但是HL7數據接收失敗。 目的地是樹莓派。 因此,當我調試ssh客戶端已連接時,端口已轉發,tcp客戶端也已連接,但是我的查詢沒有任何答案。 請給我一些例子!

在這個項目中,我已經在Android上實現了它-工作正常。 因此,在.Net中,我嘗試了NHapiTools庫,也嘗試了直接的TcpClient方法。 localPort = remotePort。 我用localIP =“ localhost”

static void Main(string[] args)
    {
        try
        {
            PrivateKeyFile file = new PrivateKeyFile(@"./key/private.key");
        using (var client = new SshClient(remoteIP, sshPort, username, file))
            {
                client.Connect();
                var ci = client.ConnectionInfo;
                var port = new ForwardedPortLocal(localIP, localPort, client.ConnectionInfo.Host, remotePort);
                client.AddForwardedPort(port);
                port.Start();
                var req = "MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5";

                ////TCP
                var tcpClient = new TcpClient();
                tcpClient.Connect(localIP, (int)localPort);
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(req);

                using (var stream = tcpClient.GetStream())
                {
                    stream.Write(data, 0, data.Length);

                    using (var buffer = new MemoryStream())
                    {
                        byte[] chunk = new byte[4096];
                        int bytesRead;

                        while ((bytesRead = stream.Read(chunk, 0, chunk.Length)) > 0)
                        {
                            buffer.Write(chunk, 0, bytesRead);
                        }

                        data = buffer.ToArray();
                    }
                }
   //I used this also with same result -> no respond
   //SimpleMLLP
   /*
   var connection = new SimpleMLLPClient(localIP, localPort, 
   Encoding.UTF8);
   var response = connection.SendHL7Message(req);
   */
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.ReadLine();
    }

}

因此,我體驗了TCP中的緩沖區大小為0(由於超時)。 在SimpleMLLP測試中,SendHK7Message方法永遠不會返回

您在發送消息時未實現MLLP (也稱為LLP)協議。

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

這樣,當您將消息發送到偵聽器(TCP / MLLP服務器)時,它將在傳入數據中查找啟動塊。 它永遠找不到。 考慮到垃圾,它只是丟棄您的整個消息。 因此,您沒有從監聽器中得到任何回報。

隨着MLLP實現,你的消息(東西你寫上插座)看起來應該象下面這樣:

<VT>MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5<FS><CR>

請注意<VT><CR><FS>是以上消息中的占位符。

你可以參考這個文章的詳細信息(讀第4步以后):

 using System; using System.Net; using System.Net.Sockets; using System.Text; namespace SimpleMllpHl7ClientAdvanced { public class Program { private static char END_OF_BLOCK = '\'; private static char START_OF_BLOCK = '\'; private static char CARRIAGE_RETURN = (char)13; static void Main(string[] args) { TcpClient ourTcpClient = null; NetworkStream networkStream = null; var testHl7MessageToTransmit = new StringBuilder(); //a HL7 test message that is enveloped with MLLP as described in my article testHl7MessageToTransmit.Append(START_OF_BLOCK) .Append("MSH|^~\\\\&|AcmeHIS|StJohn|CATH|StJohn|20061019172719||ORM^O01|MSGID12349876|P|2.3") .Append(CARRIAGE_RETURN) .Append("PID|||20301||Durden^Tyler^^^Mr.||19700312|M|||88 Punchward Dr.^^Los Angeles^CA^11221^USA|||||||") .Append(CARRIAGE_RETURN) .Append("PV1||O|OP^^||||4652^Paulson^Robert|||OP|||||||||9|||||||||||||||||||||||||20061019172717|20061019172718") .Append(CARRIAGE_RETURN) .Append("ORC|NW|20061019172719") .Append(CARRIAGE_RETURN) .Append("OBR|1|20061019172719||76770^Ultrasound: retroperitoneal^C4|||12349876") .Append(CARRIAGE_RETURN) .Append(END_OF_BLOCK) .Append(CARRIAGE_RETURN); try { //initiate a TCP client connection to local loopback address at port 1080 ourTcpClient = new TcpClient(); ourTcpClient.Connect(new IPEndPoint(IPAddress.Loopback, 1080)); Console.WriteLine("Connected to server...."); //get the IO stream on this connection to write to networkStream = ourTcpClient.GetStream(); //use UTF-8 and either 8-bit encoding due to MLLP-related recommendations var sendMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString()); if (networkStream.CanWrite) { //send a message through this connection using the IO stream networkStream.Write(sendMessageByteBuffer, 0, sendMessageByteBuffer.Length); Console.WriteLine("Data was sent data to server successfully...."); var receiveMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString()); var bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length); // Our server for this example has been designed to echo back the message // keep reading from this stream until the message is echoed back while (bytesReceivedFromServer > 0) { if (networkStream.CanRead) { bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length); if (bytesReceivedFromServer == 0) { break; } } } var receivedMessage = Encoding.UTF8.GetString(receiveMessageByteBuffer); Console.WriteLine("Received message from server: {0}", receivedMessage); } Console.WriteLine("Press any key to exit..."); Console.ReadLine(); } catch (Exception ex) { //display any exceptions that occur to console Console.WriteLine(ex.Message); } finally { //close the IO strem and the TCP connection networkStream?.Close(); ourTcpClient?.Close(); } } } } 

您應該按以下方式修改您的以下代碼行:

var req = START_OF_BLOCK + "MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5" + END_OF_BLOCK + CARRIAGE_RETURN;

有關更多開放源代碼,您可以參考 github項目。

經過幾天的努力,我解決了問題。 主要錯誤在於端口轉發。 我建議使用Renci的SSH.Net(Tamir ssh出現算法錯誤)。 創建ssh連接后,我用它來轉發端口:

           var port = new ForwardedPortLocal(localIP, localPort, "localhost", remotePort);

使用cmd中的ipconfig / all檢查您的localIP。 或使用127.0.0.1作為回送IP。 SimpleMLLPClient對我不起作用,因此我使用了直接tcp客戶端查詢方式。 像這樣:

            TcpClient ourTcpClient = new TcpClient();
            ourTcpClient.Connect(localIP, (int)localPort); 
            NetworkStream networkStream = ourTcpClient.GetStream();

            var sendMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString());

            if (networkStream.CanWrite)
            {
                networkStream.Write(sendMessageByteBuffer, 0, sendMessageByteBuffer.Length);

                Console.WriteLine("Data was sent to server successfully....");
                byte[] receiveMessageByteBuffer = new byte[ourTcpClient.ReceiveBufferSize];
                var bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length);

                if (bytesReceivedFromServer > 0 && networkStream.CanRead)
                {
                    receivedMessage.Append(Encoding.UTF8.GetString(receiveMessageByteBuffer));
                }

                var message = receivedMessage.Replace("\0", string.Empty);
                Console.WriteLine("Received message from server: {0}", message);
            }

所以它給了我0字節的即時答復(沒有超時)。 這是阿米特·喬希(Amit Joshi)的幫助。 我使用他對START_OF_BLOCK,CARRIAGE_RETURN和END_OF_BLOCK的建議進行查詢,然后終於開始工作了。 謝謝阿米特·喬希!

附加信息:在Android(java / Kotlin)中,jsch會話setPortForwardingL在以下三個參數中可以正常工作:

        val session = jsch.getSession("user", sshIP, sshPort)
        session.setPassword("")
        jsch.addIdentity(privatekey.getAbsolutePath())
        // Avoid asking for key confirmation
        val prop = Properties()
        prop.setProperty("StrictHostKeyChecking", "no")
        session.setConfig(prop)
        session.connect(5000)
        session.setPortForwardingL(localForwardPort, "localhost", remotePort)

        val useTls = false
        val context = DefaultHapiContext()
        connection = context.newClient("localhost", localForwardPort, useTls)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM