Reputation: 973
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:
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.
Upvotes: 2
Views: 2136
Reputation: 422
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);
Upvotes: 1
Reputation:
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
Upvotes: 1