简体   繁体   English

同时执行recv()和send()winsock

[英]Do both recv() and send() winsock

I wanted to create a simple chat application with no common server on which to connect to and route their data. 我想创建一个简单的聊天应用程序,没有可以连接和路由数据的公共服务器。 However, I don't know how to do it without taking turns, which is odd for a chat program. 但是,我不知道如何不轮流进行,这对于聊天程序来说很奇怪。

I figured I could do multithreading but the information I found so far was just about threading with concern to client requests(to go around the client queue thing). 我想我可以做多线程,但到目前为止我发现的信息只是关于客户端请求的线程(围绕客户端队列的事情)。 I absolutely haven't tried multithreading before. 我以前绝对没有尝试过多线程。 I also don't know if it's the only way. 我也不知道这是否是唯一的方法。 I also though of doing something event driven but I couldn't make ncurses to work on VS(it linked and compiled successfully but there's something wrong in the library itself, it seems). 我也做了事情驱动的事情,但是我无法让ncurses在VS上工作(它成功链接和编译但是库本身出现了问题)。

So basically how do I make a chat program and not take turns. 所以基本上我如何制作聊天程序而不是轮流。 After all, calling recv() just holds until it receives something so during that time I can't call any stdin functions. 毕竟,调用recv()只是保持不变,直到它收到一些东西,所以在那段时间我不能调用任何stdin函数。

Use an event loop. 使用事件循环。

1) Did anything happen? 1)有什么事发生吗?

2) If so, handle it. 2)如果是这样,请处理它。

3) If not, wait for something to happen or for a certain amount of time. 3)如果没有,等待某事发生或持续一段时间。

4) Go to step 1. 4)转到步骤1。

Now, you just have to make everything that can happen (such as data being received on the socket) an event that you can wait for in step 3. For sockets, you do that with WSAEventSelect . 现在,您只需要将可能发生的所有事情(例如在套接字上接收的数据)设置为您可以在步骤3中等待的事件。对于套接字,您可以使用WSAEventSelect执行此操作 You can wait for events with WaitForMultipleEvents . 您可以使用WaitForMultipleEvents等待事件。

Alternatively, you can arrange to have Winsock send your program a Windows message whenever data is received on a socket with WSAAsyncSelect . 或者,您可以安排让Winsock在使用WSAAsyncSelect的套接字上收到数据时向您的程序发送Windows消息。

Before you call recv, check if data is available. 在调用recv之前,请检查数据是否可用。 You can use select or poll to use that. 您可以使用select或poll来使用它。 See select reference and maybe winsock FAQ . 请参阅select参考和winsock常见问题解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM