在C或C ++中的同一套接字上同时读取和写入 [英] simultaneously read and write on the same socket in C or C++

查看:155
本文介绍了在C或C ++中的同一套接字上同时读取和写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个简单的服务器,该服务器接受单个连接,然后使用该套接字从读取和写入线程同时读取和写入消息. 在Linux上的c/c ++中,从同一套接字描述符同时读取和写入的安全简便方法是什么? 我不必担心从同一套接字读取和写入多个线程,因为将只有一个专用读取线程和一个专用写入线程向该套接字写入.

I am implementing a simple server, that accepts a single connection and then uses that socket to simultaneously read and write messages from the read and write threads. What is the safe and easy way to simultaneously read and write from the same socket descriptor in c/c++ on linux? I dont need to worry about multiple threads read and writing from the same socket as there will be a single dedicated read and single dedicated write thread writing to the socket.

在上述情况下,需要任何形式的锁定吗?

In the above scenario, is any kind of locking required?

以上情况是否需要非阻塞套接字?

Does the above scenario require non blocking socket?

是否有任何开源库在上述情况下会有所帮助?

Is there any opensource library, that would help in the above scenario?

推荐答案

在上述情况下,需要任何形式的锁定吗?

In the above scenario, is any kind of locking required?

没有.

以上情况是否需要非阻塞套接字?

Does the above scenario require non blocking socket?

您可能担心的一点-建立的连接上的读取和写入线程-如果您对那些线程坐在那里等待完成感到满意,则不必保持非阻塞状态.这通常是您使用线程而不是选择,轮询或异步操作的原因之一……也使代码更简单.

The bit you're probably worried about - the read and write threads on an established connection - do not need to be non-blocking if you're happy for those threads to sit there waiting to complete. That's normally one of the reasons you'd use threads rather than select or poll or async operations... keeps the code simpler too.

如果接受新客户端的线程乐于阻止对accept()的调用,那么您在那也很好.

If the thread accepting new clients is happy to block in the call to accept(), then you're all good there too.

仍然,TCP服务器存在一个细微的问题,您可能想记住……如果您的程序增长到可以处理多个客户端并且需要进行一些定期的内务处理.使用带有超时的select语句来检查侦听套接字上的可读性是很自然而且很诱人的-指示客户端尝试连接-然后accept进行连接.那里存在竞争状态:客户端连接尝试可能已在select()accept()之间掉线,在这种情况下,如果侦听套接字没有非阻塞,则accept()将阻塞,这可能会阻止及时返回到循环并暂停定期的超时处理,直到另一个客户端连接为止.

Still, there's one subtle issue with TCP servers you might want to keep in the back of your mind... if your program grows to handle multiple clients and have some periodic housekeeping to do. It's natural and tempting to use a select statement with a timeout to check for readability on the listening socket - which indicates a client connection attempt - then accept the connection. There's a race condition there: the client connection attempt may have dropped between select() and accept(), in which case accept() will block if the listening socket's not non-blocking, and that can prevent a timely return to the select() loop and halt the periodic on-timeout processing until another client connects.

是否有任何开源库在上述情况下会有所帮助?

Is there any opensource library, that would help in the above scenario?

有成百上千个用于编写基本服务器的库,但是最终您所要求的是在操作系统提供的BSD套接字或其Windows混搭之上轻松实现的.

There are hundreds of libraries for writing basic servers, but ultimately what you've asked for is easily achieved atop OS-provided BSD sockets or their Windows bastardisation.

这篇关于在C或C ++中的同一套接字上同时读取和写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆