All about programming in GNU/LINUX

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

Image

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:

Tag

Description

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

SOCK_RAW

 

Provides raw network protocol access.

SOCK_RDM

 

Provides a reliable datagram layer that does not guarantee ordering.

SOCK_PACKET

 

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.

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

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

       EAFNOSUPPORT
        The  implementation  does not support the specified address family.

       EINVAL

       Unknown protocol, or protocol family not available.

       EINVAL

       Invalid flags in type.

       EMFILE

       Process file table overflow.

       ENFILE

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

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

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s