C++温索克2个问题
我已经阅读了 MSDN 上的 Winsock2 文档,但我仍然需要澄清一些事情,如果有人可以提供帮助的话。
我计划做一些类似于使用 WSAAsyncSelect() 时获得的设置,但使用单独的线程。我可以使用 WSAEventSelect() 将多个套接字链接到单个事件对象吗?
如果我改用完成端口,我可以通过完成端口获得哪些事件? MSDN 有一个关于文件操作的列表,但我无法将它们与通过完成端口发送的事件(FD_READ、FD_WRITE 等)联系起来。有没有办法知道哪个事件正在完成?或者我每次发送或接收某些内容时都必须注意这一点并将其存储在结构中吗?
感谢任何人可以在这个 =D
编辑上给我的任何*帮助:更好的是,我最好切换到 C# 来执行此操作吗?它似乎很受欢迎并且更适合这个特定任务。
I have read through the documentation for Winsock2 on MSDN, but I still need clarification on a few things, if anyone can help.
I planned to make something like the the setup you get when you use WSAAsyncSelect(), but using a separate thread. Can I use WSAEventSelect() to link more than one socket to a single event object?
If I used a completion port instead, which events do I get through the completion port? The MSDN has a list in terms of file operations, but I had trouble relating them to which events (FD_READ, FD_WRITE, ect..) would be sent through the completion port. Is there a way to tell which event is completing? or do I have to take note of this and store it in a struct each time I send or recv something?
thanks for any *help anyone can give me on this =D
edit: better yet, would I better off just switching to C# to do this? It seems to be very popular and better suited to this specific task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,您不能将多个套接字链接到单个 WSAEVENT。您必须为要接收通知的每个单独套接字调用 WSACreateEvent() 和 WSAEventSelect()。不过,您可以使用 WSAWaitForMultipleEvents() 让单个线程等待来自多个套接字的事件。
至于使用完成端口,我建议您阅读MSDN有关此事的文章,例如:
Windows Sockets 2.0:使用完成端口编写可扩展的 Winsock 应用程序。
No, you cannot link multiple sockets to a single WSAEVENT. You have to call WSACreateEvent() and WSAEventSelect() for each individual socket that you want to receive notifications for. You can use WSAWaitForMultipleEvents() to have a single thread wait for events from multiple sockets, though.
As for using completion ports, I suggest you read MSDN's articles on the matter, such as:
Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports.
[免责声明:我在套接字编程方面的经验极其有限。]
那没有意义。一个事件只能发出一次信号。因此,如果您有多个套接字连接到同一事件,那么当它收到信号时,您永远不知道哪个套接字发出了该事件信号!
您可以做的是为每个套接字创建一个事件(每个套接字可能有多个事件:对于您感兴趣的每个 FD_* 事件)并使用 WSAWaitForMultipleEvents ( http://msdn.microsoft.com/en-us/library/ms742219%28v=VS.85%29.aspx )
[Disclaimer: I've extremely limited experience with socket programming.]
That wouldn't make sense. An event can just be signaled once. So if you'd have multiple sockets connected to the same event, then when it gets signaled you'd never knew which socket signaled the event!
What you could do is create an event for every socket (possibly multiple events per socket: for every FD_* event you're interested in) and use WSAWaitForMultipleEvents ( http://msdn.microsoft.com/en-us/library/ms742219%28v=VS.85%29.aspx)