1. Kamran Ashraf
MSCs 1st Semester
13-NTU-4009 National Textile University Page 1
“APPLICATION PROGRAMMING INTERFACE”
In General network applications have two parts simultaneously, application for interaction with
users and protocol for communication. Network protocols are part of the operating system in
most computer systems. “Application programming interface (API)” is the interface provided by
the OS to use the network. API’s can be independently written for each OS and can also be
ported to other OS, that’s why few API’s like Socket interface became part of nearly all
operating systems. Socket interface was defined in unix by berkeley distribution. It was then
ported to other operating systems which made easier for developers to make applications for
different operating systems.
Conceptually socket is used by the programmer to send/receive data on the network. It is
recognized by IP and port number. Main operation of interface can be explained as:
Creating the socket
Socket attachment to network
Send/Receive through the socket
Close the socket
Typical operations of client application:
• Socket creation
• Resolve server IP and port number
• Connection to the server
• Read/Write data through socket
• Close the socket
Typical operations of server application:
• Socket creation
• Binding IP and port number to socket
• Listen how many client requests pending
• Accept connection from client
• Read/Write data through socket
• Close the socket
2. Kamran Ashraf
MSCs 1st Semester
13-NTU-4009 National Textile University Page 2
To create a socket we use following operation on both client and server:
• “int socket(int domain, int type, int protocol)”
o Operation was designed the way any protocol suite can be used
o Returns a handler which identifies the socket
o Domain argument indicates protocol family to be used
i.e. “PF_INET” is used for internet family
o Type argument denotes communication type
i.e. “SOCK STREAM” is used for reliable byte stream
o Protocol argument indicates particular protocol to be used
Can be left unspecified as combination of previous arguments indicates
TCP protocol
To establish connection on server we use following:
• “int bind(int socket, struct sockaddr *address, int addr len)”
o Binds IP and port number to socket
o i.e. Port 80 is commonly used by web servers
• “int listen(int socket, int backlog)”
o Listens how many client requests pending
• “int accept(int socket, struct sockaddr *address, int *addr len)”
o Blocks the whole process until new connection is established
o Returns a new socket for established connection
To establish the client connection we use following:
• “int connect(int socket, struct sockaddr *address, int addr len)”
o Only address of remote participant is given in practice
o OS selects any un-used port for connection
To send/receive data application invokes following operation:
• “int send(int socket, char *message, int msg len, int flags)”
o Sends message through connected socket
• “int recv(int socket, char *buffer, int buf len, int flags)”
o Receives message through connected socket
To close the socket we use:
• “Int close (int socket)”