WinSock客户端C
我有一个客户端使用 select() 来检查是否有任何内容要接收,否则会超时并且用户可以发送()。效果很好。但是,程序会锁定等待用户输入,因此在用户发送内容之前它无法再次使用 receive() 。 我在使用线程方面也没有太多运气,因为我似乎找不到好的资源,这向我展示了如何使用它们。
我尝试为发送和接收函数创建两个线程(使用 CreateThread),这基本上是两个使用 while 循环来保持发送和接收的函数。然后两个 CreateThreads() 被包裹在一个 while 循环中,否则它看起来就会退出。
我以前基本上没有线程方面的经验,所以我对我所做的事情的描述可能听起来很荒谬。但我将不胜感激任何帮助,正确使用它们进行这种用途,或者替代方法也很棒。
I have a client using select() to check if there is anything to be received, else it times out and the user is able to send(). Which works well enough. However, the program locks up waiting for user input, so it cant recv() again until the user has sent something.
I am not having much luck with using threads either, as I cannot seem to find a good resource, which shows me how to use them.
I have tried a creating two threads (using CreateThread) for send and recv functions which are basically two functions using while loops to keep sending and receiving. And then the two CreateThreads() are wrapped up in a while loop, because otherwise it just seemed to drop out.
I have basically no previous experience with threads, so my description of what i've been doing will probably sound ridiculous. But I would appreciate any help, in using them properly for this kind of use, or an alternative method would also be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找不到有关套接字编程的好资源?呸。阅读圣经:
Can't find a good resource on socket programming? Bah. Read the bible:
这有很强的作业感觉...
我不太喜欢 Windows 编程,但是当我之前做一些事情时,曾经有一个
kbhit
(可能)函数允许您检查用户是否发送了任何输入。您可以寻找类似的东西,并在再次开始选择之前尝试非阻塞用户输入检查。然后你就可以在没有多线程的情况下让它工作
(除非,呃,你必须使用它)。
this has a strong feel of assignment work...
I am not too much into Windows programming but when I did do some earlier, there used to be a
kbhit
(probably) function that allowed you to check if the user had sent any inputs. You could look for something similar and try a non-blocking user input check before starting your select again.You could then get this working without multi-threading
(unless, er, you have to use that).