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!
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!