A straightforward demo using Tcp

Server Side Code:

class Program
    {
        static void Main(string[] args)
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 9999);
            tcpListener.Start(2);
            while (true)
            {
                var tcpClient = tcpListener.AcceptTcpClient();
                var clientEndPoint = tcpClient.Client.RemoteEndPoint as IPEndPoint;
                Console.WriteLine("a tcp client has been accepted, who's IP and Port is {0},{1} respectively", clientEndPoint.Address.ToString(), clientEndPoint.Port);

                //If we call Send Method consecutively as follows, and if the client side doesn't call the Receive in time. The issue named 'Zhan Bao' will be introduced. 
                //tcpClient.Client.Send(new byte[] { 40 });
                //tcpClient.Client.Send(new byte[] { 41 });
                //Console.WriteLine("send message with length {0} to client", data.Length);

                var communicator = new ClientCommunicator(tcpClient);
                ThreadPool.QueueUserWorkItem(communicator.StartCommunicate);
            }
        }
    }

    class ClientCommunicator
    {
        private TcpClient _client;
        public ClientCommunicator(TcpClient client)
        {
            _client = client;
        }

        public void StartCommunicate(object state)
        {
            try
            {

                StartCommunicate();
            }
            catch (Exception)
            {
                //Just swallow the exception
            }
        }

        private void StartCommunicate()
        {
            while (true)
            {
                var bytes = new byte[256];
                var receivedLength = _client.Client.Receive(bytes);
                var msg = Encoding.UTF8.GetString(bytes, 0, receivedLength);
                if (msg == "quit")
                {
                    _client.Close();
                    break;
                }
                else
                {
                    var msgFeedback = string.Format("what do you mean by typing message:{0}", msg);
                    _client.Client.Send(Encoding.UTF8.GetBytes(msgFeedback));
                }
            }
        }
    }
View Code

Client Side Code:

class Program
    {
        static void Main(string[] args)
        {
            var tcpClient = new TcpClient();
            tcpClient.Connect(IPAddress.Loopback, 9999);

            int i = 0;
            while (true)
            {
                Thread.Sleep(3000);

                var clientMsg = string.Format("hello,{0}", i++);
                var clientBytes = Encoding.UTF8.GetBytes(clientMsg);
                tcpClient.Client.Send(clientBytes);

                var bytes = new byte[1000];
                var receivedLength = tcpClient.Client.Receive(bytes);
                var msg = Encoding.UTF8.GetString(bytes, 0, receivedLength);
                Console.WriteLine("The following message received from server:{0}", msg);
            }
        }
    }
View Code
原文地址:https://www.cnblogs.com/cnbwang/p/3663224.html