Network Programming in GNU/LINUX- socket() system call


The elementary system call in an any SOCKET/NETWORK program(server/client) in GNU/LINUX written using C is socket().The call to socket() creates a socket ,that is a communication end point.The prototype for the socket call is as follows

#include <sys/socket.h>

int socket(int domain, int type, int protocol);

The domain(first argument) can be one of the following values .It specifies the address family(AF)

     Name                   Purpose                 
       AF_UNIX, AF_LOCAL      Local communication              
       AF_INET                IPv4 Internet protocols        
       AF_INET6               IPv6 Internet protocols
       AF_IPX                 IPX - Novell 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 

  The socket has the indicated type(second argument), which specifies the communication semantics. Currently defined types are:





Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.



Supports datagrams (connectionless, unreliable messages of a fixed maximum length).



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 read system call.



Provides raw network protocol access.



Provides a reliable datagram layer that does not guarantee ordering.



Obsolete and should not be used in new programs

CAUTION:All combinations of domain(first argument) and type(second argument) are not valid.

The third argument protocol  specifies  a  particular  protocol  to  be used with the    socket.  Normally only a single protocol exists to support a particular  socket  type within a given protocol family, in which case protocol can  be specified as 0.

       On  success,  a  file  descriptor  for  the new socket is returned.  On
       error, -1 is returned, and errno is set appropriately.

       EACCES Permission to create a socket of the specified type and/or  protocol is denied.

        The  implementation  does not support the specified address family.


       Unknown protocol, or protocol family not available.


       Invalid flags in type.


       Process file table overflow.


       The system limit on the total number  of  open  files  has  been  reached.

              Insufficient  memory is available.  The socket cannot be created
              until sufficient resources are freed.

              The protocol type or the specified  protocol  is  not  supported  within this domain.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s