Bug 40957 - Cannot listen to IPAddress.Any and IPAddress.IPV6Any
Summary: Cannot listen to IPAddress.Any and IPAddress.IPV6Any
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-05-09 00:56 UTC by Alan McGovern
Modified: 2016-07-31 16:32 UTC (History)
4 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Alan McGovern 2016-05-09 00:56:42 UTC
Testcase Create an IPV4 socket bound to IPAddress.Any and IPV6 socket bound to IPAddress.IPV6Any and have them both listen to port 5555. Sample code illustrating the issue: https://gist.github.com/alanmcgovern/8bbf6eb4a8e73539e350f40194bc381b

Expected result: Both sockets function and listen for incoming connections.

Actual result: Everything works fine on Mac, but on Linux i get an error saying the port is in use. The error I get on my raspberry pi is

[09/05/2016 00:50:55] error: Unhandled exception in BeginListeningAsync. InterNetwork
System.Net.Sockets.SocketException: Address already in use
  at System.Net.Sockets.Socket.Listen (Int32 backlog) <0x7363e3d0 + 0x00084> in <filename unknown>:0 
  at Tunez.RequestHandler+<BeginListeningAsync>c__async1.MoveNext () [0x0006e] in /Users/alan/tunez/Server/TunezServer/RequestHandler.cs:43 

Using Mono 4.4.0 (Stable Thu Apr 28 15:07:33 UTC 2016)
Comment 1 Alan McGovern 2016-05-09 01:06:08 UTC
I just realised that applying this patch makes it work on linux as expected. I'm not sure which behaviour is the correct one, but i presume it's related to dualmode sockets  and the default on Mac is different to Linux.

diff --git a/Server/TunezServer/RequestHandler.cs b/Server/TunezServer/RequestHandler.cs
index 22c5729..d57ea7f 100644
--- a/Server/TunezServer/RequestHandler.cs
+++ b/Server/TunezServer/RequestHandler.cs
@@ -24,6 +24,7 @@ namespace Tunez
                        var tcpListener = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        var tcp6Listener = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                       tcp6Listener.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, true);
                        using (tcpListener)
                        using (tcp6Listener) {

Note You need to log in before you can comment on or make changes to this bug.