System.Net.Sockets.Socket.SetSocketOption C# (CSharp) Method

SetSocketOption() private method

private SetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent ) : void
optionLevel SocketOptionLevel
optionName SocketOptionName
optionValue int
silent bool
return void
        internal unsafe void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, $"optionLevel:{optionLevel} optionName:{optionName} optionValue:{optionValue} silent:{silent}");

            if (silent && (CleanedUp || _handle.IsInvalid))
            {
                if (NetEventSource.IsEnabled) NetEventSource.Info(this, "skipping the call");
                return;
            }
            SocketError errorCode = SocketError.Success;
            try
            {
                errorCode = SocketPal.SetSockOpt(_handle, optionLevel, optionName, optionValue);
                if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.setsockopt returns errorCode:{errorCode}");
            }
            catch
            {
                if (silent && _handle.IsInvalid)
                {
                    return;
                }
                throw;
            }

            // Keep the internal state in sync if the user manually resets this.
            if (optionName == SocketOptionName.PacketInformation && optionValue == 0 &&
                errorCode == SocketError.Success)
            {
                _receivingPacketInformation = false;
            }

            if (silent)
            {
                return;
            }

            // 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;
            }
        }

Same methods

Socket::SetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue ) : void
Socket::SetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, byte optionValue ) : void
Socket::SetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue ) : void
Socket::SetSocketOption ( SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue ) : void

Usage Example

        public bool initializeConnection()
        {
            try
            {
                m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

                m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                m_socket.SendTimeout = 5000;
                m_socket.ReceiveTimeout = 5000;

                m_frm.Print("Connecting to " + ECCServerIP + " at port " + ECCServerPort + " ...");
                m_socket.Connect(ECCServerIP, Convert.ToInt32(ECCServerPort));
            }
            catch (Exception ex)
            {
                m_frm.Print("Error!Failed to connect to ECC server!" + ex.Message);
                return false;
            }

            if (!Authenticate())
                return false;

            return true;
        }
All Usage Examples Of System.Net.Sockets.Socket::SetSocketOption