Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket API or library for C++? [closed]

Tags:

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.

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

like image 712
TheJediCowboy Avatar asked Nov 16 '10 21:11

TheJediCowboy


People also ask

What library is socket for C?

In the GNU C Library, the header file sys/socket. h exists regardless of the operating system, and the socket functions always exist, but if the system does not really support sockets these functions always fail.

When should I close a socket connection?

If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed. The descriptor of the socket to be closed. Note: All sockets should be closed before the end of your process.

Can socket programming be done in C?

Socket Programming in C/C++ What is socket programming? Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection.


2 Answers

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.

like image 198
pts Avatar answered Sep 16 '22 17:09

pts


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.

like image 33
hookenz Avatar answered Sep 18 '22 17:09

hookenz