Socket API or library for C++? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more开发者_开发技巧. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this questionI 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.
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++.
I am fairly new at socket programming and C++, so please no answers that will throw me for a loop.
Thanks
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, adding extern "C"
in front of all function and global variable declarations.
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
:
- Socket++.
- ENUt.
- LibSylph.
- CPPSocket might also work for you, although the last update was in 2003.
- SocketW is 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 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.
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:
#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
And this is a tutorial I made on my blog: Cause You're Stuck
I have only been able to find reference/tutorials on C based socket implementations. Is there a reason for this?
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.
I like to use Qt to program sockets. It provides an object-oriented implementation and it's multi-platform
精彩评论