• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
27 views

Problem:

I am having this issue with my code : 

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

Can anyone guide me please?

by (330 points)  
edited by

1 Answer

0 votes

Possible Cause:

1. This might be due to authenticated calls, if clients suspects such calls it closes the connection.Repeated calls of such action causes to connect and close connection repeatedly.

2. The other reason is your call is http ie without https calls and you have set value keep-alive = false.

3. This also means you have used all the ports for network on your machine which is 4000 ports. For 240 seconds 5 variables

{ Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into TIME_WAIT state and could not be used.

Lets say if there are 16 connections per second for 4 minutes there would be 

16*4*60=3840 connections killing the ports.

Solution to fix this:

First Solution:

The port range by max default is 5000, you can increase range of port. Lets say you can do it to 65534.

Use this key HKLM\System>CurrentControlSet>Services>Tcpip>Parameters>MaxUserPort 

Second Solution:

Second option is to reduce the time period of TIME_WAIT from 4 minutes to 20 seconds.

Use this key in this case:

HKLM>System>CurrentControlSet>Services>Tcpip>Parameters>TCPTimedWaitDelay

Third Solution:

3. Doing it trhough code if you do not want to get it done through registry is another solution.

See the code below which chooses the local end point for the

connection.

Req.ServicePoint.ConnectionBindingThroughCode

 = new BindIPEndPoint(BindIPEndPointCallback);

public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,

     IPEndPoint remoteEndPoint,

     int retryCount)

{

  int port = Interlocked.Increment(ref m_LastBindPortUsed); 

  Interlocked.CompareExchange(ref m_LastBindPortUsed, 4001, 54434);

  if(remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) {

 return new IPEndPoint(IPAddress.Any,port);

  }

  else {

 return new IPEndPoint(IPAddress.IPv6Any,port);

  }

}

by (1.4k points)  
...