-2

I have a question regarding a TCP Connection Class I'm writing.

The Connection Class I already wrote can establish a connection via TCP. It listens and writes to that connection from two different threads. Now I want to improve that class.

I want to establish this connection fully asynchronous so that the connection can be establishes while other things in my application will be prepared. But I have some other Classes which needs an established connection to work properly. So, my question is, could someone explain me with an little example how I could establish the connection asynchronous with the possibility that an other class starts to work after the connection is established?

My first idea was to fire an event after the TCP Client is connected but then I got in trouble with the fact, that the connection is only fully established if the remote server sends a message back. After that I got an idea to create a connection state. But now I don't know how to proceed further.

If it's not the way how to use await and async please let me know.

Thanks in advance, Patrick

Edit:

Thanks for your answers. To clarify things I want to update my post.

As someone requested what I have so far, I posted my source code at GitHub. In Line 46 I set the connection state to Connecting. After the TCP connection is established I change the status to Fetching (L155). Now I have to wait for the message which indicates that the connection is fully ready. After the Message came I expected I set the State to Open (L315) and fire an ConnectionEstablished Event.

Please excuse but I can't post more than 2 links.

iPaat
  • 792
  • 4
  • 16
  • You need to add a block to the threads that doesn't allow messages to be sent until the connection is made. See msdn : https://msdn.microsoft.com/en-us/library/system.threading.waithandle(v=vs.110).aspx. See also WaitOne : https://msdn.microsoft.com/en-us/library/kzy257t0(v=vs.110).aspx – jdweng Apr 20 '17 at 12:24
  • What have you tried so far? This is not a free-code fabric, please show your effort, your code, etc. – Massimiliano Kraus Apr 20 '17 at 12:28
  • I see that you are using `BeginConnect` which is an older API than `ConnectAsync` so my first suggestion would be to transfer to that instead. See a discussion [here](http://stackoverflow.com/questions/5764921/whats-the-difference-between-beginconnect-and-connectasync) regarding BeginConnect vs ConnectAsync and [Stephen Clearys blog](http://blog.stephencleary.com/2012/02/async-and-await.html) for an introduction to `async await` – default Apr 20 '17 at 13:57
  • @SUNflOw1991 what exactly are you trying to accomplish? The `TcpClient` already has an async api as pointed out by @Default. You need to narrow or otherwise clarify your question to a single goal and a single reproducible problem. – JSteward Apr 20 '17 at 17:35

1 Answers1

0

My bit into this: instead of using thread blocks; write a Task for the TCP connection with ManualresetEvent. Making the other part of Thread to change their state based on ManualresetEvent. I personally like Task link is -> https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx over Thread or Threadpool. Edit: I have written this on notepad today. If it gives any help.

Task t = Task.Run(() =>
            {
                //do the connection thing
                Connection();
                connectionDone.Set(); //Manualeventreset connectionDone = new ManualResetEvent(false);
            });
        try
        {
            t.Wait();
        }
        catch (AggregateException ae)
        {
            //write the exception somewhere
            WriteLog("Exception in task: " + ae);
        }
        finally
        {
            connectionDone.Reset();
        }
khmub
  • 112
  • 1
  • 10