• 利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现


    上篇中使用了ThreadPool加上Socket同步方式实现多客户端和单服务器端通讯,稍加修改,得到异步编程模型实现方式

    主要使用到Socket的BeginSend, EndSend, BeginAccept, EndAccept, BeginReceive, EndReceive

    代码:

    // Server端

    namespace SocketAPMServer
    {
        
    public partial class Form1 : Form
        {
            Socket socket;
            
    public Form1()
            {
                InitializeComponent();
                InitSocket();
            }

            
    private void InitSocket()
            {
                socket 
    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPHostEntry ipHostEntry 
    = Dns.GetHostEntry(Dns.GetHostName());
                IPEndPoint ipEndPoint 
    = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
                socket.Bind(ipEndPoint);
                socket.Listen(
    20);
            }

            
    private void btnBeginRec_Click(object sender, EventArgs e)
            {
                ThreadPool.QueueUserWorkItem((_) 
    =>
                {
                    
    while (true)
                    {
                        Socket socketAccept 
    = null;
                        
    if (socket.Poll(-1, SelectMode.SelectRead))
                        {
                            socket.BeginAccept(arAcpt 
    => {
                                socketAccept 
    = socket.EndAccept(arAcpt);
                                
    if (socketAccept != null)
                                {
                                    ThreadPool.QueueUserWorkItem((o) 
    =>
                                    {
                                        
    while (true)
                                        {
                                            
    byte[] byteArray = new byte[100];
                                            socketAccept.BeginReceive(byteArray, 
    0, byteArray.Length, SocketFlags.None, (arRec) => {
                                                socketAccept.EndReceive(arRec);
                                                
    string strRec = System.Text.Encoding.UTF8.GetString(byteArray);
                                                
    if (this.txtMsg.InvokeRequired)
                                                {
                                                    
    this.txtMsg.Invoke(new ChangeText(ShowMsg), strRec);
                                                }
                                            }, 
    null);
                                            System.Threading.Thread.Sleep(
    100);
                                        }
                                    });
                                }
                            }, 
    null);
                        }
                    }
                });
                System.Threading.Thread.Sleep(
    100);
            }

            
    delegate void ChangeText(string obj);
            
    private void ShowMsg(string obj)
            {
                
    this.txtMsg.AppendText(obj + "  ");
            }
        }
    }

    // Client端

    namespace SocketAPMClient
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Socket socket 
    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPHostEntry ipHostEntry 
    = Dns.GetHostEntry(Dns.GetHostName());
                IPEndPoint ipEndPoint 
    = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
                socket.Connect(ipEndPoint);

                
    while (true)
                {
                    
    string input = Console.ReadLine();
                    
    try
                    {
                        
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(input);
                        socket.BeginSend(buffer, 
    0, buffer.Length, SocketFlags.None, (ar) => {
                            socket.EndSend(ar);
                        }, 
    null);
                    }
                    
    catch (Exception ex)
                    {
                        
    if (socket != null)
                        {
                            socket.Close();
                        }
                        Console.WriteLine(
    "Client Error: " + ex.Message);
                    }
                    System.Threading.Thread.Sleep(
    100);
                }
            }
        }
    }

    利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式

  • 相关阅读:
    合肥禹州珑玥湾业主qq群:791026131
    Operation category READ is not supported in state standby 故障解决
    yarn资源调度之FairScheduler
    storm启动和拓扑启动和关闭的一般命令
    es的一些实用案例
    leetCode例题引出的多线程CountDownLatch和CyclicBarrier使用例题
    spark运维管理
    spark streaming基础
    spark sql工作原理、性能优化和spark on hive----转载
    spark-sql基础
  • 原文地址:https://www.cnblogs.com/myself/p/2002142.html
Copyright © 2020-2023  润新知