以前碰到一個問題,希望能夠重用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