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
  • 相关阅读:
    修改weblogic jvm启动参数
    weblogic部署步骤
    weblogic安装步骤
    sqldeveloper 导出数据库
    VO与PO 的理解
    【Hyper-V】与【VirtualBox】【VMware】冲突的解决方法
    xstream 实现simplebean2xml ,xml2simplebean
    eclipse中SVN报错解决
    点击eclipse包报错
    myeclipce项目导入eclipse中报错
  • 原文地址:https://www.cnblogs.com/cnbwang/p/3663224.html
Copyright © 2011-2022 走看看