C System. Net. Sockets = TcpListener usage

Time:2019-10-4
Private TcpListener_listener;#region initialization listener 
 public override void Init()
        {
            try
            {
                DevInfo.Read();
               var localaddr = IPAddress.Parse(Ip);
                var port = Convert.ToInt32(port);
                _listener = new TcpListener(localaddr, port);
            }
            finally
            {
                StatusChangedEvent?.Invoke(Status);
            }
        }
#endregion

Start running

        public override void Start()
        {
            Init();
            _listener?.Start();
            Task.Factory.StartNew(MonitTcpClientConnect);           
        }

Monitor

        private void MonitTcpClientConnect()
        {
            while (true)
            {
                try
                {
                    var client = _listener.AcceptTcpClient();
                    Task.Factory.StartNew(MonitorRecvData, client);
                }
                catch
                {
                    _listener.Stop();
                    _listener = null;                return;
                }
            }
        }

Handling events

private void MonitorRecvData(object obj)
        {
            try
            {
                var tcpClient = obj as TcpClient;
                if (tcpClient == null)
                {
                    return;
                }

                var buffer = new byte[1024];var stream = tcpClient.GetStream();
                while (true)
                {
                    var count = stream.Read(buffer, 0, buffer.Length);

                    if (count == 0)
                    {
                        Throw new Exception ("client offline");
                    }

                    var str = Encoding.ASCII.GetString(buffer, 0, count);
                }
            }
            catch
            {
                // ignored
            }
        }

 

Recommended Today

Freeanchor: abandon single IOU matching, more free anchor matching method 𞓜 nips 2019

This paper abandons the previous method of assigning anchor and gt matching relationship according to the rigidity of IOU, and proposes freeanchor method to match more freely. This method defines the training of target detection as the maximum likelihood estimation (MLE) process, and simultaneously learns target classification, target detection and matching relationship from end to […]