Link to home
Start Free TrialLog in
Avatar of townsma
townsmaFlag for Indonesia

asked on

.Net TCPIP Async Server unable to hold connections open.

I am trying to make a TCP IP Stream based server using the new async methods in .Net 4.5.  What I am trying to achieve is that when the server is started, it will listen for clients connecting to it.  Once client connects, it should hold the connection open for bi-directional traffic.

No more that two or three clients will ever connected, but usually it will only be one.

With each client I need to listen for <STX><DATA><ETX> string messages.   When we receive a message we pass it off to another class to decode and process, and we listen for the next message.

At the same time, we need to be able to send similarly formatted messages to a selected client.

I have found some code on the web, but I am very new to async programming, and am getting lost.  I would be grateful for any help anyone can offer.

What I found was:

public async void Run()
        {
            var listener = new TcpListener(IPAddress.Any, _port);
            listener.Start();
            
            while (true)
            {
                try
                {
                    var tcpClient = await listener.AcceptTcpClientAsync();
                    var t = Process(tcpClient);
                    await t;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        private async Task Process(TcpClient tcpClient)
        {
            var clientEndPoint = tcpClient.Client.RemoteEndPoint.ToString();
            Console.WriteLine("Received connection request from " + clientEndPoint);
            try
            {
                var networkStream = tcpClient.GetStream();
                var reader = new StreamReader(networkStream);
                var writer = new StreamWriter(networkStream) {AutoFlush = true};

                while (true)
                {
                    var request = await reader.ReadLineAsync();

                    if (request == null) break;

                    Console.WriteLine("Received service request: " + request);

                    OnMessageReceived(request);
                }

                tcpClient.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (tcpClient.Connected)
                    tcpClient.Close();
            }
        }

Open in new window


The problems I have with this is three fold.  Firstly, the server does not seems to be telling the client we have the message.  According to the client developers, we do not need to send an Ack, the normal TCP message handling should be fine.  This is causing the client to timeout, and reset and send the message again.

Secondly, and this may be related to the first, when we receive a correctly formatted message, and handle it, the program goes around to the ReadLineAsync again, but immediately receives a null message.  Again the client developers say they are not sending any nulls.

Finally, how can I find a specific client to send a message to, and how can I send it.  In the sample code it seems I can only send after I receive something.

Any and all help would be greatly appreciated, I have been trying all sorts of options, but everything ends in failure.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of townsma

ASKER

Many thanks for the reply, based on new information received from the developer of the client, and the information provided in the sample above, I have managed to get the interface working.