コード例 #1
0
        private void OnConnected(IAsyncResult AsyncResult)
        {
            Socket socketHandler = null;

            try
            {
                socketHandler = this._listener.EndAccept(AsyncResult);
                _connected.Set();

                IPEndPoint iPEndPoint = (IPEndPoint)socketHandler.RemoteEndPoint;
                Console.WriteLine("socket connected from {0}!", iPEndPoint.Address.ToString());

                ConfigureHandlerSocket(socketHandler);
                var infoHost = SocketInfoHost.Create(socketHandler);
                socketHandler.BeginReceive(infoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), infoHost);
            }

            catch (Exception e)
            {
                _connected.Set();
                if (socketHandler != null)
                {
                    socketHandler.Close();
                }
                _log.Error(e.Message);
            }
            //finally
            //{
            //    ThreadPool.QueueUserWorkItem(new WaitCallback(this.BeginAccept), null);
            //}
        }
コード例 #2
0
        private void OnReceived(IAsyncResult ar)
        {
            SocketInfoHost infoHost = (SocketInfoHost)ar.AsyncState;
            Socket         handler  = infoHost.SocketHandler;
            int            read     = handler.EndReceive(ar);

            var content = Encoding.ASCII.GetString(infoHost.buffer, 0, read);

            if (content.Length > 30)
            {
                _log.Warn(content);
            }
            //content = content.Trim('\0');
            //if (!string.IsNullOrEmpty(content))

            infoHost.Storage.Append(content);
            //All of the data has been read, construct SocketInfoHost
            infoHost.PunchTimeStamp();
            _infoRecords.Add(infoHost);
            Console.WriteLine("received " + infoHost.Value);

            SocketInfoHost newInfoHost = SocketInfoHost.Create(handler);

            handler.BeginReceive(newInfoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), newInfoHost);
            //  CloseHandle(handler);
        }
コード例 #3
0
        private void OnConnectionAccepted(IAsyncResult AsyncResult)
        {
            Socket socketHandler = null;

            try
            {
                socketHandler = this._listener.EndAccept(AsyncResult);
                ThreadPool.QueueUserWorkItem(new WaitCallback(this.BeginAccept), null);
                Console.WriteLine("socket connected!");
                IPEndPoint iPEndPoint = (IPEndPoint)socketHandler.RemoteEndPoint;

                InitializeHandlerSocker(socketHandler);
                SocketInfoHost buffer = new SocketInfoHost(socketHandler);
                socketHandler.BeginReceive(buffer.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), buffer);
            }
            catch (Exception e)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(this.BeginAccept), null);
                if (socketHandler != null)
                {
                    socketHandler.Close();
                }
                _log.Warn(e.Message);
            }
            //finally
            //{
            //    ThreadPool.QueueUserWorkItem(new WaitCallback(this.BeginAccept), null);
            //}
        }
コード例 #4
0
        private void OnConnectionAccepted1(IAsyncResult AsyncResult)
        {
            Socket socketHandler = null;

            try
            {
                socketHandler = this._listener.EndAccept(AsyncResult);
                //this._listener.BeginAccept(new AsyncCallback(this.OnConnectionAccepted1), this._listener);
                _connected.Set();
                IPEndPoint iPEndPoint = (IPEndPoint)socketHandler.RemoteEndPoint;
                Console.WriteLine("socket connected from {0}!", iPEndPoint.ToString());

                InitializeHandlerSocker(socketHandler);
                SocketInfoHost hostInfo = SocketInfoHost.Create(socketHandler);
                socketHandler.BeginReceive(hostInfo.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), hostInfo);
            }

            catch (Exception e)
            {
                _connected.Set();
                if (socketHandler != null)
                {
                    socketHandler.Close();
                }
                _log.Error(e.Message);
            }
        }
コード例 #5
0
        private void OnReceived(IAsyncResult ar)
        {
            SocketInfoHost infoHost = (SocketInfoHost)ar.AsyncState;

            try
            {
                Socket handler = infoHost.SocketHandler;
                int    read    = handler.EndReceive(ar);
                //TODO when sending content is larger than receiving buffer size
                //if (read > 0)
                //{
                //    infoHost.Storage.Append(Encoding.ASCII.GetString(infoHost.buffer, 0, read));
                //    handler.BeginReceive(infoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), infoHost);
                //}
                //else
                //{
                //    if (infoHost.Storage.Length > 0)
                //    {
                //        //All of the data has been read, construct SocketInfoHost
                //        infoHost.PunchTimeStamp();
                //        _infoRecords.Add(infoHost);
                //        Console.WriteLine("received " + infoHost.Value);

                //    }
                //    handler.Close();
                //}
                infoHost.Storage.Append(Encoding.ASCII.GetString(infoHost.buffer, 0, read));
                infoHost.PunchTimeStamp();
                _infoRecords.Add(infoHost);
                var contentReceived = infoHost.Storage.ToString();
                Console.WriteLine("received " + contentReceived);

                if (contentReceived.Length > 10)
                {
                    _log.Warn(contentReceived);
                }

                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception ex)
            {
                _log.Error("Receive Error " + infoHost.Storage.ToString(), ex);
            }
        }