如何在C套接字编程中的聊天客户端程序中使用select()?
我想让客户端程序接收来自键盘的输入和来自服务器的数据。我不希望当用户输入某些内容(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎 :)
我建议您尽快获取一份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.