Skip to content

Instantly share code, notes, and snippets.

@justincc
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justincc/c431165a6d7194169328 to your computer and use it in GitHub Desktop.
Save justincc/c431165a6d7194169328 to your computer and use it in GitHub Desktop.
Example program to bind two sockets to same UDP port
using System;
using System.Net;
using System.Net.Sockets;
public class UdpSamePort
{
public static void Main(string[] args)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9000);
Socket udpSocket1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSocket1.Bind(ipep);
Socket udpSocket2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Uncomment this line causes the udpSocket2 bind to throw an exception complaining that the address is already in use.
//udpSocket2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
udpSocket2.Bind(ipep);
}
}
@justincc
Copy link
Author

This fails on Windows with the expected "Only one usage of each socket address is normally permitted" message. At least on Mono 3.2.8 this raises no error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment