【问题标题】:Response not sending from Tcp Listener响应未从 Tcp 侦听器发送
【发布时间】:2012-06-24 07:13:48
【问题描述】:

我创建了一个简单的 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();
    }
}

【问题讨论】:

  • 当您使用 Wireshark 观察流量时,您是否在网络上看到您的传出数据包?
  • 好吧,我试图通过 Fiddler 观看它,但它正在通过 VPN 隧道,我不知道如何设置它来收听。我根本看不到传入或传出的流量。
  • 您不妨使用 Wireshark 并使用旨在嗅探所有网络流量的东西,而不仅仅是您在这里似乎没有使用的两个协议...跨度>
  • 感觉服务器刷新太频繁了——你还不如在一个数据包中发送一打 ACK 消息——但我无法想象这实际上会破坏任何东西。
  • 我只是将clientStream.flush() 调用移出内部循环。用wireshark祝你好运>

标签: vpn tcplistener hl7


【解决方案1】:

当您在套接字上读取消息和写入响应时,我没有看到 MLLP 实现。

一般 MLLP 是必需的,并且大多数应用程序都会验证 MLLP 块。如果没有 MLLP,客户端只会跳过正在写入套接字的数据。

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

我已经在我的other 回答中详细解释了这一点。

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 2014-12-02
    • 1970-01-01
    • 2021-04-14
    • 2022-07-14
    • 2021-01-05
    • 1970-01-01
    • 2012-05-20
    • 2020-09-06
    相关资源
    最近更新 更多