Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Creating a socket

&pagelevel(3)&pagelevel

A socket is created with the socket() function:

int s;
...
s = socket(domain, type, protocol); 

The socket() call creates a socket of type type in the domain domain and returns a descriptor (integer value). The new socket can be referenced in all further socket function calls via this descriptor.

The domains are defined as fixed constants in the <sys/socket.h> header file. The following domains are supported:

  • Internet communications domain AF_INET

  • Internet communications domain AF_INET6

  • local host communications domain AF_UNIX

You must therefore specify AF_INET, AF_INET6 or AF_UNIX as the domain.

The socket types type are also defined in the <sys/socket.h> file:

  • Specify SOCK_STREAM for type, if you want to set up connection-oriented communications via a stream socket.

  • Specify SOCK_DGRAM for type, if you want to set up connectionless communications via a datagram socket.

If you set protocol to 0, you specify the standard protocol:

  • TCP for socket type SOCK_STREAM

  • UDP for socket type SOCK_DGRAM