Skip to content

Unix sockets

Clément Hamada edited this page May 7, 2021 · 6 revisions

Unix sockets provide a way to send data between processes and network interfaces.

A socket connection has a protocol and an address. For internet communication, addresses are composed of an IP address and a port. Computers can open multiple connections to and from the same addresses using different ports.

There are several functions used to manipulate sockets:

  • socket: Creates an endpoint for communication and returns its associated file-descriptor.
  • setsockopt: Set socket stream options.
  • getsockopt: Get socket stream options.
  • getsockname: Get the socket's associated name.
  • bind: Bind a socket to a name.
  • connect: Initiate a connection to another socket's address.
  • listen: Listen for incoming socket connections on the bound address.
  • accept: Accept an incoming socket connection and get an associated file-descriptor.
  • send: Write data to a socket stream.
  • recv: Read incoming data from a socket stream.

There are several functions used to manipulate network format numbers:

  • htons, htonl: Convert values from host to network byte order.
  • ntohs, ntohl: Convert values from network to host byte order.
  • inet_aton, inet_addr, inet_network: Interpret strings representing numbers in the internet standard '.' notation.
  • inet_pton: Convert a presentation format address to network format.
  • inet_aton: Interpret string as an internet address.
  • inet_ntop: Convert an address from network format to presentation format.
  • getprotobyname: Get a network protocol by name.
  • gethostbyname: Get a network host's address from its hostname.
  • getaddrinfo: A more flexible replacement to gethostbyname and getservbyname.
  • freeaddrinfo: Releases memory allocated by getaddrinfo.
Clone this wiki locally