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 identified 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

  • ISO communications domain AF_ISO

You must therefore specify AF_INET, AF_INET6 or AF_ISO 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.

  • Specify SOCK_RAW for type, if you want to send an ICMP message via a raw socket.

The protocol parameter is not supported and should have the value 0.

Creating a socket in the AF_INET domain

The following call creates a stream socket in the AF_INET Internet domain:

s = socket(AF_INET, SOCK_STREAM, 0);

In this case, the underlying communications support is provided by the TCP protocol.

The following call creates a datagram socket in the AF_INET Internet domain:

s = socket(AF_INET, SOCK_DGRAM, 0);

The UDP protocol used in this case transfers the datagrams without any further communications support to the underlying network services.

Creating a socket in the AF_INET6 domain

The following call creates a stream socket in the IPv6 Internet domain AF_INET6:

s = socket(AF_INET6, SOCK_STREAM, 0);

In this case, the underlying communications support is provided by the TCP protocol.

The following call creates a datagram socket in the IPv6 Internet domain AF_INET6:

s = socket(AF_INET6, SOCK_DGRAM, 0);

The UDP protocol used in this case transfers the datagrams without any further communications support to the underlying network services.

Creating a socket in the AF_ISO domain

The following call creates a socket in the ISO domain for using the ISO transport service:

s = socket(AF_ISO, SOCK_STREAM, 0);