如何在ssh连接中在.NET中实现发送和接收hl7数据 [英] How to implement send and receive hl7 data in .NET in ssh connection

查看:64
本文介绍了如何在ssh连接中在.NET中实现发送和接收hl7数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.Net中实现一个应用程序.我必须通过SSH创建有效的连接,但是HL7数据接收失败.目的地是树莓派.因此,当我调试ssh客户端已连接时,端口已转发,tcp客户端也已连接,但是我的查询没有任何答案.请给我一些例子!

I'm implementing an application in .Net. I have to create a connection by SSH which is works, but the HL7 data receiving fails. The destination is a raspberry pi. So when I'm debugging the ssh client is connected, the port is forwarded, the tcp client also connected, but there is no answer for my queries. Plese suggest me some examples!

在这个项目中,我已经在Android上实现了它-运行正常.因此,在.Net中,我尝试了NHapiTools库,也尝试了直接的TcpClient方法.localPort = remotePort.我用的是localIP ="localhost"

In this project I have already implemented it on Android - it works fine. So in .Net I tried the NHapiTools library and I also tried the direct TcpClient way too. localPort = remotePort. I used 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方法永远不会返回

So I experinced that the buffer size is 0 in TCP (due to time out). In the SimpleMLLP test SendHK7Message method never returns

推荐答案

经过几天的苦苦挣扎,我已经解决了这个问题.主要错误在于端口转发.我建议使用Renci的SSH.Net(Tamir ssh出现算法错误).创建ssh连接后,我使用它进行端口转发:

After days of struggling I have solved the problem. The main error was with the port forwarding. I would reccomend to use SSH.Net by Renci (There was algorithm error with Tamir ssh). After ssh connection created I used this to port forward:

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

使用cmd中的ipconfig/all检查您的localIP.或使用127.0.0.1作为回送IP.SimpleMLLPClient对我不起作用,因此我使用了直接tcp客户端查询方式.像这样:

Check your localIP with ipconfig /all in cmd. Or use 127.0.0.1 as a loopback IP. SimpleMLLPClient did not worked for me so I used the direct tcp client query way. Like this:

            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的建议进行查询,然后终于开始工作了.谢谢阿米特·乔希(Amit Joshi)!

So it gave me instant answer with 0 bytes (not due timeout). And here comes Amit Joshi help. I used a query what he suggested with START_OF_BLOCK, CARRIAGE_RETURN and END_OF_BLOCK and finally started to work. Thank you Amit Joshi!

其他信息:在Android(java/Kotlin)中,jsch会话setPortForwardingL可以在以下三个参数下正常工作:

Additional info: In Android (java/Kotlin) jsch session setPortForwardingL works fine with three params:

        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)

这篇关于如何在ssh连接中在.NET中实现发送和接收hl7数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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