Пример #1
0
        protected virtual void Dispose(bool disposing)
        {
            if (ClientStream != null)
            {
                ClientStream.Close();
                ClientStream.Dispose();
            }

            if (ServerStream != null)
            {
                ServerStream.Close();
                ServerStream.Dispose();
            }

            if (ClientTcpClient != null)
            {
                ClientTcpClient.Dispose();
            }

            if (ServerTcpClient != null)
            {
                ServerTcpClient.Dispose();
            }
        }
Пример #2
0
        protected static void tcpListen()
        {
            try
            {
                //IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
                //listener = new TcpListener(ipAddress, ServerTcpClient.odbTcpPort);
                //listener = new TcpListener(IPAddress.IPv6Any, ServerTcpClient.odbTcpPort);
                listener = new TcpListener(IPAddress.Any, SessionBase.s_serverTcpIpPortNumber);
                Socket       s            = listener.Server;
                LingerOption lingerOption = new LingerOption(true, 0);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
                s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
                listener.Start();
            }
            catch (System.Exception e)
            {
                ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
            }

            try
            {
                while (!ServerTcpClient.ShutDown)
                {
                    ServerTcpClient.s_acceptDone.Reset();
#if NET_COREx
                    //listener?.AcceptSocketAsync().Wait();
                    ServerTcpClient.AcceptTcpClient(listener);
#else
                    listener?.BeginAcceptTcpClient(new AsyncCallback(ServerTcpClient.AcceptTcpClient), listener);
#endif

                    ServerTcpClient.s_acceptDone.WaitOne();
                }
            }
            catch (SocketException e)
            {
#if !NET_COREx
                if (e.ErrorCode != 10054) // client closed socket
#endif
                {
                    ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
                }
            }
            catch (System.Exception e)
            {
                ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
            }
            finally
            {
                listener.Stop();
            }

            if (!stopService)
            {
                try
                {
                    listener?.Stop();
                    Environment.Exit(0);
                }
                catch (System.Exception e)
                {
                    ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
                }
            }
        }
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     ServerTcpClient.Close();
 }