Solved

Socket Reuse Issue

Posted on 2010-11-10
4
930 Views
Last Modified: 2012-05-10
In the following code sample, I am explicitely setting a local endpoint for tracking purposes and then sending out a request.  While this works without issue the first time through, if I run the program again too quickly, I recieve the following exception:

System.Net.Sockets.SocketException: Only one use of each socket address (protocol/network address/port) is normally permitted

As you can see, I am closing out the connection in the Finally block but that does not appear to make a difference.  If you comment out the tester.Bind line you never get this error.

Can somebody please help?
Socket tester = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint thisEnd = new IPEndPoint(IPAddress.Any, 9736);

            tester.Bind(thisEnd);

            tester.Ttl = 255;



            try

            {

                tester.Connect(IPAddress.Parse("173.194.35.104"), 80);

                tester.Disconnect(true);

            }

            catch (Exception exc)

            {

                Console.WriteLine("Error with TCP Test:\n" + exc.ToString());

            }

            finally

            {

                if (tester != null && tester.Connected)

                {

                    tester.Disconnect(true);

                }

                if (tester != null)

                {

                    tester.Close();

                }

            }

Open in new window

0
Comment
Question by:ktola
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34108242
From http://blogs.msdn.com/b/dgorti/archive/2005/09/18/470766.aspx :

When a connection is closed, on the side that is closing the connection the 5 tuple
{ Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
0
 

Author Comment

by:ktola
ID: 34108294
Yeah - I read that and then tried to change the Registry setting (actually added in the DWORD as it did not exist) but that did not change anything.  I just modified the code as follows and, for some reason, this appears to be working on the same port every time.
Socket tester = null;

            int startPort = 9729;

            IPEndPoint thisEnd;

            bool haveEndPt = false;



            while (!haveEndPt)

            {

                try

                {

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

                    tester.ExclusiveAddressUse = false;

                    tester.Ttl = 255;

                    startPort += 1;

                    if (startPort > 9750)

                    {

                        haveEndPt = true;

                        throw new Exception("Unable to connect on any port from 9730 to 9750");

                    }

                    thisEnd = new IPEndPoint(IPAddress.Any, startPort);

                    tester.Bind(thisEnd);

                    haveEndPt = true;

                    Console.WriteLine("The selected TCP Handshake port was: " + startPort.ToString());

                    tester.Connect(IPAddress.Parse("173.194.35.104"), 80);

                    tester.Disconnect(true);

                }

                catch (SocketException sockX)

                {

                    //Console.WriteLine(sockX.ToString());

                }

                catch (Exception exc)

                {

                    Console.WriteLine("Error with TCP Test:\n" + exc.ToString());

                }

                finally

                {

                    if (tester != null && tester.Connected)

                    {

                        tester.Disconnect(true);

                    }

                    if (tester != null)

                    {

                        tester.Close();

                    }

                }

            }

Open in new window

0
 

Author Comment

by:ktola
ID: 34108327
In playing around with the code, it appears that the following line:
tester.ExclusiveAddressUse = false;

Actually overcomes the issue and allows for multiple requests on the same connection.  I also had to make sure that the Disconnect was set to true:
tester.Disconnect(true);
 
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34108329
Did you reboot the system after changing the registry?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
using BGP Attributes 2 54
Class object 2 28
Vb. Net application freezes 9 31
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now