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); } }
/// <summary> /// Perform initialization of this PgmSocket, including creating the socket handle. /// </summary> internal void Init() { #if DEBUG // Don't want to bloat the code with excessive debugging information, unless this is a DEBUG build. jh try { #endif Handle = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PgmProtocolType); #if DEBUG } catch (SocketException x) { string xMsg = $"SocketException with SocketErrorCode={x.SocketErrorCode}, Message={x.Message}, in PgmSocket.Init, within AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PGM_PROTOCOL_TYPE), {this}"; Debug.WriteLine(xMsg); // If running on Microsoft Windows, suggest to the developer that he may need to install MSMQ in order to get PGM socket support. #if NETSTANDARD1_3 bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); #else PlatformID p = Environment.OSVersion.Platform; bool isWindows = true; switch (p) { case PlatformID.Win32NT: break; case PlatformID.Win32S: break; case PlatformID.Win32Windows: break; default: isWindows = false; break; } #endif if (isWindows) { Debug.WriteLine("For Microsoft Windows, you may want to check to see whether you have installed MSMQ on this host, to get PGM socket support."); } throw new FaultException(innerException: x, message: xMsg); } #endif Handle.ExclusiveAddressUse = false; Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); }
/// <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); } }