コード例 #1
0
ファイル: TcpServer.cs プロジェクト: the-alex-mark/proglib-cs
        /// <summary>
        /// Метод получения данных от клиента.
        /// </summary>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SecurityException"></exception>
        private void Listener()
        {
            while (true)
            {
                try
                {
                    // Получение клиентского сокета
                    Socket Client = _server.Accept();

                    #region Получение входящих данных (Способ №1)

                    //// Получение входящих данных
                    //BytesBuilder Bytes = new BytesBuilder();
                    //while (Client.Available > 0)
                    //{
                    //    Byte[] Buffer = new Byte[50000];
                    //    Int32 Length = Client.Receive(Buffer);

                    //    Bytes.Append(Buffer, Length, false);
                    //}

                    #endregion

                    #region Получение входящих данных (Способ №2)

                    // Получение входящих данных
                    BytesBuilder Bytes  = new BytesBuilder();
                    Byte[]       Buffer = new Byte[1024];
                    Int32        Length = 0;
                    while ((Length = Client.Receive(Buffer)) > 0)
                    {
                        Bytes.Append(Buffer, Length, false);
                    }

                    #endregion

                    // Обработка события при получении данных
                    Receiver?.Invoke(this, new TcpReceiverEventArgs(Client, Bytes.ToArray(), Bytes.ToArray().Length));
                    Bytes.Dispose();

                    if (_data != null)
                    {
                        // Отправка данных клиенту
                        Client.Send(_data);
                    }

                    // Закрытие клиентского сокета
                    Client.Shutdown(SocketShutdown.Both);
                    Client.Close();
                }
                catch (Exception Exception)
                {
                    // Обработка события при возникновении ошибок в работе сервера
                    Error?.Invoke(this, new TcpErrorEventArgs(Exception));
                }
            }
        }
コード例 #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            P_CMD_Request           req  = new P_CMD_Request();
            OnlineUsersQueryRequest info = new OnlineUsersQueryRequest();
            var          data            = ESHelper.Pack(ref req, 1);
            BytesBuilder bb = new BytesBuilder();

            bb.Append(data
                      ).Append(ESHelper.Pack(ref info));

            this.SendMessage(bb.ToBytes());
        }