Share via


Only one usage of each socket address (protocol/network address/port) is normally permitted

Lets say that you are invoking a web service from another web service. Both are on the same box. You might be making authenticated
or unauthenticated calls and perhaps you are setting the KeepAlive = false.
Intermittently, (under load) you might get "Only one usage of each socket address (protocol/network address/port) is normally permitted (typically under load)."
You might be wondering why are you getting a *SOCKET* Exception...

Here is the scoop
1. When you make authenticated calls, the client is closing connections. And when you are making authenticated calls
repeatedly to the same server, you are making and closing connections repeatedly
2. The same might happen when you are making regular http [un authenticated] calls but setting keep-alive = false.

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.
In this case, the protocol is fixed - TCP
the local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
What happens is that when you don't bind a port in the range 1024-5000 is used.
So roughly you have 4000 ports. If you use all of them in 4 minutes - meaning roughly you
make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.

OK now how can this be fixed?
1. One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534.
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.

2. The second thing you can do is once the connection does get into an TIME_WAIT state you can reduce the time it is
in that state, Default is 4 monutes, but you can set this to 30 seconds
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.
Set this to 30 seconds

3. If you don't have access to registry you can do this through code.
In System.Net 2.0 we addeed what is called a BindIPEndPOintDelegate
What this does is to give you a chance to choose the local end point for the
connection that is being made.
See https://blogs.msdn.com/malarch/archive/2005/09/13/466664.aspx
for more info.

Using this, the following sample code attempts to choose between 5001 and 65534
and wrap around when we reach 65534.
Req.ServicePoint.BindIPEndPointDelegate
= new BindIPEndPoint(BindIPEndPointCallback);
public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount)
{
int port = Interlocked.Increment(ref m_LastBindPortUsed); //increment
Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534);
if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) {
return new IPEndPoint(IPAddress.Any,port);
}
else {
return new IPEndPoint(IPAddress.IPv6Any,port);
}
}

Durgaprasad Gorti
Durgaprasad.Gorti@microsoft.com.dontspam
Test Lead
System.Net

Comments

  • Anonymous
    September 17, 2005
    I love posts like this that explain how to tune System.Net.  System.Net.ServicePoint has long been...

  • Anonymous
    October 10, 2005
    Note that this BindIPEndPoint functionality is new to v2.0 of the .Net Framework.

  • Anonymous
    March 09, 2006
    I've posted an update to Cassini v2 sources that fixes a leak of Connection objects pointed out to me...

  • Anonymous
    October 03, 2008
    [latest here ] I've posted an update to Cassini v2 sources that fixes a leak of Connection objects pointed

  • Anonymous
    December 18, 2008
    Hi, I use CRM WebService retrieve over 20K contact email address, get "unable to access remote server"

  • Anonymous
    January 18, 2009
    PingBack from http://www.keyongtech.com/4971104-specifying-tcpclient-localendpoint-yeilds-10048-a

  • Anonymous
    February 09, 2009
    When doing bulk data import on CRM 4.0 using self-made application that utilizes CRM SDK to communicate

  • Anonymous
    February 09, 2009
    crosspost from http://blogs.msdn.com/rextang When doing bulk data import on CRM 4.0 using self-made application

  • Anonymous
    May 01, 2009
    PingBack from http://codinglifestyle.wordpress.com/2007/04/24/multi-threaded-webservice-unable-to-connect-to-remote-server/

  • Anonymous
    May 28, 2009
    In working with a customer we ran into this issue from MOSS Content Deployment. This is a good one to

  • Anonymous
    September 16, 2009
    hi, i m also getting same error while doing load testing and i had changes what you have suggested but it still showing the same error message in testing. what could be the reason for this, can you help me to getting out of this ? thanks brijesh

  • Anonymous
    January 04, 2010
    The comment has been removed

  • Anonymous
    June 07, 2010
    This is really nice way to post issues, first you highlighted the effects then you provided the resolution along with reason. Really a nice post. Thanks For Sharing Regards Zeeshan Umar

  • Anonymous
    December 29, 2010
    I receive the error in my vb.net app while testing ports using the following code: Dim port As Integer            Dim ipe As IPEndPoint            Dim nsTCP, nsUDP As Socket            port = 1            Do While port < 65535                port = port + 1                ipe = New IPEndPoint(IPAddress.Any, port)                nsTCP = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)                  Try                    nsTCP.Bind(ipe)                Catch ex As SocketException                    WritePortLogEntry("Port TCP " & port & " - " & ex.Message)                End Try                nsTCP.Close()                        Loop I have read your solution but I can not seem to translate it into vb.net and make it fit my code.  Any help?

  • Anonymous
    June 20, 2011
    Thanks for this, really saved a day in a CRM 3.0 related environment.

  • Anonymous
    June 25, 2011
    I like posts like this that explain how to tune System.Net.&nbsp; System.Net.ServicePoint has long been...

  • Anonymous
    January 22, 2012
    Thanks for the wonderful post!!!

  • Anonymous
    April 17, 2012
    i have tried those suggestion you mention but still get the same error message what can be other solution for that.

  • Anonymous
    September 15, 2013
    The comment has been removed

  • Anonymous
    January 21, 2014
    The comment has been removed

  • Anonymous
    November 18, 2016
    YOU MUST RESTART FOR IT TO TAKE EFFECThttps://msdn.microsoft.com/en-us/library/aa560610%28v=bts.20%29.aspx?f=255&MSPPError=-2147217396