Пример #1
0
        private void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            ClientBase client   = (ClientBase)sender;
            int        clientId = clientTable[client];

            MessageFromClientEventArgs args = new MessageFromClientEventArgs(e.RawMessage, client, clientId);

            MessageFromClient?.Invoke(this, args);
        }
Пример #2
0
        /// <summary>
        /// Get message from client
        /// </summary>
        public void Start()
        {
            tcpSocket.Bind(tcpEndpoint);
            tcpSocket.Listen(1);

            while (true)
            {
                Socket Listener      = tcpSocket.Accept();
                byte[] receivedBytes = new byte[128];
                var    size          = 0;
                var    data          = new StringBuilder();

                do
                {
                    size = Listener.Receive(receivedBytes);
                    data.Append(Encoding.UTF8.GetString(receivedBytes, 0, size));
                } while (Listener.Available > 0);

                MessageFromClient?.Invoke(data.ToString());
                Listener.Shutdown(SocketShutdown.Both);
                Listener.Close();
            }
        }