コード例 #1
0
ファイル: SocketHelp.cs プロジェクト: brzz/SimpleSocket.Net
        private static void _ServerAccept(Object arg)
        {
            SocketHelp _this = (SocketHelp)arg;

            _this.ClientList = new List <ClientStatus>();

            _this.SocketThis.Bind(_this.endp);
            _this.SocketThis.Listen(20);

            Console.WriteLine("Port:{0} Bind Success Wait Client...", _this.endp.Port);

            while (_this._Open)
            {
                ClientStatus c =
                    new ClientStatus(_this.SocketThis.Accept(), new Thread(ClientRec));

                c._this = _this;
                _this.ClientList.Add(c);

                c.clientThread.Start(c);
            }


            _this.ClientList.Clear();
            _this.ClientList = null;

            _this.SocketThis.Close();
        }
コード例 #2
0
ファイル: SocketHelp.cs プロジェクト: brzz/SimpleSocket.Net
        private static void ClientRecv(Object arg)
        {
            SocketHelp _this = (SocketHelp)arg;

            try
            {
                _this.SocketThis.Connect(_this.endp);
            }
            catch (Exception e)
            {
                if (_this.onRecErr != null)
                {
                    _this.onRecErr.Invoke(e, _this, _this.SocketThis);
                }
            }



            byte[] buffer  = new byte[1024 * 8];
            int    recByte = 0;

            while (_this._Open && _this.onPacketRec != null)
            {
                try
                {
                    recByte = _this.SocketThis.Receive(buffer);
                    if (recByte <= 0)
                    {
                        throw new SocketException();
                    }
                    if (_this.onPacketRec != null && recByte > 0)
                    {
                        _this.onPacketRec.Invoke(_this.SocketThis, buffer, recByte);
                    }
                    recByte = 0;
                    //threadclient.Start();
                }
                catch (Exception e)
                {
                    if (_this.onRecErr != null)
                    {
                        _this.onRecErr.Invoke(e, _this, _this.SocketThis);
                    }
                    else
                    {
                        _this._Open = false;
                    }
                }
            }
        }