配对插座 [英] Pairing sockets

查看:169
本文介绍了配对插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器程序(在所有三个主要的os系统上工作),在c ++中创建,努力连接两个陌生人通信。

I have a server program (works on all three major os systems), created in c++ which strives to connect two strangers for communication. My current model seems depreciated, and I am simply wondering if there is a better way to go about serving the clients.

- 服务器接收连接请求

- 禁止

-starts线程用于此套接字

-server receives connection request
-checks for ban
-starts thread for this socket

这些步骤]

与合作伙伴(支票经理)确认连接

- 未连接的请求合作伙伴来自经理类

- 接收在我的socket< - 客户端发送保持活动数据包每2s左右

- 断开通知管理器,并关闭线程。

-confirm connection with partner (check manager)
-if unconnected request partner from manager class
-receive on my socket <- client sends keep alive packets every 2s or so
-on disconnect inform manager, and close thread.

[管理员类的工作原理]

[the manager class works like this]

添加socket:push_back on我的sockPairs向量

- 请求合作伙伴:找到未连接的sockPair,如果没有创建新的sockPair,一旦连接,标记sockPair删除,如果已经标记,删除它。

-remove socket:将标记添加到我已断开连接的合作伙伴的经理ID

-check socket:检查管理器ID以断开连接

-add socket: push_back on my vector of sockPairs
-request partner: find unconnected sockPair, if none exist create new sockPair, once connected, mark sockPair for removal, if already marked, remove it.
-remove socket: add flag to manager id of partner that I've disconnected
-check socket: check manager id for disconnection

我想地图会更有效率,但我不积极,因为我从来没有与他们合作,你认为我应该改变什么?我希望能够用这个模型服务200个客户,我真的不确定当前模型是否可以处理它...

I'm thinking a map would be much more efficient, however I'm not positive, as I've never worked with them, what else do you think I should change? I'm hoping to be able to serve 200 clients with this model, I'm really unsure if the current model could handle it...

推荐答案

每个连接的线程只是不会缩放。你需要使用一个机制,如select(),poll(),epoll(),WSAAsyncSelect()或任何其他的东西,让你查询一套套接字的事件。然后按顺序处理每个套接字,并重复。

A thread per connection simply does not scale. You need to use a mechanism such as select(), poll(), epoll(), WSAAsyncSelect() or anything else that will let you query a set of sockets for events. Then you process each socket in order, and repeat.

这篇关于配对插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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