Example #1
0
        public async Task <bool> Conenct(string ip)
        {
            if (sock != null)
            {
                sock.Events.Connected    -= Sock_Connected;
                sock.Events.DataReceived -= Sock_DataReceived;
                sock.Events.Disconnected -= Sock_Disconnected;
                sock.Dispose();
                sock = null;
            }
            sock = new SimpleTcpClient(_ip, 7171, false, null, null);
            sock.Events.Connected    += Sock_Connected;
            sock.Events.DataReceived += Sock_DataReceived;
            sock.Events.Disconnected += Sock_Disconnected;
            try
            {
                cancelTock = new CancellationTokenSource();
                if (ip == "0.0.0.0")
                {
                    return(false);
                }
                await Task.Run(() => sock.Connect());

                ParsRun();
                return(true);
            }
            catch
            {
                sock.Dispose();
                sock = null;
                Debug.WriteLine($"{ip}:7171 Client 소켓연결 실패.");
                return(false);
            }
        }
Example #2
0
        /// <inheritdoc />
        public override void Dispose()
        {
            base.Dispose();

            _tcpClient.Disconnect();
            _tcpClient.Dispose();
        }
        public void Dispose()
        {
            _socket.Disconnect();
            _socket.Close();

            _socket = null;

            if (_sender != null)
            {
                _sender.Disconnect();
                _sender.Dispose();

                _sender = null;
            }

            if (_receiver != null)
            {
                if (_receiver.IsStarted)
                {
                    _receiver.Stop();
                }

                _receiver = null;
            }
        }
Example #4
0
        public void Init(string serverIpPort)
        {
            if (!serverIpPort.CheckIpPort())
            {
                throw new ArgumentException($"IpPort Error : {serverIpPort}");
            }

            _disposables?.Dispose();
            _disposables = new CompositeDisposable();

            _client?.Dispose();


            _client = new SimpleTcpClient(serverIpPort);

            var connect = Observable
                          .FromEventPattern <ClientConnectedEventArgs>(h => _client.Events.Connected += h,
                                                                       h => _client.Events.Connected -= h)
                          .Select(_ => true)
            ;

            var disconnect = Observable
                             .FromEventPattern <ClientDisconnectedEventArgs>(h => _client.Events.Disconnected += h,
                                                                             h => _client.Events.Disconnected -= h)
                             .Select(_ => false);


            connect
            .Merge(disconnect)
            .StartWith(false)
            .DistinctUntilChanged()
            .SubscribeOn(NewThreadScheduler.Default)
            .Subscribe(s => _connectionState.OnNext(s))
            .DisposeWith(_disposables);


            Observable
            .FromEventPattern <DataReceivedEventArgs>(h => _client.Events.DataReceived += h,
                                                      h => _client.Events.DataReceived -= h)
            .Select(p => p.EventArgs.Data)
            .SubscribeOn(NewThreadScheduler.Default)
            .Subscribe(b => _received.OnNext(b))
            .DisposeWith(_disposables);
        }
Example #5
0
 public bool Connect(string hostName, int port)
 {
     if (Client != null)
     {
         Client.Dispose();
     }
     ServerAddr            = hostName;
     ServerPort            = port;
     Client                = new SimpleTcpClient <T>(hostName, port);
     Client.ReceivePacket += Switch;
     Client.Disconnect    += Client_Disconnect;
     if (Client.Connect())
     {
         Connected?.Invoke(this, "OK");
         return(true);
     }
     else
     {
         return(false);
     }
 }
 public void connectToServer()
 {
     if (!client.IsConnected)
     {
         try
         {
             client.Connect();
         }
         catch (Exception ex)
         {
             client.Dispose();
         }
     }
 }
Example #7
0
 public void StartServer(string ip = "127.0.0.1", int port = 53399)
 {
     try
     {
         if (Client == null)
         {
             Client = new SimpleTcpClient().Connect(ip, port);
         }
         else
         {
             Client.Disconnect();
             Client.Dispose();
             Client = new SimpleTcpClient().Connect(ip, port);
         }
     }
     catch (Exception e)
     {
         Server.SetInfoText($"ServerERR: {e.Message}");
     }
 }
        /// <summary>
        /// 소켓 환경 설정 및 이벤트 등록하는 함수
        /// </summary>
        /// <returns></returns>
        private async Task InitCommSetting()
        {
            if (TcpClientSocket != null)
            {
                TcpClientSocket.Dispose();
            }

            TcpClientSocket = new SimpleTcpClient(iep.Address.ToString(), iep.Port, false, null, null);

            TcpClientSocket.Events.Connected    += ServerConnected;
            TcpClientSocket.Events.Disconnected += ServerDisconnected;
            TcpClientSocket.Events.DataReceived += MessageReceived;

            TcpClientSocket.Keepalive.EnableTcpKeepAlives    = true;
            TcpClientSocket.Keepalive.TcpKeepAliveInterval   = 5;
            TcpClientSocket.Keepalive.TcpKeepAliveTime       = 5;
            TcpClientSocket.Keepalive.TcpKeepAliveRetryCount = 5;

            TcpClientSocket.Connect();
            await Task.Delay(1000);
        }
Example #9
0
 public void Connect()
 {
     try
     {
         var r = Client?.TcpClient.Connected;
         if (r != true)
         {
             if (Client != null)
             {
                 Client.Disconnect();
                 Client.Dispose();
             }
             Client = new SimpleTcpClient().Connect(Properties.Settings.Default.CAMERA_IP, Properties.Settings.Default.CAMERA_PORT);
         }
     }
     catch (SocketException ex)
     {
         Events.Publish(new MsgItem {
             Level = "E", Time = DateTime.Now, Value = ex.Message
         });
     }
 }
Example #10
0
 public void Dispose()
 {
     cancelTock.Cancel();
     sock?.Dispose();
     sock = null;
 }
Example #11
0
 private void CloseConnection()
 {
     client.Dispose();
     client.Disconnect();
     Settings.IsClient = false;
 }
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     client.Disconnect();
     Logger(DateTime.Now.ToString() + " Programm close" + Environment.NewLine);
     client.Dispose();
 }
Example #13
0
 private void PlayStateNotifier_OnPlaymodeExit()
 {
     PlayStateNotifier.OnPlaymodeExit -= PlayStateNotifier_OnPlaymodeExit;
     _client?.Disconnect();
     _client?.Dispose();
 }
 public void Disconnect()
 {
     Client.Disconnect();
     Client.Dispose();
 }
Example #15
0
        static void Main(string[] args)
        {
            _ServerIp   = InputString("Server IP   :", "127.0.0.1", false);
            _ServerPort = InputInteger("Server Port :", 9000, true, false);
            _Ssl        = InputBoolean("Use SSL     :", false);

            if (_Ssl)
            {
                _PfxFilename = InputString("PFX Certificate File:", "simpletcp.pfx", false);
                _PfxPassword = InputString("PFX File Password:"******"simpletcp", false);
            }

            _Client = new SimpleTcpClient(_ServerIp, _ServerPort, _Ssl, _PfxFilename, _PfxPassword);

            _Client.Events.Connected    += Connected;
            _Client.Events.Disconnected += Disconnected;
            _Client.Events.DataReceived += DataReceived;

            _Client.Settings.MutuallyAuthenticate      = false;
            _Client.Settings.AcceptInvalidCertificates = true;
            _Client.Logger = Logger;
            _Client.Connect();

            while (_RunForever)
            {
                string userInput = InputString("Command [? for help]:", null, false);
                switch (userInput)
                {
                case "?":
                    Menu();
                    break;

                case "q":
                case "Q":
                    _RunForever = false;
                    break;

                case "c":
                case "C":
                case "cls":
                    Console.Clear();
                    break;

                case "send":
                    Send();
                    break;

                case "sendasync":
                    SendAsync();
                    break;

                case "connected":
                    IsConnected();
                    break;

                case "dispose":
                    _Client.Dispose();
                    break;

                case "stats":
                    Console.WriteLine(_Client.Statistics.ToString());
                    break;

                case "stats reset":
                    _Client.Statistics.Reset();
                    break;
                }
            }
        }
Example #16
0
 // Ham nhan tat ca moi thu phan hoi ve
 private void Client_DataReceived(object sender, SimpleTCP.Message e)
 {
     MessageBox.Show(e.MessageString);
     client.Disconnect();
     client.Dispose();
 }
 /// <summary>
 /// Kill the client. Brutal!
 /// </summary>
 public void Dispose()
 {
     client.Dispose();
 }