惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - o0myself0o

生产者消费者模式,代码中碰到的疑问(已解决) 利用线程池实现多客户端和单服务器端Socket通讯(一):同步方式 Entity Framework 4.0 ObjectContext下的各种方法实践 题目:若干个不重复数,打乱顺序输出 wtf js(四) - o0myself0o - 博客园 wtf js(三) number的类型不是number wtf js(二) 算法:给定两个已从小到大排好序的整型数组arrA和arrB,将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序 应用中的单例模式 面试题:给你三个bool类型变量a, b, c,判断至少有两个为true javascript面向对象编程(一) - o0myself0o - 博客园 wtf js(一) - o0myself0o - 博客园 社区网站功能实现系列(三):社区页面无刷新回发的一种实现方式 社区网站功能实现系列(二):社区内容分享到别的SNS 社区网站功能实现系列(一):多国语言的实现 反射获取Class中Property的值 A*寻路初探 闲谈ASP.NET 2.0缓存技术 使用 jQuery 简化 Ajax 开发
利用线程池实现多客户端和单服务器端Socket通讯(二):异步编程模型实现
o0myself0o · 2011-04-01 · via 博客园 - o0myself0o

上篇中使用了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通讯(一):同步方式