多个TCP套接字连接 [英] Multiple TCP Socket connections

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

问题描述

您好,

 我是C#套接字编程的新手,我正在创建一个应用程序,我的应用程序必须连接到多个服务器(哪些是硬件控制器)和与他们沟通。所有服务器IP地址和端口号都存储在
数据库中,一旦启动应用程序,我的应用程序将遍历数据库表并开始向每个服务器发送连接请求,一旦建立连接,应用程序必须开始发送并从多个服务器接收数据。
请给我一些关于我该怎么做的信息,我的意思是我如何同时处理多个连接,如何同时发送和接收来自多个服务器。同步和异步通信哪一个适合我的应用程序
,如果我从某个服务器收到一些数据,我如何确定哪个服务器发送该数据?

 i am new to C# socket programming and i am creating an application where my application has to connect to multiple server's(Which are hardware controllers) and communicate with  them. All the server I.P address and port number's are stored in database,once the application is launched my application will iterate through the database table and start sending connection request to each server and once the connection is established,application has to start sending and receiving data from multiple servers. please provide me some information on how can i do this,i mean how can i handle multiple connections at the same time,how can i send to and receive from multiple servers at the same time.out of synchronous and asynchronous communication which one is good for my application,if at all i received some data from some server how can i identified that which server sends that data?

我完全迷失了。请提供一些示例代码和信息,以便我可以拥有良好的设计并开始实施。

i am completely lost. please give some sample code and information so that i can i have good design and start implementation.

推荐答案

我过去做过的事情我将Socket对象存储在List或Dictionary中。根据您使用的代码,字典可能更容易,因为您可以将密钥设置为服务器的IP。



因此创建对象。

What I have done in the past is I store the Socket object inside a List or a Dictionary. Depending on the code you are using, a Dictionary may be easier as you can set the Key to be the IP of the server.

So create the object.

 


//Dictionary method.

public Dictionary<IPAddress, Socket> connections = new Dictionary<IPAddress, Socket>();

//List method. public List<Socket> connections = new List<Socket>();


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

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