Skip to content
Search

C:socket

lastmod 2018-08-17created 2017-01-134 edits

create an endpoint for communication.

SYNOPSIS

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
int socket(int domain, int type, int protocol);

AF vs PF

Explains the difference between AF and PF.

  • PF_INET: One of the protocol families.
  • AF_INET: One of the address families.

Typical protocol families (Protocol Family) are listed below. These are used when defining the address family in an address structure.

PF_INET

IPv4 Internet protocol.

PF_INET6

IPv6 Internet protocol.

PF_LOCAL

UNIX protocol for LOCAL communication.

PF_PACKET

Interface for low-level sockets.

PF_IPX

IPX Novell protocol.

Typical address families (Address Family) are listed below.

AF_INET

IPv4 Internet protocol.

AF_INET6

IPv6 Internet protocol.

AF_LOCAL

UNIX protocol for LOCAL communication.

When creating a socket, you must set the protocol with the environment in which the socket will be used in mind. In other words, the protocol family determines which protocol the socket will use for communication when it is created. Note that sockets are not used only for network communication. On Unix-like systems they are also used for communication between processes within the system. The commonly used PF_INET is one of the protocol families. PF_INET, which denotes a protocol family, and AF_INET, which denotes an address family, have the same constant value. Even so, it is not good practice to use PF_INET where address information is set, or to put AF_INET where protocol family information is set.

In conclusion,

  • it is better to use constants starting with PF where the protocol family is set,
  • and constants starting with AF where the address family is set.

In actual code, you may pass AF_INET as the protocol family to socket(), but passing PF_INET is preferable. Likewise, when setting the address family in a sockaddr_in structure, you may use PF_INET, but AF_INET is preferable.

See also

Favorite site