Asynchronous Sockets Client - Returning status

A

Anders Eriksson

Hello,

I'm trying to create an Asynchronous Client using the sample code from MSDN
http://msdn.microsoft.com/en-us/library/bew39x2a.aspx

My problem is that if the connection is refused, e.g. the server is not
started. I have no way of knowing this!

How do I return some kind of status from an async method?
--


Some snippets....
In the main form
MyClient vision = new MyClient();

vision.Connect();
// here I don't know if the connection is OK or not


// Async socket client class
class MyClient
{
Socket client;
private static ManualResetEvent connectDone =
new ManualResetEvent(false);

public void Connect(int port)
{
// Connect to a remote device.
try
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Loopback,
port);

// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;

// Complete the connection.
client.EndConnect(ar);

Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());

// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

--
--
English is not my first language
so any error, insults or strangeness
has happend during the translation.
Please correct my English so that I
may become better at it!
 
P

Peter Duniho

Anders said:
Hello,

I'm trying to create an Asynchronous Client using the sample code from MSDN
http://msdn.microsoft.com/en-us/library/bew39x2a.aspx

My problem is that if the connection is refused, e.g. the server is not
started. I have no way of knowing this!

How do I return some kind of status from an async method?

It depends.

However, in your case you are not implementing IAsyncResult, nor are you
using something like BeginInvoke() (for a Control or a Delegate). So,
you will have to use some besides the "EndInvoke()" pattern common to
much of the async APIs in .NET.

For this particular application, it seems to me that you should be
thinking about the problem less as a matter of "returning status" and
more as a matter of a general-purpose need to communicate from the async
thread back to other parts of your code. For example, your GUI.

What I have done in similar situations is define one or more events on
the asynchronous class. Then the client code can add handlers to the
events as desired. Any cross-thread invocation requirements are handled
in the event handlers by the client code. The asynchronous class itself
doesn't worry about that at all. It just raises the event on whatever
thread happens to be executing when the event needs to be raised.

You can either declare different events for completion and error case,
or you can declare a single event that simply alerts the client that
there's some new status in the async class, and then the client can
examine that status when the event is raised. I prefer the latter, but
there may some reason in your code to choose the former, or even yet
another variation on the idea that seems more suitable to you.

Of course, one option is to go ahead and implement the IAsyncResult
pattern in your own class. If you choose to do that, I'll recommend
this article:
http://blogs.msdn.com/junfeng/archive/2006/03/28/563627.aspx
I came across it recently while I was reviewing some other IAsyncResult
issues, and found it to be a really nice write-up of the kinds of things
you'll have to ensure are correct for an IAsyncResult implementation.

Pete
 
A

Anders Eriksson

Hello Peter,

As you already understand, I'm a beginner C# programmer. The event thing
sounds good. Can you point to a sample or some article that show how to use
events?

If you have a better sample than the MSDN code I also would appreciate it!

// Anders --
 
P

Peter Duniho

Anders said:
Hello Peter,

As you already understand, I'm a beginner C# programmer. The event thing
sounds good. Can you point to a sample or some article that show how to
use events?

If you have a better sample than the MSDN code I also would appreciate it!

It seems to me that the MSDN C# Programming Guide topics related to
events are reasonably comprehensive. If there's something specific
there you need help with, please feel free to ask:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

The event is in some ways just an elaborated version of a delegate
callback. Of course, if you prefer, you can just use a callback
directly instead of implementing an event. In that case, rather than
the client code subscribing to the event, it would just pass the
delegate reference for the callback to the Connect(int port) method, and
that reference would be used as the callback when the operation finally
completes.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top