Groups | Blog | Home
all groups > dotnet clr > april 2005 >

dotnet clr : Urgent. Socket.BeginConnect doesn't work when is called from a separate thread


Anibal Acosta
4/26/2005 12:44:30 PM
One complete day a lost finding the problem, Now I know what is, but I
don't know why?

This is my simple source code. The first method call the BeginConnect()
using the same windows form thread, and the second method call the
BeginConnect() using another thread

You will think... "Why you want to execute the BeginConnect using
another thread" But believe me that a really need


I really need your help

AA




private void btnSync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using the same thread, using this way the socket tries to
//connect and the print in the console (false)

BeginConnect();
}



private void btnAsync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using other thread, using this way the socket tries to
//connect but print (true) in the console :(

System.Threading.Thread myThread = new System.Threading.Thread(new
System.Threading.ThreadStart(BeginConnect));
myThread.Start();
}





internal void BeginConnect()
{
Socket mSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

IPEndPoint mEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.0"),
8978);
mSocket.BeginConnect(mEndPoint, new AsyncCallback(ConnectCallBack),
mSocket);
}


private void ConnectCallBack(IAsyncResult ar)
{
Socket mSocket = (ar.AsyncState as Socket);
Console.WriteLine(mSocket.Connected);
Feroze [msft]
5/19/2005 3:46:17 PM
Can you be more specific as to what does not work? Are you getting an
exception somewhere?

Can you try changing the ccode in ConnectCallback as follows:

private void ConnectCallBack(IAsyncResult ar)
{
try {
Socket mSocket = (ar.AsyncState as Socket);
mSocket.EndConnect(ar);
Console.WriteLine(mSocket.Connected);
} catch(Exception e) {
Console.WriteLine(e);
}
}
--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

[quoted text, click to view]

AddThis Social Bookmark Button