FlazX | Categories | Forum | Links | Blog | Login


Socket Basics



by basileis



Socket Basics

What is a socket?

Socket is a piece of code which we wrote to enable communication between two separate entities (here it means computers) or within the entity itself. For e.g., like we use telephone and dial telephone number of the person whom we wish to talk, in this process we create a end point of communication between two telephones same principle is applied in sockets.

Identifying a socket

Each socket have address of their own (except nameless socket) to identify specifically.

On a broader view there are three types of sockets:

  1. Local sockets or Nameless sockets.
  2. Internet Domain sockets.
  3. Raw sockets.

Local Sockets or Nameless Sockets

These types of sockets do not have address because they connect within the machine itself also known as UNIX domain sockets. The function socketpair() is used to create such types of sockets. Local domain sockets uses AF_LOCAL prefix here AF stands for Address Family.

Internet Domain Sockets

Internet domain sockets are used when we want to establish communication between process running on two different computers. Internet domain sockets
uses PF_INET macro here PF stands for protocol familiy.

Local socket address structure

This type of address format is used by the sockets
which are local to the host. Structure used for this type of sockets is sockaddr_un and is defined in the
header file of Linux.


  1. struct sockaddr_un {          
  2. sa_family_t     sun_family; /* Address Family */    
  3. char       sun_path[108];/* Pathname */
  4. };


The sun_family variable must have the AF_LOCAL or AF_UNIX assigned to it.There is no null byte required at the end of character array.

Internet Domain (IPv4) address structure

The socket address sockaddr_in is used in Internet domain structures, it is defined in
header file of Linux.


  1. struct sockaddr_in {
  2. sa_family_t          sin_family;/*Address Family */      
  3. uint16_t            sin_port;/* Port number */  
  4. struct in_addr    sin_addr;/* Internet Address */
  5. unsigned char      sin_zero[8];/*Pad Bytes */
  6. };
  7.  
  8. struct in_addr {                      
  9. uint32_t        s_addr;/* Internet Address */
  10. };

sin_family should be initialized to PF_INET, sin_port represents the TCP/IP port number for the socket address ( this value should be in network byte order ), sin_addr represents the internet protocol (IP)
address in network byte order, it consists of 32 bit unsigned integer.The remaining structure is now padded to 16 bytes by sin_zero[8] for 8 bytes.

Creating nameless socket

For creating a socket we should do


  1. #include <sys> /* It is required to define some of the C macro constants. */
  2. #include <sys> /* This file defines the socketpair() function */
  3.  
  4. int socketpair( int domain,int type, int protocol , int file[2]); /* Function which creates the socket */

Arguments used in the socketpair() function are:

  • int domain : Domain of the socket, normally we use AF_LOCAL(or AF_UNIX
    domain) macro in it.
  • int type : Type of the socket .We can use
    SOCK_STREAM for connection oriented socket and SOCK_DGRAM for connection less sockets. int protocol: Protocol to be used in creating the socket (e.g. UDP, TCP) for socketpair() we supply 0 that means the operating system will automatically choose the required protocol for the socket.
  • int file[2] : It is a pointer to the array that will receive the file descriptor that reference the created socket.It is a array of two integers that will represent two sockets .Each of the descriptor represent one socket(endpoint) .

This function returns 0 on success and -1 otherwise.

Performing Input Output operations on socket.

Read and write operation on a socket can be done just like an open file. We do


  1. #include <unistd>
  2. ssize_t read( int fd, void *buf, size_t count);
  3. ssize_t write(int fd, const void *buf, size_t count);
  4. int close(int fd);

read returns the input that is available from the file descriptor fd,into the supplied buffer buf of a maximum size of count bytes.The return value represents the number of bytes read. A return count of zero represents the end-of-file. Write writes the data to the file descriptor fd, into the supplied buffer buf for a total amount of count bytes.The return value represents the actual number of bytes written.

If the return value of any of the above function is -1 that means some error has occurred and the reason for error is defined in external variable errno to use this variable we need to include header file.

Closing socket

Sockets are closed in the same manner like we close the file. In sockets the other end will receive an end-of-file indication when the other endpoint (socket) has been closed. The problem occurs when the local socket is closed from one end and acknowledgement is expected from remote end but since the local socket is closed so it cannot receive the acknowledgement to solve this problem we use the half closing of socket using the function


  1. #include <sys>
  2. Int shutdown (int sock_desc , int method);

sock_desc specifies the socket descriptor which is to be shutdown. Int method describes how the socket is to be shutdown, possible values for this argument are:

  • 0 SHUT_RD -> No futher read allowed on the particular socket.
  • 1 SHUT_WR -> No further write allowed on the socket.
  • 2 SHUT_RDWR->No further read and write allowed on the socket.

Possible errors returned by shutdown () function

  • EBADF -> Given socket is not a valid file descriptor.
  • ENOTSOCK -> Given file descriptor is not a valid socket.
  • ENOTCONN -> Given socket is not connected.

Little endian (Host byte ordering) and Big endian (Network byte order)

There are two types of conversions used, these are:

  1. Host ordering to network ordering.
  2. Network ordering to host ordering.

By Host order we mean the byte ordering that the CPU uses
for Intel CPUs it means little-endian byte ordering Network order is big-endian byte ordering. In Network byte ordering most significant bits (MSB) comes first and in Host byte ordering the Least significant bits (LSB) comes first.



Back to Network Programming





Top 100 Search Keywords
Last 100 Search Keywords

Nokia Themes
Free Download
Daily Internet Guide
EgyDown
Share4All
FreeBookCity.Com
Providings.com
DownArchive
Allulook4.com
eu-warez.net




eXTReMe Tracker