We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
在这里,我试图回答你们的一些具体的、事实性的问题,我对此可以做出一些贡献。
是的,您可以在 C++ 中使用任何 C 套接字库。如果由于链接器报告您要使用的库函数的未定义引用而无法开箱即用,则可以通过编辑
.h 来修复它code> 库的文件,在所有函数和全局变量声明前面添加
extern "C"
。要查找库,请转到 http://freshmeat.net/ ,然后搜索
C++ 套接字
或C 套接字
。以下是我找到的C++ 套接字
:正如 Raphael 在他的回答中提到的,您可能会发现 Qt 库的套接字部分很有用。请参阅 QTCpSocket 以供参考,以及 fortune 客户端 示例代码。
另外 Boost.Asio 也出现在我的脑海中,但是它可能有太多的抽象和低级细节暴露给你。
在freshmeat 上搜索
C socket
,您可能会发现一个比任何C++ 库都更适合的C 库。Here I'm attempting to answer some of your specific, factual questions to which I have something to contribute.
Yes, you can use any C socket library in C++. If it doesn't work out-of-the-box because the linker reports an undefined reference for the library functions you want to use, then can fix it by editing the
.h
file(s) of the library, addingextern "C"
in front of all function and global variable declarations.To find libraries, go to http://freshmeat.net/ , and search for
C++ socket
orC socket
. Here is what I've found forC++ socket
:As Raphael has mentioned in his answer, you might find the socket part of the Qt library useful. See QTCpSocket for reference, and the fortune client for example code.
Also Boost.Asio has popped to my mind, but it might have too much abstraction and low-level details exposed for you.
Do your search for
C socket
on freshmeat, you may find a C library which suits better than any C++ library.可能是因为所有套接字实现都是基于原始的C语言berkeley套接字api,它定义了诸如recv、send、listen、accept、select等函数。
我强烈建议您查看Boost ASIO。它是跨平台 C++ API,因此您开发的任何代码都是可移植的。事实上,您会发现许多其他 Boost 库对您有用,并且它们都是跨平台的。
参考基本API。您可以在Windows和Linux中使用原始的socket C函数。但请注意,在 Windows 下存在一些细微的差异。例如,您必须首先调用 WSAstartup 函数。
Beej 的网络编程指南是对基本套接字编程的一个非常好的参考。
http://beej.us/guide/bgnet/
我建议您阅读一下即使您使用的是 C++ api,也请忽略它,因为它可以让您了解正在发生的事情。
编辑:说实话我不再使用 Boost ASIO 了。我发现它慢得可怕。使用 LibEV 或类似的或自己开发。 Boost ASIO 似乎没有在 Linux 上使用 epoll。
Probably because all the socket implementations are based on the original C language berkeley socket api which defines functions like recv, send, listen, accept, select etc.
I can highly recommend you look at the Boost ASIO. It's a cross platform C++ API so any code you develop will be portable. In fact, a number of other Boost libraries you will find useful to you and all of them are cross platform.
With reference to the basic API. You can use the original socket C functions in both windows and Linux. However, be aware that under windows there are some slight differences. e.g. you have to call the WSAstartup function first.
A really good reference to basic socket programming is Beej's guide to network programming.
http://beej.us/guide/bgnet/
I'd recommend having a bit of a read over it even if you're using a C++ api as it gives you an understanding of what's going on.
Edit: To be honest I don't use Boost ASIO anymore. I found it horribly slow. Use LibEV or similar or roll your own. Boost ASIO doesn't appear to use epoll on Linux.
我用 C++ 开发了一个套接字库,但仅适用于 Windows。
它提供了一个面向对象的实现,带有用于接收消息的回调!
这就是我从客户端建立连接的方式:
这是服务器部分:
正如您所看到的,它实现了用于接收消息和处理错误的回调。
这是感兴趣的github: SocketClient
这是我的教程在我的博客上制作:因为你被困住了
I developed a library for sockets in c++, but only for windows.
It provides an object oriented implementation with callbacks for receiving messages!
This is how I make a connection from the client:
And this is the server part:
As you can see it implements callbacks for receiving messages and handling errors.
Here is the github for the interested: SocketClient
And this is a tutorial I made on my blog: Cause You're Stuck
我喜欢使用 Qt 来编写套接字。它提供了面向对象的实现并且是多平台的
I like to use Qt to program sockets. It provides an object-oriented implementation and it's multi-platform