Response not sending from Tcp Listener

落爺英雄遲暮 提交于 2019-12-06 11:23:42

问题


I created a simple TCP Listener to handle HL7 messages, I am receiving the messages correctly, and attempting to send an ACK message back. The server on the other end doesn't seem to be getting the responses though, do you see anything wrong with this set up?

I realize it needs refactored a little, right now I'm just trying to establish the connection.

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:


I do not see MLLP implemented while you are reading the message and writing response on socket.

Generally MLLP is necessary and most of the applications verify the MLLP block. Without MLLP, client just skip your data being written on socket.

Apparently, your application do not have any issue without MLLP. This answer is not valid if Client does not implement MLLP.

I have explained this in more details in my other answer.



来源:https://stackoverflow.com/questions/11148553/response-not-sending-from-tcp-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!