Saturday, 14 May 2011

Socket Programming in C

What are Sockets?
 Sockets provide a standard connection protocol which enables the communication of data over a network.

 Sockets supports both connection oriented (Transmission Control Protocol) and connectionless (User Datagram Protocol)
 Sockets can be easily configured to act like a client (The one which initiates the connection), or can act as a server (The one which accepts the connection).

The Socket Structures:
 The first structure is struct sockaddr that holds socket information.

         struct sockaddr
         {
                    unsigned short   sa_family;      /* address family */ 
                    char                    sa_data[14];  /* 14 bytes of protocol address */ 
          };

AttributeValuesDescription
sa_familyAF_INET
AF_UNIX
AF_NS
AF_IMPLINK
This represents an address family. In most of the Internet based applications we use AF_INET.
sa_dataProtocol Specific AddressThe content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family we will use port number IP address which is represented by sockaddr_in structure defined below.

But, there's another structure (struct sockaddr_in) that help you to reference to the socket's elements.

         struct sockaddr_in
         {
                    short int                     sin_family; /* Address family */
                    unsigned short int     sin_port; /* Port */
                    struct in_addr            sin_addr; /* Internet Address */
                    unsigned char           sin_zero[8]; /* Same size as struct sockaddr */
         };

AttributeValuesDescription
sa_familyAF_INET
AF_UNIX
AF_NS
AF_IMPLINK
This represents an address family. In most of the Internet based applications we use AF_INET.
sin_portService PortA 16 bit port number in Network Byte Order.
sin_addrIP AddressA 32 bit IP address in Network Byte Order.
sin_zeroNot UsedYou just set this value to NULL as this is not being used.

struct in_addr
{
         unsigned long    s_addr;
};


AttributeValuesDescription
s_addrservice portA 32 bit IP address in Network Byte Order.

There is one more important structure. This structure is used to keep information related to host.

struct hostent
 {
         char *                    h_name; /* Official name of host. */       
         char **                   h_aliases; /* Alias list. */
         int                          h_addrtype; /* Host address type. */       
         int                          h_length; /* Length of address. */
         char **                   h_addr_list; /* List of addresses from name server. */
         #define h_addr      h_addr_list[0] /* Address, for backward compatibility. */
 };


AttributeValuesDescription
h_nameti.com etcThis is official name of the host. For example tutorialspoint.com, google.com etc.
h_aliasesTIThis will hold a list of host name aliases.
h_addrtypeAF_INETThis contains the address family and in case of Internet based application it will always be AF_INET
h_length4This will hold the length of IP address which is 4 for Internet Address.
h_addr_listin_addrFor the Internet addresses the array of pointers h_addr_list[0], h_addr_list[1] and so on are points to structure in_addr.

Following structure is used to keep information related to service and associated ports.

struct servent
{
         char *s_name;
         char **s_aliases;
         int s_port;
         char *s_proto;
};


AttributeValuesDescription
s_namehttpThis is official name of the service. For example SMTP, FTP POP3 etc.
s_aliasesALIASThis will hold list of service aliases. Most of the time this will be set to NULL.
s_port80This will have associated port number. For example for HTTP this will be 80.
s_protoTCP
UDP
This will be set to the protocol used. Internet services are provided using either TCP or UDP.

1 comment:

  1. You can also subscribe here and my warm welcome to my blog
    knowledgeshareindia.blogspot.com

    ReplyDelete