以前碰到一个问题,希望能够重用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 );
希望对有这个需求的TX有所帮助 :)
http://www.cnblogs.com/CuteNet/archive/2006/12/21/599102.html
时间: 2024-09-12 21:44:20