System.Net.Sockets.Socket.GetSocketOption C# (CSharp) Méthode

GetSocketOption() public méthode

public GetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName ) : object
optionLevel SocketOptionLevel
optionName SocketOptionName
Résultat object
        public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
        {
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger)
            {
                return GetLingerOpt();
            }
            else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                return GetMulticastOpt(optionName);
            }
            else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
            {
                // Handle IPv6 case
                return GetIPv6MulticastOpt(optionName);
            }

            int optionValue = 0;

            // This can throw ObjectDisposedException.
            SocketError errorCode = SocketPal.GetSockOpt(
                _handle,
                optionLevel,
                optionName,
                out optionValue);

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.getsockopt returns errorCode:{errorCode}");
        
            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }

            return optionValue;
        }

Same methods

Socket::GetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength ) : byte[]
Socket::GetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, byte optionValue ) : void

Usage Example

 protected static int SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, int value)
 {
     if (((int)socket.GetSocketOption(level, name)) == value)
         return value;
     socket.SetSocketOption(level, name, value);
     return (int)socket.GetSocketOption(level, name);
 }
All Usage Examples Of System.Net.Sockets.Socket::GetSocketOption