Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Socket Reuse Issue

Posted on 2010-11-10
4
Medium Priority
?
937 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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