Title: Introduction to Socket Programming
1Introduction to Socket Programming
2Overview of TCP
- Features of TCP that are relevant to the network
programmer - Addressing
- Segment Structure
- Connection establishment
- Connection release
- State transition
3Why is the emphasis on TCP ?
- The following use TCP
- BGP (Routing)
- HTTP (Web traffic)
- SMTP (Email)
- FTP (File transfer)
- NNTP (Newsgroups)
- Telnet (Remote login)
4Addressing
- 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
5Continued
- 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
6TCP Segment Header
7TCP Connection ManagementThree-way Handshake
8Establishing a Connection
- TCP uses a three way handshake mechanism for
connection establishment. - Each side chooses an initial sequence number that
needs to be acknowledged by its 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 its peer is said to do a
passive open - It is possible for both the sides to do an active
open
9Releasing 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
10TCP State transition diagram
Use netstat command to view them
11- TIMED-WAIT
- QUIET TIME
- FIN_WAIT2
- RESET SEGMENTS
- SIMULTANEOUS OPEN/CLOSE
-
- MAXIMUM SEGMENT SIZE
12The 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
13- 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 calls. - File I/O and socket I/O are exactly similar from
the programmer perspective.
14Organisation of networking code
User process
Socket Interface
Socket implementation
Protocol Layer
(TCP/IP,IPX,UNIX)
Media Driver
Media (Ethernet,FDDI,Wireless)
15Components of the Socket API
- Address structure
- Initialisation/Shutdown
- Connection management
- Data exchange(I/O)
- I/O multiplexing
- Optional parameters
- Signal handling
- Protocol behavior manipulation through ioctl
- Descriptor manipulation through fcntl
- Nonblocking I/O
- Hostname and address conversions
- Data manipulation functions
- Client-server design choices
16Socket 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
17- The generic socket address structure
- sockaddr
-
- address family
- protocol specific data
-
- The internet/IPv4 socked address structure
- sockaddr_in
-
- in_family Internet address family
- sin_port Transport layer Port Number
- in_addr sin_addr IP address
- sin_zero8 Padding
18Datatypes required by POSIX
- int8_t signed 8-bit integer - ltsys/types.hgt
- uint8_t unsigned 8-bit integer - ltsys/types.hgt
- int16_t signed 16-bit integer - ltsys/types.hgt
- uint16_t unsigned 16-bit integer - ltsys/types.hgt
- int32_t signed 32-bit integer - ltsys/types.hgt
- uint32_t unsigned 32-bit integer - ltsys/types.hgt
- sa_family_t address family of - ltsys/socket.hgt
- socklen_t length of socket address structure
-ltsys/socket.hgt - in_addr_t IPv4 address, normally uint32_t
ltnetinet/in.hgt - in_port_t TCP/UDP port, normally uint16_t
ltnetinet/in.hgt
19Data 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)
20Continued..
- 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)
deprecated
21Initialisation and Shutdown
- sockfd socket(domain, type, protocol)
- domain is the protocol/address family
AF_INET,AF_IPX.. - type is the the type of service
SOCK_DGRAM,SOCK_STREAM - 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
22Connection 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 accepts) - 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
23Continued
- connfd accept(sockfd,ptr_to_sockaddr,ptr_to_sockad
dr_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 its size respectively)
of the connecting entity - 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 socket - Above calls return 1 on error
24Illustrative Applications
- Application 1
- Iterative tcp server - echoserver.c
- tcp client - echoclient.c
- I/O using send/receive
- Illustrate basic socket calls
25Continued
- Application 2
- simultaneous tcp server - echoserver-select.c -
multiple ports - Same client
- Illustrate canonical/noncanonical mode of input