public void SetAddress(Uri uri) { IPAddress ipAddress; if (uri.Host == "*") ipAddress = IPAddress.IPv6Any; else if (!IPAddress.TryParse(uri.Host, out ipAddress)) { var availableAddresses = Dns.GetHostEntry(uri.Host).AddressList; // higher priority to IPv6 ipAddress = availableAddresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetworkV6); if (ipAddress == null) ipAddress = availableAddresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork); if (ipAddress == null) throw new NetMQException(ErrorCode.Invalid, string.Format("unable to find an IP address for {0}", uri.Host)); } m_endPoint = new IPEndPoint(ipAddress, uri.Port); try { m_handle = AsyncSocket.Create(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) { try { // This is not supported on old windows operation system and might throw exception m_handle.SetSocketOption(SocketOptionLevel.IPv6, IPv6Only, 0); } catch { } } m_handle.Bind(m_endPoint); m_handle.Listen(Options.Backlog); } catch (SocketException ex) { if (m_handle != null) { m_handle.Dispose(); m_handle = null; } throw NetMQException.FromSocketError(ex.SocketErrorCode); } }
/// <exception cref="InvalidException">Unable to parse the address's port number, or the IP address could not be parsed.</exception> /// <exception cref="NetMQException">Error establishing underlying socket.</exception> public void Init( string network) { m_address = new PgmAddress(network); m_pgmSocket = new PgmSocket(m_options, PgmSocketType.Listener, m_address); m_pgmSocket.Init(); m_handle = m_pgmSocket.Handle; try { m_handle.Bind(m_address.Address); m_pgmSocket.InitOptions(); m_handle.Listen(m_options.Backlog); } catch (SocketException ex) { Close(); throw NetMQException.Create(ex); } m_socket.EventListening(m_address.ToString(), m_handle); }
/// <summary> /// Set address to listen on. /// </summary> /// <param name="addr">a string denoting the address to set this to</param> public virtual void SetAddress([NotNull] string addr) { m_address.Resolve(addr, m_options.IPv4Only); try { m_handle = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Debug.Assert(m_handle != null); if (!m_options.IPv4Only && m_address.Address.AddressFamily == AddressFamily.InterNetworkV6) { try { // This is not supported on old windows operation system and might throw exception m_handle.SetSocketOption(SocketOptionLevel.IPv6, IPv6Only, 0); } catch { } } m_handle.ExclusiveAddressUse = false; m_handle.Bind(m_address.Address); m_handle.Listen(m_options.Backlog); // Copy the port number after binding in case we requested a system-allocated port number (TCP port zero) m_address.Address.Port = m_handle.LocalEndPoint.Port; m_endpoint = m_address.ToString(); m_socket.EventListening(m_endpoint, m_handle); m_port = m_handle.LocalEndPoint.Port; } catch (SocketException ex) { Close(); throw NetMQException.Create(ex); } }