Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a WCF service and a Web application. Web application makes calls to this WCF service in a continous manner a.k.a polling. In our production environment, I receive this error very rarely. Since, this is an internal activity users were not aware of when this error is thrown.

Could not connect to http://localhost/QAService/Service.svc. TCP error code 10048: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80

I am having trouble in reproducing this behaviour in our dev/qa environment. I have made sure that the client connection is closed in a try..catch..finally block. Still don't understand what is causing this issue .. any one aware of this?

Note: I've looked at this SO question, but not seems to be answering my problem, so it is not repeated questions.

share|improve this question
Is that because the web service is using port 80, which is in use by IIS? Which port does your service use in production? Which port, is IIS configured on in production? – shahkalpesh Aug 27 '09 at 6:20
It is same 80 for both applications. WCF service is configured as a virtual directory inside the root in which the site is hosted. So, you're saying there could be occasionally a conflict between both web page request and a request to service from web page trying to use same port? – asyncwait Aug 27 '09 at 6:23
I have encountered a similar situation. Planning to use net tcp port sharing feature in wcf service. msdn.microsoft.com/en-us/library/ms734772.aspx Please let me know if this works – user272363 Feb 13 '10 at 11:10

1 Answer

up vote 25 down vote accepted

You are overloading the TCP/IP stack. Windows (and I think all socket stacks actually) have a limitation on the number of sockets that can be opened in rapid sequence due to how sockets get closed under normal operation. Whenever a socket is closed, it enters the TIME_WAIT state for a certain time (240 seconds IIRC). Each time you poll, a socket is consumed out of the default dynamic range (I think its about 5000 dynamic ports just above 1024), and each time that poll ends, that particular socket goes into TIME_WAIT. If you poll frequently enough, you will eventually consume all of the available ports, which will result in TCP error 10048.

Generally, WCF tries to avoid this problem by pooling connections and things like that. This is usually the case with internal services that are not going over the internet. I am not sure if any of the wsHttp bindings support connection pooling, but the netTcp binding should. I would assume named pipes does not run into this problem. I couldn't say for the MSMQ binding.

There are two solutions you can use to get around this problem. You can either increase the dynamic port range, or reduce the period of TIME_WAIT. The former is probably the safer route, but if you are consuming an extremely high volume of sockets (which doesn't sound like the case for your scenario), reducing TIME_WAIT is a better option (or both together.)

Changing the Dynamic Port Range

  1. Open regedit.
  2. Open key HKLM\System\CurrentControlSet\Services\Tcpip\Parameters
  3. Edit (or create as DWORD) the MaxUserPort value.
  4. Set it to a higher number. (i.e. 65534)

Changing the TIME_WAIT delay

  1. Open regedit.
  2. Open key HKLM\System\CurrentControlSet\Services\Tcpip\Parameters
  3. Edit (or create as DWORD) the TCPTimeWaitDelay.
  4. Set it to a lower number. Value is in seconds. (i.e. 60 for 1 minute delay)

One of the above solutions should fix your problem. If it persists after changing the port range, I would see try increasing the period of your polling so it happens less frequently...that will give you more leeway to work around the time wait delay. I would change the time wait delay as a last resort.

share|improve this answer
Thanks for your elaborate answer – asyncwait Aug 28 '09 at 9:07
Uhm... this link msdn.microsoft.com/en-us/library/aa560610(v=bts.20).aspx spells the parameter TCPTimeWaitDelay differently. It spells it TcpTimedWaitDelay. How can I get confirmation which it is? More info from technet: technet.microsoft.com/en-us/library/cc938217.aspx – Jaans Jul 9 at 8:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.