0

I try to listen a specific port on server. I write a simple console app but I get error:

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

However, I don't find any process to listening that port. Resource manager and CurrPorts don't show any information.

My code (all code):

var ipEndPoint = new IPEndPoint(IPAddress.Any, 7001);
var tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpListener.Bind(ipEndPoint);
tcpListener.Listen(100);

My questions are:

  1. How can I find which process listen the port? Currports doesn't show, also resmon too.
  2. Why node.js is listen the port and getting messages? What is different?
  3. I think I have a hidden thread but I doesn't find it. I use ProcessExplorer.

Update:

When I run my console app after server reset, it is working correctly. However, when close and re-open the app, it is not working and given that exception above.

is_oz
  • 813
  • 1
  • 8
  • 27
  • This might help https://stackoverflow.com/questions/41836209/only-one-usage-of-each-socket-address-protocol-network-address-port-is-normall this question has been asked multiple times, check the other solutions on this forum – Pharaoh Jul 24 '18 at 14:18
  • Possible duplicate of [HttpClient: Only one usage of each socket address (protocol/network address/port) is normally permitted](https://stackoverflow.com/questions/26428827/httpclient-only-one-usage-of-each-socket-address-protocol-network-address-port) – Pharaoh Jul 24 '18 at 14:22
  • I try all possibilities in that posts. But still same. netstat -a doesnt shown my port. So first solution is not working. And second one is impossibble because I added my whole code. Only 4 lines. And no multithread, no recursive call, no real listening actually. I want to only bind that port. – is_oz Jul 24 '18 at 14:38
  • Can you bind to an other random port? Even when you can't see anything _listening_ to 7001, it's still possible an outgoing TCP connection is using 7001 as source port. Check the complete netstat output for that port. – huysentruitw Jul 24 '18 at 14:45
  • I try to 7002. It is ok to bind. `netstat -o -n -a | findstr 7001` shown no result. – is_oz Jul 24 '18 at 14:52

2 Answers2

1

I run into the same problem with the mysql port 3306. Neither tcpview , netstat -ano nor currports shows anything.

But

netsh interface ipv4 show excludedportrange protocol=tcp

shows that the port is reserved. It seems hyper-v / docker was the culprit and the fix is to disable hyper-v , reserve the port with

netsh int ipv4 add excludedportrange protocol=tcp startport=<your port> numberofports=1

and reenable hyper-v.

Origin : https://stackoverflow.com/a/54727281/32083 . More background is here https://github.com/docker/for-win/issues/3171

1

I believe you have connections to that port which are not completely closed. This prevents the port from being opened again. To solve the problem you have to set the socket option ReuseAddress before binding the port.

    var ipEndPoint = new IPEndPoint(IPAddress.Any, 7001);
    var tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    tcpListener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
    tcpListener.Bind(ipEndPoint);
    tcpListener.Listen(100);
Martin
  • 361
  • 1
  • 3
  • 7
  • I fix this problem 3 years ago but I forget to write here. Your answer is correct. SocketOption is fix problem. – is_oz Dec 02 '21 at 06:22