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

推荐订阅源

D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
P
Privacy & Cybersecurity Law Blog
Hugging Face - Blog
Hugging Face - Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
A
Arctic Wolf
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
The GitHub Blog
The GitHub Blog
P
Privacy International News Feed
WordPress大学
WordPress大学
U
Unit 42
S
Securelist
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
小众软件
小众软件
Know Your Adversary
Know Your Adversary
The Cloudflare Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
Security Latest
Security Latest
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - Franky
Y
Y Combinator Blog
博客园 - 叶小钗
Security Archives - TechRepublic
Security Archives - TechRepublic
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
S
Secure Thoughts
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
M
MIT News - Artificial intelligence

博客园 - lichdr

高德地图热力图的Flutter实现 App消息推送的简单实现 nginx访问日志过滤(多条件) CAS的service参数验证 自定义Token的CAS登录 单元格的计算 表格行列的删除 表格行列的移动 FastReport的一些另类用法 FastReport分组页码 RFID会议签到系统总结(二十二)――系统中的模式 RFID会议签到系统总结(二十)――数据窗体状态控制 RFID会议签到系统总结(十九)――单数据窗体 RFID会议签到系统总结(十八)――菜单与工具栏的加载 RFID会议签到系统总结(十七)――菜单与工具栏的改造(下) RFID会议签到系统总结(十六)――菜单与工具栏按钮的改造(上) RFID会议签到系统总结(十五)――管控端的窗体组织 RFID会议签到系统总结(十四)――管控端业务模块的加载 RFID会议签到系统总结(十三)――模块概述(下)
RFID会议签到系统总结(二十一)――服务端的通讯
lichdr · 2007-07-30 · via 博客园 - lichdr

这一篇其实没什么可讲的,只提一下跟客户端不太一样的一些地方。

服务端跟客户端最大的区别是它面对的不是单单一个连接,而是有一些个连接。对于接收与发送来讲它是要具体到accept进来的每一个连接的,所以这里有一个SocketStateObject参数会贯穿始终,这个参数主要就是放对应客户端的Socket连接及一些状态变量,在accept进来一个连接后即创建一个这个对象。

       public void Listen(int port)

       {         

           IPEndPoint ipe = new IPEndPoint(IPAddress.Any,port);

           workSock = new Socket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);

           workSock.Bind(ipe);

           workSock.Listen(100);

           acpT = new Thread(new ThreadStart(acceptThread));

           acpT.IsBackground = true;

           acpT.Start();

       }

       private void acceptThread()

       {

           while(true)

           {

              Socket asock = workSock.Accept();

              SocketStateObject state = new SocketStateObject(asock);    

              if (OnConnect != null)

                  OnConnect(state,System.EventArgs.Empty);

               ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveThreadEntryPoint),state);

           }

       }

象上面OnConnect事件及ReceiveThreadEntryPoint方法里都会跟进这么一个参数。

管理连接是个很复杂、很讲究技术的活。可用连接的数据接收与发送(甚至有些接收与发送必须是相关的)、连接的可用性监测、失效连接的移除。所有这些都要作应有的记录,并且一些还要往UI界面触发事件,事件触发这一点至关重要,也最为棘手,这其中涉及到很多线程同步、线程阻塞的问题。

对于这种极为复杂的问题,最好的解决方法是参照现有的成熟方案进行设计、实现或者拿来主义。但之前也没涉足到这些方面的开发,一时还真找不到比较理想的东西,加之项目本身对于通讯的要求不甚高,10个连接顶天了(如果真有哪个客户要10个签到终端,那真是绝对的上帝了,至目前为止最多的也就5个终端,一般不会多于3个),所以在这次开发中我就用了一些简单的方法来管理客户端的连接。

由于这一块并没有经过认真的设计,也暂时无力作认真的设计,最后实现的东西自己也不满意,这里就不去讲述如何实现管理连接的了。