响应不是从Tcp Listener发送的

时间:2012-06-22 00:03:13

标签: vpn tcplistener hl7

我创建了一个简单的TCP侦听器来处理HL7消息,我正在接收消息,并尝试发回ACK消息。虽然另一端的服务器似乎没有收到回复,你觉得这个设置有什么问题吗?

我意识到它需要重构一点,现在我只是想建立连接。

class Server
{
    private TcpListener tcpListener;
    private Thread listenThread;

    public Server()
    {
        this.tcpListener = new TcpListener(IPAddress.Parse("hidden"), 55555);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            TcpClient client = this.tcpListener.AcceptTcpClient();

            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                break;
            }

            if (bytesRead == 0)
            {
                break;
            }

            ASCIIEncoding encoder = new ASCIIEncoding();
            string result = encoder.GetString(message, 0, bytesRead);

            string[] Lines = result.Split('\n');
            string id = "";
            foreach (string line in Lines)
            {
                string[] values = line.Split('|');
                if (values[0].Contains("MSH"))
                {
                    id = values[9];

                    byte[] buffer = encoder.GetBytes("\\vMSH|^~\\&|Rhapsody|JCL|EpicADT|JCL-EPIC-TEST|||ACK|A" + id + "|P|2.4|\\nMSA|AA|" + id + "|");

                    Console.WriteLine("MSH|^~\\&|Rhapsody|Test|EpicADT|TEST|||ACK|A" + id + "|P|2.4|\\nMSA|AA|" + id + "|");

                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                }
            }
        }

        tcpClient.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

在阅读消息并在套接字上写响应时,我看不到MLLP已实现。

通常是必须的,并且大多数应用程序都验证MLLP块。如果没有MLLP,客户端只需跳过您写在套接字上的数据即可。

很明显,没有MLLP,您的应用程序不会有任何问题。如果客户端未实现MLLP,则此答案无效。

我已经在我的other答案中对此进行了详细说明。