zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    数据库压力测试的参考地址
    Infopath表单部署到Farm的方法
    oracle 的几个开发工具比较
    智能Web算法/NLP 参考图书
    Wireshark & Ethereal包分析工具【图书节选】
    Sharepoint内置的”翻译管理库”体验
    开发相关“视频公开课webcast”资源地址
    读书:架构师的12项技能 12 ESSENTIAL SKILLS FOR SOFTWARE ARCHITECTS
    Linux 下Oracle Client JAVA JDBC 集成点滴
    MOS2010的界面介绍和定制方法简介【资料汇集】
  • 原文地址:https://www.cnblogs.com/cnbwang/p/3663224.html
Copyright © 2011-2022 走看看