Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket explanation in C and C++

Tags:

c++

c

sockets

Well I have been wondering if there is a standard socket header file for C++

I did search the whole internet (using google search engine ), but couldn't find any standard socket header file for C++ , beside finding some libraries, like Boost, chilkat etc...

I have only succeeded in finding a standard socket header file for C programming language. If I used the C standard socket header file, but inside my C++ code, does it mean my program is Pure C++ or C and C++?

Because I didn't find any standard C++ socket header file. Like there is <string> for C++ and there is <string.h> for C, but there is no socket standard header file for C++.

I hope someone C/C++ wise would explain all that for me, step by step.

like image 738
user1341993 Avatar asked Jan 17 '23 21:01

user1341993


2 Answers

There is no standard socket library in C++. You can either use whatever sockets API your operating system provides (typically a C API, on Unix operating systems it would be the BSD sockets API), or you can use a C++ library like Boost.ASIO, which is cross-platform.

like image 170
HighCommander4 Avatar answered Jan 24 '23 23:01

HighCommander4


BSD sockets, invented by Bill Joy back in the 70's, is arguably the "standard sockets API".

Typically, you'd include the following headers:

#include <sys/socket.h> // Core BSD socket functions and data structures.
#include <netinet/in.h> // AF_INET and AF_INET6 address families and their
                        // corresponding protocol families PF_INET and PF_INET6.
#include <arpa/inet.h>  // Functions for manipulating numeric IP addresses.
#include <netdb.h>      // Name resolution

Beej's Guide is an excellent tutorial on sockets (BSD sockets) programming:

http://beej.us/guide/bgnet/

like image 30
paulsm4 Avatar answered Jan 24 '23 21:01

paulsm4