如何在C套接字编程中的聊天客户端程序中使用select()?

发布于 2024-09-16 11:56:42 字数 87 浏览 4 评论 0原文

我想让客户端程序接收来自键盘的输入和来自服务器的数据。我不希望当用户输入某些内容(scanf)时它会阻止从服务器接收数据。

如何用C语言编写代码?

I want to make the client program to receive input from keyboard and data from server. I don't want if while user type something (scanf) and it blocks to receive data from server.

How to write the code in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-09-23 11:56:42

欢迎 :)

我建议您尽快获取一份Unix 环境中的高级编程,第二版 。它有关于使用 select() 的优秀示例(以及其他所有内容)。

上面页面上的源代码包包含一个很好的示例,calld/loop.c,它或多或少准确地显示了您想要的内容 - 接受连接的服务器循环,将连接添加到选择掩码的文件描述符,并依次处理文件描述符。

如果您想要更友好的界面,请研究 libevent。 Libevent 可以在各种平台上为您提供更高的性能漂亮的界面。非常适合生产代码,但对于学习内核如何工作可能不太好。

Welcome :)

I suggest grabbing a copy of Advanced Programming in the Unix Environment, 2nd Edition as soon as you can. It has excellent examples on using select() (and everything else, too).

The source code package on the page above includes an excellent example, calld/loop.c that shows more or less exactly what you want -- a server loop that accepts connections, adds the connections to the select mask of file descriptors, and handles file descriptors in turn.

If you'd like a friendlier interface, investigate libevent. Libevent can give you higher performance on a wide range of platforms and a nice interface. Great for production code, maybe less great for learning how the kernel works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文