public void Listen(IPAddress ipAddress, int port) { lock (listeners) { IPEndPoint endpoint = new IPEndPoint(ipAddress, port); if (listeners.ContainsKey(endpoint)) { UdpSocketListener listener = listeners[endpoint]; listener.AddRef(); } else { UdpSocketListener listener = new UdpSocketListener(ipAddress, port, false, MaxBufferPoolSize, MaxMessageSize, dataReceivedCallback); listener.Open(); listener.AddRef(); listeners.Add(endpoint, listener); } } }
/// <summary> /// Used to get a unique uri (by CompositeDuplexChannelFactory for example). /// We get a unique TCP port by binding to "port 0" /// </summary> public void InitializeUniqueUri(string host) { if (host == null) { throw new ArgumentNullException("host"); } this.listenSockets = new List <Socket>(); int port; lock (this.ThisLock) { IPAddress ipAddress = null; if (IPAddress.TryParse(host, out ipAddress)) { Socket socket = UdpSocketListener.CreateListenSocket( ipAddress, 0, this.multicast); port = ((IPEndPoint)socket.LocalEndPoint).Port; listenSockets.Add(socket); } else { Socket socket = UdpSocketListener.CreateListenSocket( IPAddress.Any, 0, this.multicast); port = ((IPEndPoint)socket.LocalEndPoint).Port; listenSockets.Add(socket); if (Socket.OSSupportsIPv6) { listenSockets.Add(UdpSocketListener.CreateListenSocket( IPAddress.IPv6Any, port, this.multicast)); } } } UriBuilder uriBuilder = new UriBuilder(Scheme, host, port); InitializeUri(uriBuilder.Uri, String.Empty); }
public void Open() { DataReceivedCallback callback = new DataReceivedCallback(OnDataReceived); if (listenSockets != null) { socketListener = new UdpSocketListener(this.listenSockets, this.maxBufferSize, this.maxMessageSize, callback); } else { IPAddress address = IPAddress.Broadcast; if (this.ListenUri.HostNameType == UriHostNameType.IPv4 || this.ListenUri.HostNameType == UriHostNameType.IPv6) { address = IPAddress.Parse(this.ListenUri.DnsSafeHost); } socketListener = new UdpSocketListener(address, this.ListenUri.Port, this.multicast, this.maxBufferSize, this.maxMessageSize, callback); } socketListener.Open(); }