用于 C++ 的套接字 API 或库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4199185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket API or library for C++?
提问by TheJediCowboy
I recently have been getting more into C++. I have done some (very minimal) socket programming with C, but have been interested in doing some work with C++. I have only been able to find reference/tutorials on C based socket implementations. Is there a reason for this? I know, or believe I know that you can use the C socket libraries for C++, but am not sure.
我最近对 C++ 有了更多的了解。我已经用 C 完成了一些(非常少的)套接字编程,但对用 C++ 做一些工作很感兴趣。我只能找到有关基于 C 的套接字实现的参考/教程。是否有一个原因?我知道,或者相信我知道您可以将 C 套接字库用于 C++,但我不确定。
Is there a C++ socket library that is used more often then others? This isn't a subjective question, I am actually looking to find out what the Socket API's/libraries are for C++.
是否有比其他人更经常使用的 C++ 套接字库?这不是一个主观问题,我实际上是想找出 C++ 的 Socket API/库是什么。
I am fairly new at socket programming and C++, so please no answers that will throw me for a loop.
我对套接字编程和 C++ 还很陌生,所以请不要让我陷入循环的答案。
Thanks
谢谢
采纳答案by pts
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 referencefor the library functions you want to use, then can fix it by editing the .h
file(s) of the library, adding extern "C"
in front of all function and global variable declarations.
是的,您可以在 C++ 中使用任何 C 套接字库。如果它不能开箱即用,因为链接器报告了您要使用的库函数的未定义引用,则可以通过编辑.h
库的文件来修复它,extern "C"
在所有函数前添加和全局变量声明。
To find libraries, go to http://freshmeat.net/, and search for C++ socket
or C socket
. Here is what I've found for C++ socket
:
要查找库,请访问http://freshmeat.net/,然后搜索C++ socket
或C socket
。这是我发现的C++ socket
:
- Socket++.
- ENUt.
- LibSylph.
- CPPSocketmight also work for you, although the last update was in 2003.
- SocketWis similarly old: last update in 2003.
As Raphael has mentioned in his answer, you might find the socket part of the Qt library useful. See QTCpSocketfor reference, and the fortune clientfor example code.
正如 Raphael 在他的回答中提到的,您可能会发现 Qt 库的套接字部分很有用。参考QTCpSocket,财富客户端示例代码。
Also Boost.Asiohas popped to my mind, but it might have too much abstraction and low-level details exposed for you.
此外Boost.Asio的已经弹出来我的脑海里,但它可能会暴露你太多的抽象和低级别的细节。
Do your search for C socket
on freshmeat, you may find a C library which suits better than any C++ library.
C socket
在新鲜肉上搜索,您可能会找到一个比任何 C++ 库都更适合的 C 库。
回答by Matt
I have only been able to find reference/tutorials on C based socket implementations. Is there a reason for this?
我只能找到有关基于 C 的套接字实现的参考/教程。是否有一个原因?
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.
可能是因为所有的 socket 实现都是基于原始的 C 语言 berkeley socket api,它定义了 recv、send、listen、accept、select 等函数。
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.
我强烈建议您查看Boost ASIO。它是一个跨平台的 C++ API,因此您开发的任何代码都是可移植的。事实上,您会发现许多其他 Boost 库对您很有用,而且它们都是跨平台的。
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.
参考基本API。您可以在 Windows 和 Linux 中使用原始的套接字 C 函数。但是,请注意,在 Windows 下存在一些细微差别。例如,您必须先调用 WSAstartup 函数。
A really good reference to basic socket programming is Beej's guide to network programming.
Beej 的网络编程指南是对基本套接字编程的一个很好的参考。
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.
即使您使用的是 C++ api,我也建议您阅读它,因为它可以让您了解正在发生的事情。
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.
编辑:老实说,我不再使用 Boost ASIO。我发现它非常慢。使用 LibEV 或类似的或自己动手。Boost ASIO 似乎没有在 Linux 上使用 epoll。
回答by Omar Aflak
I developed a library for sockets in c++, but only for windows. It provides an object oriented implementation with callbacks for receiving messages!
我在 c++ 中开发了一个套接字库,但仅适用于 Windows。它提供了一个面向对象的实现,带有用于接收消息的回调!
This is how I make a connection from the client:
这是我从客户端建立连接的方式:
#include <iostream>
#include <winsock2.h>
#include "SocketClient.h"
using namespace std;
void onError(errorStruct *e)
{
cout << e->code << " : " << e->message << endl;
}
int main()
{
SocketClient client("127.0.0.1", 5555);
client.setErrorCallback(onError);
client.connect();
client.send("Hello World!");
client.close();
}
And this is the server part:
这是服务器部分:
#include <iostream>
#include <winsock2.h>
#include "SocketClient.h"
#include "SocketServer.h"
using namespace std;
bool good=true;
void messageReceived(messageStruct *s)
{
cout << "client: " << s->message << endl;
}
void errorOccurred(errorStruct *e)
{
cout << e->code << " : " << e->message << endl;
good=false;
}
int main()
{
SocketServer server(5555);
SocketClient client(server.accept());
client.setErrorCallback(errorOccurred);
client.setMessageCallback(messageReceived);
while(good){};
client.close();
server.close();
}
As you can see it implements callbacks for receiving messages and handling errors.
如您所见,它实现了用于接收消息和处理错误的回调。
Here is the github for the interested: SocketClient
这是感兴趣的github:SocketClient
And this is a tutorial I made on my blog: Cause You're Stuck
这是我在博客上制作的教程:Cause You're Stuck
回答by Raphael
I like to use Qt to program sockets. It provides an object-oriented implementation and it's multi-platform
我喜欢用 Qt 来编程套接字。它提供了面向对象的实现并且是多平台的