多线程 TCP 服务器的 TCPStream 类
我目前正在致力于将小型控制台应用程序转换为 TCP 服务器/客户端应用程序。客户端将通过任何 Telnet 客户端连接到服务器,服务器将为每个 Telnet 连接复制标准控制台界面。
我开始考虑使用从 Beej 的网络编程指南中学到的技术来实现这一点——接受连接,然后使用 fork() 将其分离到自己的进程中。
但是,我更愿意保留对流 IO 的使用(原始控制台应用程序使用 cin / cout,对网络逻辑使用类似的函数将使转换过程更加简单)。
我发现了 TCPStream 类,隐藏在 sockets.h (http://www.gnutelephony.org/doxy/bayonne2/a00215.html) 中,
看来这个类将允许我使用具有流 IO 的服务器。但是,我找不到使用此类的单个示例,也找不到有关如何使用 fork() 的解释。
有什么想法吗?预先感谢您的任何帮助。
I'm currently working on transitioning a small console application to a TCP server / client application. The client will connect to the server via any Telnet client, and the server will replicate the standard console interface for each Telnet connection.
I started looking into doing this using the techniques I've learned from Beej's guide to network programming -- accepting the connection and then using fork() to separate it into its own process.
However, I would prefer to maintain my use of streaming IO (the original console application uses cin / cout, using similar functions for the networking logic would make the conversion process much simpler).
I've discovered the TCPStream class, hiding within sockets.h (http://www.gnutelephony.org/doxy/bayonne2/a00215.html)
It appears this class will allow me to use the server with streaming IO. However, I can't find a single example of using this class, nor can I find an explanation as to how to use fork() with it.
Any ideas? Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你混淆了树木和森林。一个套接字类只是您需要做的总体工作的一小部分,因此不值得关注它。
如果您的目标只是让您的项目正常运行,那么只需使用现有的框架,而不是尝试从大型项目中提取单个类。 POCO 有一个 TCPServer 类,可以为您完成 90% 的工作。 QT、ACE 等也有类似的类。 POCO 上的文档并不多,但它们确实很好地涵盖了 TCPServer,如果您确实感兴趣的话,您可以通过阅读源代码学到很多东西。
I think you are confusing the trees for the forest. One socket class is such a small part of what you need to do overall that it is not worth focusing on that.
If your objective is just to get your project working then just use an existing framework rather than trying to pull individual classes out of a large project. POCO has a TCPServer class that will do 90% of the work for you. QT, ACE and others have similar classes. There is not a huge amount of documentation on POCO but they do cover TCPServer pretty well and you can learn a lot from reading the source code if that is really where your interest lies.