socket()
create an endpoint for communication and return a descriptor
#include 'sys/socket.h'
sockfd = socket ( int socket_family, int socket_type, int protocol );

Socket Family Name Purpose
AF_UNIX, AF_LOCAL Local communication
AF_INET IPv4 Internet Protocols
AF_INET6 IPv6 Internet Protocls
AF_IPX IPX Internet Protocols
AF_NETLINK Kernel User interface device
AF_X25 ITU-T X.25 / ISO-8208 protocol
AF_AX25 Amateur radio AX.25 protocol
AF_ATMPVC Access to raw ATM PVCs
AF_APPLETALK Appletalk
AF_PACKET Low level packet interface

Socket Type Purpose
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported
SOCK_DGRAM Supports datagrams (connectionless, unreliable message of a fixed maximum length)
SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.
SOCK_RAW Provides raw network protocol access
SOCK_RDM Provides a reliable datagram layer that does not guarantee ordering
SOCK_PACKET Obsolute and should not be used in new programs
SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open file description.
SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.

Protocol :
  • Protocol value for TCP/IP
  • The default TCP\IP value is 0
Example :
  • int skfd = socket ( PF_INET, SOCK_STREAM, 0 );
  • It will create TCP Socket
  • It returns socket descriptor on success and return -1 if any error in connection