2

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();
    }
}
Jhorra
  • 6,233
  • 21
  • 69
  • 123
  • Do you see your outgoing packet on the wire when you watch the traffic with Wireshark? – sarnold Jun 22 '12 at 00:06
  • Well, I was trying to watch it over Fiddler, but it's going over a VPN tunnel and I'm not sure how to set it up to listen. I don't see the incoming or outgoing traffic at all. – Jhorra Jun 22 '12 at 00:07
  • You might as well use Wireshark and use something designed to sniff _all_ network traffic, not just two protocols that you don't appear to be using here... – sarnold Jun 22 '12 at 02:01
  • It feels like the server will flush too frequently -- you might as well send a dozen ACK messages in a single packet -- but I can't imagine that would actually break anything. – sarnold Jun 22 '12 at 02:39
  • I'd just move the `clientStream.flush()` call out of the inner loop. Any luck with wireshark> – sarnold Jun 22 '12 at 21:57

1 Answers1

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.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141