Introduction to Socket Programming by ebd21054


									Introduction to Socket Programming

               Overview of TCP

• Features of TCP that are relevant to the network
   – Addressing
   – Segment Structure
   – Connection establishment
   – Connection release
   – State transition

     Why is the emphasis on TCP ?

• The following use TCP
   – BGP     (Routing)
   – HTTP (Web traffic)
   – SMTP (Email)
   – FTP      (File transfer)
   – NNTP     (Newsgroups)
   – Telnet (Remote login)

• TCP uses the concept of source and destination port
  numbers which act as the TSAPs and uniquely
  identify a particular communicating process on a host

• Usually server applications use what are known as
  “Well known port numbers” which are defined by a
  standards body.
   – IANA assigns these port numbers. Most systems allow only
     privileged users to communicate on these ports.
   – Most operating systems allow only privileged applications to
     communicated on these ports.
   – They range from 0 through 1023


• Registered ports are those that are
  administratively assigned by a standards body to
  some specific applications.
   – They are assigned by IANA
   – They range from 1024 through 49151
• Private ports are those that do not have any specific
  application associated with them.
   – Used by applications that are ephemeral in nature
   – They range from 49152 through 65535

TCP Segment Header

TCP Connection Management
   Three-way Handshake

          Establishing a Connection
• TCP uses a three way handshake mechanism for connection

• Each side chooses an initial sequence number that needs to be
  acknowledged by it’s peer

• Sequence number of the packets that are part of a connection
  are obtained using the formula
  Seq(Pn) = Seq(Pn-1) + Length(Pn-1)
  Ack(LPn) = Seq(RPn-1) + Length(RPn-1) + 1

• The side that sends the SYN first is said to do an active open
  and it’s peer is said to do a passive open

• It is possible for both the sides to do an active open

          Releasing A Connection

• TCP connections are full duplex (they can be thought
  of as two simplex connections).

• Each simplex connection is released independently.

• Two-army problem avoided using timers.

• The side sending the FIN first is said to do an active
  close however it is possible for both sides to do an
  active close

• A closing of a TCP connection means no more data in
  the direction of the active close                  9
TCP State transition diagram







           The SOCKET Interface
• What is the socket interface all about ?
   It is a protocol independent interface to multiple
   transport layer primitives.

• Where is it used ?
   In order to write applications which need to communicate
   with other applications.

• History of socket interface
   The first implementation was released along with BSD 4.2
   Never mind
• What are the advantages of using this interface ?
   Syntax of the API functions is independent of the
   protocol being used. Ex:- TCP/IP and UNIX domain
   protocols can be used by applications using a
   common set of functions.
   Gives way to better portability of applications across
   protocol suites.
   Hides the finer details of the protocols from
   application programs thereby yielding faster and bug
   free application development
   Sockets are referenced through socket descriptors
   which can be passed directly to UNIX system I/O
   File I/O and socket I/O are exactly similar from the
   programmer perspective.                                  13
Organisation of networking code

        Components of the Socket API

•   Address structure                • Descriptor manipulation
•   Initialisation/Shutdown            through fcntl
•   Connection management            • Nonblocking I/O
•   Data exchange(I/O)               • Hostname and address
•   I/O multiplexing                   conversions
•   Optional parameters              • Data manipulation functions
•   Signal handling                  • Client-server design choices
•   Protocol behavior manipulation
    through ioctl

         Socket address structure

• This contains the protocol specific addressing
  information that is passed from the user process to
  the kernel and vice versa
• Each of the protocols supported by a socket
  implementation have their own socket address
  structure sockaddr_suffix
      Where suffix represents the protocol family
      Ex: sockaddr_in – Internet/IPv4 socket address structure
         sockaddr_ipx – IPX socket address structure

• The generic socket address structure
     address family
     protocol specific data

• The internet/IPv4 socked address structure
      in_family         Internet address family
      sin_port          Transport layer Port Number
      in_addr sin_addr IP address;
      sin_zero[8]        Padding ;
Datatypes required by POSIX
• int8_t signed 8-bit integer - <sys/types.h>
• uint8_t unsigned 8-bit integer - <sys/types.h>
• int16_t signed 16-bit integer - <sys/types.h>
• uint16_t unsigned 16-bit integer - <sys/types.h>
• int32_t signed 32-bit integer - <sys/types.h>
• uint32_t unsigned 32-bit integer - <sys/types.h>
• sa_family_t address family of - <sys/socket.h>
• socklen_t length of socket address structure -
• in_addr_t IPv4 address, normally uint32_t <netinet/in.h>
• in_port_t TCP/UDP port, normally uint16_t <netinet/in.h>

      Data manipulation functions

• Byte ordering
   – Network byte order
   – Host byte order
   – htons(l), ntohs(l)

• Memory content initialization
   – memset(buffer,value,buffersize)

• Data copying and comparison
   – memcpy(dest,src,num_of_bytes)
   – memcmp(buffer1,buffer2,num_of_bytes)


• IP address notation conversion
   – Integer notation
   – Dotted decimal notation
       • status inet_aton(ddstring_pointer,address_pointer)
          – Returns 1 on success 0 on error
      • ddstring_pointer inet_ntoa(address_pointer)

      • address_pointer inet_addr(ddstring_pointer)

        Initialisation and Shutdown

• sockfd socket(domain, type, protocol)
   – domain is the protocol/address family AF_INET,AF_IPX..
   – type is the the type of service
   – protocol is the specific protocol that is supported by the
     protocol family specified(as param1)
   – Returns a fresh socket descriptor on success, –1 on error

• status close(sockfd)
   – Flushes(supposed to) the pending I/O to disk
   – Returns –1 on error

           Connection Management
• status bind(sockfd,ptr_to_sockaddr,sockaddr_size)
   – Associates the sockaddr with sockfd
   – The rules for successful binding depend on the protocol
      family of the socket(specified during call to socket)
   – Necessary for receiving connections on STREAM socket

• status listen(sockfd,backlog)
   – Notifies the willingness to accept connections
   – backlog Maximum number of established connections yet
      to be notified to their respective user processes(calls to
   – On unbounded sockets an implicit bind is done with
      IN_ADDRANY and a random port as the address and port
      parameters respectively
            * Above calls return –1 on error
• connfd accept(sockfd,ptr_to_sockaddr,ptr_to_sockaddr_size)
   – Blocks till a connection gets established on sockfd and
     returns a new file descriptor on which I/O can be performed
     with the remote entity
   – Fills the sockaddr and size parameters with the address
     information (and it’s size respectively) of the connecting
   – bind and listen are assumed to have been called on sockfd
     prior to calling accept

• status connect(sockfd, ptr_to_sockaddr, sockaddr_size)
   – Initiates a new connection with the entity addressed by
      sockaddr in case of a STREAM socket
   – Sets the default remote address for I/O in case of DGRAM

       * Above calls return –1 on error                         23
          Illustrative Applications

• Application 1
   – Iterative tcp server - echoserver.c
   – tcp client - echoclient.c
   – I/O using send/receive
   – Illustrate basic socket calls


• Application 2
   – simultaneous tcp server - echoserver-select.c -
     multiple ports
   – Same client
   – Illustrate canonical/noncanonical mode of input


To top