C#中的多个异步套接字 [英] Multiple Asyncronous Socket in C#

查看:140
本文介绍了C#中的多个异步套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写一个程序,该程序能够同时处理与多个设备的多个连接.

我有多个设备(有时会变化),如何编写一个可以处理多个连接的功能,使用户能够添加或删除连接,读取数据并向设备发送命令?
设备都具有IP地址和端口号.

我用Google搜索并阅读了很多文章,但仍然找不到我想要的东西.

谢谢

Hi,

I''m trying to write a program that are able to handle multiple connections to multiple equipment at the same time.

I have multiple equipment (varies from time to time), how do I write a function that can handle multiple connections where user is able to add or delete connection, read data and send command to the equipments?
The equipment all have IP Addresses and Port Numbers.

I googled and read quite a number of articles but still I cannot get what I''m looking for.

Thanks

推荐答案

查看此处:
异步套接字服务器和客户端 [异步套接字通信 [
See here:
An Asynchronous Socket Server and Client[^]
or here:
Asynchronous socket communication[^]

or just google for "asynchronous socket c#"


所以,我警告您不要使用异步套接字,这是使项目失败的最简单方法.仅线程-根据协议和设备功能,每个设备可能需要1-2个线程.该线程将解决多个连接的问题并使设备独立.请参阅我对JF2015的Answer的评论.

另外,您将需要开发一个插件系统来插入每个设备,否则您将迷失在进一步的开发中,尤其是在支持方面.

辛苦了.



我在提示/技巧"部分中发布了一篇小文章,它应该有助于理解线程解决方案.基本上,每个设备处理都应放入与代码用法示例中的线程类似的线程中.设备操作请求应使用队列发布.此处:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ].
So, I''m warning you against asynchronous sockets -- easiest way to fail the project. Only threading -- it may require 1-2 threads per device, depending on protocol and device capabilities. The thread will resolve a problem of multiple connection and make devices independent. See my comment to Answer by JF2015.

Also, you will need to develop a plug-in system to plug each device, otherwise you will get lost in further development and especially in support.

Big work, anyway.



I posted a small article in Tips/Tricks section, it should help to understand a thread solution. Basically, every device processing should be put in a thread similar to one of those in code usage samples. The requests for device operation should be posted using the queue. Here:

Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].


这篇关于C#中的多个异步套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆