Home All Groups Group Topic Archive Search About

Socket.EndConnect doesn't throw expected exception

Author
14 Dec 2005 10:43 PM
Mike Hildner
Greetings,

Hope I'm posting in the right group.

I set up a Socket with my local IP and a port on which nothing is listening.
When I use .Connect, I get the expected SocketException 10061 No connection
could be made because the target machine actively refused it.

Now, when I use .BeginConnect / .EndConnect no exception is thrown on
..EndConnect and the code continues. I get an error on .BeginReceive telling
me the Socket is not connected. If I try the .BeginConnect / .EndConnect a
second time, I do get the expected 10061 ErrorCode.

This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
there a reason why the first .EndConnect does not throw an exception like I
expect it to?

Thanks,
Mike

Author
15 Dec 2005 9:34 AM
Vadym Stetsyak
Strange... I cannot reproduce this situation...

Here is the code, it is based on .NET 2.0

namespace Client
{
    class Program
    {
        public static ManualResetEvent allDone =
            new ManualResetEvent(false);

        public static void BeginConnect1(string host, int port)
        {

            IPAddress[] IPs = Dns.GetHostAddresses(host);

            Socket s = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

            allDone.Reset();

            Console.WriteLine("Establishing Connection to {0}",
                host);
            s.BeginConnect(IPs[0], port,
                new AsyncCallback(ConnectCallback1), s);

            // wait here until the connect finishes.
            // The callback sets allDone.
            allDone.WaitOne();

            Console.WriteLine("Connection established");
        }

        public static void ConnectCallback1(IAsyncResult ar)
        {
            allDone.Set();
            Socket s = (Socket)ar.AsyncState;
            s.EndConnect(ar);        //here i have the exception if noone is
listering on the remote address
        }

        static void Main(string[] args)
        {
            BeginConnect1("192.168.0.15", 6666);

            Console.ReadLine();
         }
    }
}


--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
news:88464CE6-4BCC-470C-AAC0-E05810847C56@microsoft.com...
> Greetings,
>
> Hope I'm posting in the right group.
>
> I set up a Socket with my local IP and a port on which nothing is
> listening.
> When I use .Connect, I get the expected SocketException 10061 No
> connection
> could be made because the target machine actively refused it.
>
> Now, when I use .BeginConnect / .EndConnect no exception is thrown on
> .EndConnect and the code continues. I get an error on .BeginReceive
> telling
> me the Socket is not connected. If I try the .BeginConnect / .EndConnect a
> second time, I do get the expected 10061 ErrorCode.
>
> This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
> there a reason why the first .EndConnect does not throw an exception like
> I
> expect it to?
>
> Thanks,
> Mike
Author
15 Dec 2005 4:58 PM
Mike Hildner
Hello Vadym,

Thanks for the reply. I was not using a ManualResetEvent. Apparently that is
the problem. When I do use one, I get the exception as expected.

Then I took your code and removed the ManualResetEvent. I expected to get no
exception, but the exception still occurred.

I'm not sure what the difference is, although my code is a Windows Service
and not a console app.

Thanks,
Mike

Show quote
"Vadym Stetsyak" wrote:

> Strange... I cannot reproduce this situation...
>
> Here is the code, it is based on .NET 2.0
>
> namespace Client
> {
>     class Program
>     {
>         public static ManualResetEvent allDone =
>             new ManualResetEvent(false);
>
>         public static void BeginConnect1(string host, int port)
>         {
>
>             IPAddress[] IPs = Dns.GetHostAddresses(host);
>
>             Socket s = new Socket(AddressFamily.InterNetwork,
>                 SocketType.Stream,
>                 ProtocolType.Tcp);
>
>             allDone.Reset();
>
>             Console.WriteLine("Establishing Connection to {0}",
>                 host);
>             s.BeginConnect(IPs[0], port,
>                 new AsyncCallback(ConnectCallback1), s);
>
>             // wait here until the connect finishes.
>             // The callback sets allDone.
>             allDone.WaitOne();
>
>             Console.WriteLine("Connection established");
>         }
>
>         public static void ConnectCallback1(IAsyncResult ar)
>         {
>             allDone.Set();
>             Socket s = (Socket)ar.AsyncState;
>             s.EndConnect(ar);        //here i have the exception if noone is
> listering on the remote address
>         }
>
>         static void Main(string[] args)
>         {
>             BeginConnect1("192.168.0.15", 6666);
>
>             Console.ReadLine();
>          }
>     }
> }
>
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
> news:88464CE6-4BCC-470C-AAC0-E05810847C56@microsoft.com...
> > Greetings,
> >
> > Hope I'm posting in the right group.
> >
> > I set up a Socket with my local IP and a port on which nothing is
> > listening.
> > When I use .Connect, I get the expected SocketException 10061 No
> > connection
> > could be made because the target machine actively refused it.
> >
> > Now, when I use .BeginConnect / .EndConnect no exception is thrown on
> > .EndConnect and the code continues. I get an error on .BeginReceive
> > telling
> > me the Socket is not connected. If I try the .BeginConnect / .EndConnect a
> > second time, I do get the expected 10061 ErrorCode.
> >
> > This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
> > there a reason why the first .EndConnect does not throw an exception like
> > I
> > expect it to?
> >
> > Thanks,
> > Mike
>
>
>
Author
16 Dec 2005 11:08 AM
Vadym Stetsyak
I've removed event from the code, and guess what - I'm still getting
exception...

What is the value of Socket.Connected value, when connection callback is
called?

Maybe as a quick solution would be the check if
Socket.Connected == true....

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
news:DF435A5F-7119-469D-A512-B95356346FC7@microsoft.com...
> Hello Vadym,
>
> Thanks for the reply. I was not using a ManualResetEvent. Apparently that
> is
> the problem. When I do use one, I get the exception as expected.
>
> Then I took your code and removed the ManualResetEvent. I expected to get
> no
> exception, but the exception still occurred.
>
> I'm not sure what the difference is, although my code is a Windows Service
> and not a console app.
>
> Thanks,
> Mike
>
> "Vadym Stetsyak" wrote:
>
>> Strange... I cannot reproduce this situation...
>>
>> Here is the code, it is based on .NET 2.0
>>
>> namespace Client
>> {
>>     class Program
>>     {
>>         public static ManualResetEvent allDone =
>>             new ManualResetEvent(false);
>>
>>         public static void BeginConnect1(string host, int port)
>>         {
>>
>>             IPAddress[] IPs = Dns.GetHostAddresses(host);
>>
>>             Socket s = new Socket(AddressFamily.InterNetwork,
>>                 SocketType.Stream,
>>                 ProtocolType.Tcp);
>>
>>             allDone.Reset();
>>
>>             Console.WriteLine("Establishing Connection to {0}",
>>                 host);
>>             s.BeginConnect(IPs[0], port,
>>                 new AsyncCallback(ConnectCallback1), s);
>>
>>             // wait here until the connect finishes.
>>             // The callback sets allDone.
>>             allDone.WaitOne();
>>
>>             Console.WriteLine("Connection established");
>>         }
>>
>>         public static void ConnectCallback1(IAsyncResult ar)
>>         {
>>             allDone.Set();
>>             Socket s = (Socket)ar.AsyncState;
>>             s.EndConnect(ar);        //here i have the exception if noone
>> is
>> listering on the remote address
>>         }
>>
>>         static void Main(string[] args)
>>         {
>>             BeginConnect1("192.168.0.15", 6666);
>>
>>             Console.ReadLine();
>>          }
>>     }
>> }
>>
>>
>> --
>> Vadym Stetsyak aka Vadmyst
>> http://vadmyst.blogspot.com
>>
>> "Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
>> news:88464CE6-4BCC-470C-AAC0-E05810847C56@microsoft.com...
>> > Greetings,
>> >
>> > Hope I'm posting in the right group.
>> >
>> > I set up a Socket with my local IP and a port on which nothing is
>> > listening.
>> > When I use .Connect, I get the expected SocketException 10061 No
>> > connection
>> > could be made because the target machine actively refused it.
>> >
>> > Now, when I use .BeginConnect / .EndConnect no exception is thrown on
>> > .EndConnect and the code continues. I get an error on .BeginReceive
>> > telling
>> > me the Socket is not connected. If I try the .BeginConnect /
>> > .EndConnect a
>> > second time, I do get the expected 10061 ErrorCode.
>> >
>> > This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
>> > there a reason why the first .EndConnect does not throw an exception
>> > like
>> > I
>> > expect it to?
>> >
>> > Thanks,
>> > Mike
>>
>>
>>
Author
16 Dec 2005 11:25 AM
Vadym Stetsyak
Can you post the code, where you're doing async connect?

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

Show quote
"Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
news:DF435A5F-7119-469D-A512-B95356346FC7@microsoft.com...
> Hello Vadym,
>
> Thanks for the reply. I was not using a ManualResetEvent. Apparently that
> is
> the problem. When I do use one, I get the exception as expected.
>
> Then I took your code and removed the ManualResetEvent. I expected to get
> no
> exception, but the exception still occurred.
>
> I'm not sure what the difference is, although my code is a Windows Service
> and not a console app.
>
> Thanks,
> Mike
>
> "Vadym Stetsyak" wrote:
>
>> Strange... I cannot reproduce this situation...
>>
>> Here is the code, it is based on .NET 2.0
>>
>> namespace Client
>> {
>>     class Program
>>     {
>>         public static ManualResetEvent allDone =
>>             new ManualResetEvent(false);
>>
>>         public static void BeginConnect1(string host, int port)
>>         {
>>
>>             IPAddress[] IPs = Dns.GetHostAddresses(host);
>>
>>             Socket s = new Socket(AddressFamily.InterNetwork,
>>                 SocketType.Stream,
>>                 ProtocolType.Tcp);
>>
>>             allDone.Reset();
>>
>>             Console.WriteLine("Establishing Connection to {0}",
>>                 host);
>>             s.BeginConnect(IPs[0], port,
>>                 new AsyncCallback(ConnectCallback1), s);
>>
>>             // wait here until the connect finishes.
>>             // The callback sets allDone.
>>             allDone.WaitOne();
>>
>>             Console.WriteLine("Connection established");
>>         }
>>
>>         public static void ConnectCallback1(IAsyncResult ar)
>>         {
>>             allDone.Set();
>>             Socket s = (Socket)ar.AsyncState;
>>             s.EndConnect(ar);        //here i have the exception if noone
>> is
>> listering on the remote address
>>         }
>>
>>         static void Main(string[] args)
>>         {
>>             BeginConnect1("192.168.0.15", 6666);
>>
>>             Console.ReadLine();
>>          }
>>     }
>> }
>>
>>
>> --
>> Vadym Stetsyak aka Vadmyst
>> http://vadmyst.blogspot.com
>>
>> "Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
>> news:88464CE6-4BCC-470C-AAC0-E05810847C56@microsoft.com...
>> > Greetings,
>> >
>> > Hope I'm posting in the right group.
>> >
>> > I set up a Socket with my local IP and a port on which nothing is
>> > listening.
>> > When I use .Connect, I get the expected SocketException 10061 No
>> > connection
>> > could be made because the target machine actively refused it.
>> >
>> > Now, when I use .BeginConnect / .EndConnect no exception is thrown on
>> > .EndConnect and the code continues. I get an error on .BeginReceive
>> > telling
>> > me the Socket is not connected. If I try the .BeginConnect /
>> > .EndConnect a
>> > second time, I do get the expected 10061 ErrorCode.
>> >
>> > This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
>> > there a reason why the first .EndConnect does not throw an exception
>> > like
>> > I
>> > expect it to?
>> >
>> > Thanks,
>> > Mike
>>
>>
>>
Author
16 Dec 2005 3:29 PM
Mike Hildner
Hello Vadym,

I hope my last post was clear.

You code (console application): Expected exception occurs, with or without
using ManualResetEvent.

My code (Windows server): Expected exception occurs only when using
ManualResetEvent

Here's the code from my service - with the ManualResetEvent.Set commented
out, the code does not throw an exception as (I think) it should.

        private void ConnectToStateSwitchCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the Socket and end the asynchronous connect
method.
                //allDone.Set();
                Socket s = (Socket)ar.AsyncState;
                s.EndConnect(ar);

You are probably right in checking for Socket.Connected, I will look into
that as MSDN says "whether a Socket is connected to a remote host as of the
last Send or Receive operation." I have not sent or received data as of yet.

What I am doing is to call Socket.BeginReceive a few lines down in the
method snippet above. .BeginReceive will throw a SocketException.ErrorCode of
10057 - socket is not connected. I then try to connect again, and on the
second try, I get the expected exception - ErrorCode 10061.

Thank you for your time. This is not a huge issue for me, as I have two or
three ways to work around it right now. I'm just curious and trying to figure
exactly what is happening.

Show quote
"Vadym Stetsyak" wrote:

> Can you post the code, where you're doing async connect?
>
> --
> Vadym Stetsyak aka Vadmyst
> http://vadmyst.blogspot.com
>
> "Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
> news:DF435A5F-7119-469D-A512-B95356346FC7@microsoft.com...
> > Hello Vadym,
> >
> > Thanks for the reply. I was not using a ManualResetEvent. Apparently that
> > is
> > the problem. When I do use one, I get the exception as expected.
> >
> > Then I took your code and removed the ManualResetEvent. I expected to get
> > no
> > exception, but the exception still occurred.
> >
> > I'm not sure what the difference is, although my code is a Windows Service
> > and not a console app.
> >
> > Thanks,
> > Mike
> >
> > "Vadym Stetsyak" wrote:
> >
> >> Strange... I cannot reproduce this situation...
> >>
> >> Here is the code, it is based on .NET 2.0
> >>
> >> namespace Client
> >> {
> >>     class Program
> >>     {
> >>         public static ManualResetEvent allDone =
> >>             new ManualResetEvent(false);
> >>
> >>         public static void BeginConnect1(string host, int port)
> >>         {
> >>
> >>             IPAddress[] IPs = Dns.GetHostAddresses(host);
> >>
> >>             Socket s = new Socket(AddressFamily.InterNetwork,
> >>                 SocketType.Stream,
> >>                 ProtocolType.Tcp);
> >>
> >>             allDone.Reset();
> >>
> >>             Console.WriteLine("Establishing Connection to {0}",
> >>                 host);
> >>             s.BeginConnect(IPs[0], port,
> >>                 new AsyncCallback(ConnectCallback1), s);
> >>
> >>             // wait here until the connect finishes.
> >>             // The callback sets allDone.
> >>             allDone.WaitOne();
> >>
> >>             Console.WriteLine("Connection established");
> >>         }
> >>
> >>         public static void ConnectCallback1(IAsyncResult ar)
> >>         {
> >>             allDone.Set();
> >>             Socket s = (Socket)ar.AsyncState;
> >>             s.EndConnect(ar);        //here i have the exception if noone
> >> is
> >> listering on the remote address
> >>         }
> >>
> >>         static void Main(string[] args)
> >>         {
> >>             BeginConnect1("192.168.0.15", 6666);
> >>
> >>             Console.ReadLine();
> >>          }
> >>     }
> >> }
> >>
> >>
> >> --
> >> Vadym Stetsyak aka Vadmyst
> >> http://vadmyst.blogspot.com
> >>
> >> "Mike Hildner" <mhildner.nospam@afweb.com> wrote in message
> >> news:88464CE6-4BCC-470C-AAC0-E05810847C56@microsoft.com...
> >> > Greetings,
> >> >
> >> > Hope I'm posting in the right group.
> >> >
> >> > I set up a Socket with my local IP and a port on which nothing is
> >> > listening.
> >> > When I use .Connect, I get the expected SocketException 10061 No
> >> > connection
> >> > could be made because the target machine actively refused it.
> >> >
> >> > Now, when I use .BeginConnect / .EndConnect no exception is thrown on
> >> > .EndConnect and the code continues. I get an error on .BeginReceive
> >> > telling
> >> > me the Socket is not connected. If I try the .BeginConnect /
> >> > .EndConnect a
> >> > second time, I do get the expected 10061 ErrorCode.
> >> >
> >> > This behaves the same in .NET 1.1 and 2.0. Am I missing something or is
> >> > there a reason why the first .EndConnect does not throw an exception
> >> > like
> >> > I
> >> > expect it to?
> >> >
> >> > Thanks,
> >> > Mike
> >>
> >>
> >>
>
>
>

AddThis Social Bookmark Button