예제 #1
1
        public static void SetKeepAliveValues(this Socket socket, bool useKeepAlive,  uint keepAliveTimeMs, uint keepAliveIntervalMs)
        {
            unsafe
            {
                TcpKeepAlive keepAliveValues = new TcpKeepAlive();

                keepAliveValues.On_Off = Convert.ToUInt32(useKeepAlive);
                keepAliveValues.KeepaLiveTime = keepAliveTimeMs;
                keepAliveValues.KeepaLiveInterval = keepAliveIntervalMs;

                byte[] inValue = new byte[12];

                for (int I = 0; I < 12; I++)
                    inValue[I] = keepAliveValues.Bytes[I];

                socket.IOControl(IOControlCode.KeepAliveValues, inValue, null);
            }
        }
예제 #2
0
        public static bool SetKeepAliveValues(this Socket socket, bool onOff, uint keepaLiveTime, uint keepAliveInterval)
        {
            var keepAliveValues = new TcpKeepAlive
            {
                OnOff             = Convert.ToUInt32(onOff),
                KeepAliveTime     = keepaLiveTime,
                KeepAliveInterval = keepAliveInterval,
            };


            try
            {
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, onOff);
                return(false);

#pragma warning disable CS0162 // Unreachable code detected
                int result = socket.IOControl(IOControlCode.KeepAliveValues, MarshalHelper.ToByteArray(keepAliveValues),
#pragma warning restore CS0162 // Unreachable code detected
                                              null);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #3
0
        private void Accept_Completed(object sender, SocketAsyncEventArgs e)
        {
            StateObject state = null;

            if (e.SocketError == SocketError.Success)
            {
                var io = _ioEventPool.Pop();
                state = (io.UserToken as StateObject);
                try
                {
                    System.Net.Sockets.Socket sock = e.AcceptSocket;
                    state.Handle = _acceptCount.CountAdd();
                    state.Socket = sock;
                    var option = new TcpKeepAlive
                    {
                        OnOff             = 1,
                        KeepAliveTime     = 5000,
                        KeepAliveInterval = 1000
                    };
                    state.Socket.IOControl(IOControlCode.KeepAliveValues, option.GetBytes(), null);
                    AddPeer(io);
                    OnAccepted(state);
                    BeginReceive(io);
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine("Accept Exception : " + ex.Message);
                    ClosePeer(state);
                }
                finally
                {
                    _allDone.Set();
                }
            }
        }
예제 #4
0
        public int SetKeepAliveValues
        (
            System.Net.Sockets.Socket Socket,
            bool On_Off,
            uint KeepaLiveTime,
            uint KeepaLiveInterval
        )
        {
            int Result = -1;

            unsafe
            {
                TcpKeepAlive KeepAliveValues = new TcpKeepAlive();

                KeepAliveValues.On_Off            = Convert.ToUInt32(On_Off);
                KeepAliveValues.KeepaLiveTime     = KeepaLiveTime;
                KeepAliveValues.KeepaLiveInterval = KeepaLiveInterval;

                byte[] InValue = new byte[12];

                for (int I = 0; I < 12; I++)
                {
                    InValue[I] = KeepAliveValues.Bytes[I];
                }

                Result = Socket.IOControl(IOControlCode.KeepAliveValues, InValue, null);
            }

            return(Result);
        }
예제 #5
0
        private static int SetKeepAliveValues
        (
            System.Net.Sockets.Socket Socket,
            bool On_Off,
            uint KeepaLiveTime,
            uint KeepaLiveInterval
        )
        {
            int Result = -1;

            unsafe
            {
                TcpKeepAlive KeepAliveValues = new TcpKeepAlive();

                KeepAliveValues.On_Off            = Convert.ToUInt32(On_Off);
                KeepAliveValues.KeepALiveTime     = KeepaLiveTime;
                KeepAliveValues.KeepALiveInterval = KeepaLiveInterval;

                byte[] InValue = new byte[12];

                //for (int I = 0; I < 12; I++)
                //InValue[I] = KeepAliveValues.Bytes[I];
                Array.Copy(KeepAliveValues.ToArray(), InValue, InValue.Length);

                Result = Socket.IOControl(System.Net.Sockets.IOControlCode.KeepAliveValues, InValue, null);
            }

            return(Result);
        }
예제 #6
0
 public void Connect(string ip, int port, int timeout = 5000)
 {
     try
     {
         if (IsConnect())
             return;
         this.ip = ip;
         this.port = port;
         _remoteEP = new IPEndPoint(IPAddress.Parse(ip), port);
         if (_stateObject.Socket == null)
         {
             System.Net.Sockets.Socket handler = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             IAsyncResult asyncResult = handler.BeginConnect(_remoteEP, null, null);
             if (asyncResult.AsyncWaitHandle.WaitOne(timeout, true))
             {
                 handler.EndConnect(asyncResult);
                 _stateObject.Socket = handler;
                 var option = new TcpKeepAlive
                 {
                     OnOff = 1,
                     KeepAliveTime = 5000,
                     KeepAliveInterval = 1000
                 };
                 _stateObject.Socket.IOControl(IOControlCode.KeepAliveValues, option.GetBytes(), null);
                 BeginReceive(_ioEvent);
                 OnConnected(_stateObject);
             }
             else
             {
                 _stateObject.Init();
                 throw new SocketException(10060);
             }
         }
         else
         {
             _stateObject.Init();
         }
     }
     catch (ArgumentNullException arg)
     {
         throw new Exception.Exception(arg.Message);
     }
     catch (SocketException se)
     {
         throw new Exception.Exception(se.Message);
     }
     catch (System.Exception e)
     {
         throw new Exception.Exception(e.Message);
     }
 }
예제 #7
0
 //extension removed, Missing System.Core.dll which requires .NET FW 3.5
 /// <summary>
 ///     Sets the Keep-Alive values for the current tcp connection
 /// </summary>
 /// <param name="socket">Current socket instance</param>
 /// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
 /// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
 public static void SetKeepAliveEx(Socket socket, uint keepAliveInterval, uint keepAliveTime)
 {
     var keepAlive = new TcpKeepAlive
     {
         onoff = 1,
         keepaliveinterval = keepAliveInterval,
         keepalivetime = keepAliveTime
     };
     int size = Marshal.SizeOf(keepAlive);
     IntPtr keepAlivePtr = Marshal.AllocHGlobal(size);
     Marshal.StructureToPtr(keepAlive, keepAlivePtr, true);
     var buffer = new byte[size];
     Marshal.Copy(keepAlivePtr, buffer, 0, size);
     Marshal.FreeHGlobal(keepAlivePtr);
     socket.IOControl(IOControlCode.KeepAliveValues, buffer, null);
 }
예제 #8
0
        public static void SetKeepAliveEx(this Socket socket, uint keepAliveInterval, uint keepAliveTime)
        {
            var keepAlive = new TcpKeepAlive
            {
                onoff             = 1,
                keepaliveinterval = keepAliveInterval,
                keepalivetime     = keepAliveTime
            };
            int    size         = Marshal.SizeOf(keepAlive);
            IntPtr keepAlivePtr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(keepAlive, keepAlivePtr, true);
            var buffer = new byte[size];

            Marshal.Copy(keepAlivePtr, buffer, 0, size);
            Marshal.FreeHGlobal(keepAlivePtr);
            socket.IOControl(IOControlCode.KeepAliveValues, buffer, null);
        }
예제 #9
0
        /// <summary>
        ///     Sets the Keep-Alive values for the current tcp connection
        /// </summary>
        /// <param name="socket">Current socket instance</param>
        /// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
        /// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
        public static void SetKeepAliveEx(Socket socket, uint keepAliveInterval, uint keepAliveTime)
        //extension removed, Missing System.Core.dll which requires .NET FW 3.5
        {
            var keepAlive = new TcpKeepAlive
            {
                onoff             = 1,
                keepaliveinterval = keepAliveInterval,
                keepalivetime     = keepAliveTime
            };
            int    size         = Marshal.SizeOf(keepAlive);
            IntPtr keepAlivePtr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(keepAlive, keepAlivePtr, true);
            var buffer = new byte[size];

            Marshal.Copy(keepAlivePtr, buffer, 0, size);
            Marshal.FreeHGlobal(keepAlivePtr);
            socket.IOControl(IOControlCode.KeepAliveValues, buffer, null);
        }
예제 #10
0
        /// <summary>
        /// Sets the keep alive values.
        /// </summary>
        /// <param name="state">if set to <c>true</c> [state].</param>
        /// <param name="keepAliveTime">The keep alive time.</param>
        /// <param name="keepAliveInterval">The keep alive interval.</param>
        /// <returns></returns>
        public int SetKeepAliveValues(bool state, int keepAliveTime, int keepAliveInterval)
        {
            if (keepAliveTime < 1000)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException("keepAliveTime");
            }
            if (keepAliveInterval < 1000)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException("keepAliveInterval");
            }

            TcpKeepAlive keepAlive = new TcpKeepAlive();

            keepAlive.State             = Convert.ToUInt32(state);
            keepAlive.KeepAliveTime     = Convert.ToUInt32(keepAliveTime);
            keepAlive.KeepAliveInterval = Convert.ToUInt32(keepAliveInterval);

            return(mSocket.IOControl(IOControlCode.KeepAliveValues, keepAlive.ToArray(), null));
        }
예제 #11
0
        public static bool SetKeepAliveValues(this Socket socket, bool onOff, uint keepAliveTime,
                                              uint keepAliveInterval)
        {
            var keepAliveValues = new TcpKeepAlive
            {
                OnOff             = Convert.ToUInt32(onOff),
                KeepAliveTime     = keepAliveTime,
                KeepAliveInterval = keepAliveInterval,
            };


            try
            {
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, onOff);
                var result = socket.IOControl(IOControlCode.KeepAliveValues, keepAliveValues.ToByteArray(), null);
                return(result > 0);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #12
0
        public static bool SetKeepAliveValues(Socket socket, bool onOff, uint keepaLiveTime, uint keepAliveInterval)
        {
            var keepAliveValues = new TcpKeepAlive
            {
                OnOff             = Convert.ToUInt32(onOff),
                KeepAliveTime     = keepaLiveTime,
                KeepAliveInterval = keepAliveInterval,
            };


            try
            {
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, onOff);
                int result = socket.IOControl(IOControlCode.KeepAliveValues, MarshalHelper.StructureToByteArray(keepAliveValues),
                                              null);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }