private void Listen()
        {
            try
            {
                IPAddress ip = null;
                IPAddress.TryParse(_IPAddress, out ip);

                var ep = new IPEndPoint(ip, _Port);

                _Server = new TcpListener(ep);

                _Server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[256];

                // Enter the listening loop.
                while (true)
                {
                    OnNotify("Waiting for a connection... ", NotifyType.Message);

                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = _Server.AcceptTcpClient();
                    OnNotify("Connected!", NotifyType.Message);

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;
                    var buffer = new TcpBuffer();
                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        buffer.Add(bytes, i);
                        var messages = buffer.GetMessages(start: (char)11, stop: (char)28);
                        foreach (var message in messages)
                        {
                            OnNotify(message, NotifyType.Received);

                            //***************************
                            //All of this needs to be decoupled from the tcp logic
                            var parser = new A01Parser();
                            var parsedMessage = parser.Parse(message);

                            IAcknowlege ack = new HL7Acknowlege();

                            string returnMessage = ack.GetAcknowlegement(parsedMessage);
                            byte[] msg = System.Text.Encoding.ASCII.GetBytes((char)11 + returnMessage + (char)28);

                            //***************************

                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                            OnNotify(String.Format("{0}", returnMessage), NotifyType.Sent);
                        }
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (Exception e)
            {
                OnNotify(e.Message, NotifyType.Error);
            }
        }
Exemple #2
0
        private void Listen()
        {
            try
            {
                IPAddress ip = null;
                IPAddress.TryParse(_IPAddress, out ip);

                var ep = new IPEndPoint(ip, _Port);

                _Server = new TcpListener(ep);

                _Server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[256];

                // Enter the listening loop.
                while (true)
                {
                    OnNotify("Waiting for a connection... ", NotifyType.Message);

                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = _Server.AcceptTcpClient();
                    OnNotify("Connected!", NotifyType.Message);

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;
                    var buffer = new TcpBuffer();
                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        buffer.Add(bytes, i);
                        var messages = buffer.GetMessages(start: (char)11, stop: (char)28);
                        foreach (var message in messages)
                        {
                            OnNotify(message, NotifyType.Received);

                            //***************************
                            //All of this needs to be decoupled from the tcp logic
                            var parser        = new A01Parser();
                            var parsedMessage = parser.Parse(message);

                            IAcknowlege ack = new HL7Acknowlege();

                            string returnMessage = ack.GetAcknowlegement(parsedMessage);
                            byte[] msg           = System.Text.Encoding.ASCII.GetBytes((char)11 + returnMessage + (char)28);

                            //***************************

                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                            OnNotify(String.Format("{0}", returnMessage), NotifyType.Sent);
                        }
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (Exception e)
            {
                OnNotify(e.Message, NotifyType.Error);
            }
        }