以前碰到一个问题,希望能够重用UDP的端口号,但一直没有成功。 今天偶然看到一段代码,竟然可以达到目的,欣喜中记下: IPAddress groupAddress = IPAddress.Parse( "239.0.84.11" ); Socket Listener = new Socket( AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp ); Listener.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1 ); Listener.Bind( new IPEndPoint( IPAddress.Any, 8411 ) ); MulticastOption multicastOption = new MulticastOption( groupAddress ); Listener.SetSocketOption( SocketOptionLevel.IP,SocketOptionName.AddMembership,multicastOption ); |