Lec23b CSC339-3!11!11 Linux Delivered
-
Upload
zishan-gul-mughal -
Category
Documents
-
view
217 -
download
0
Transcript of Lec23b CSC339-3!11!11 Linux Delivered
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
1/11
Lecture 23
COMSATS Institute of Information Technology, Islamabad
November 3 2011
Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
2/11
2
Application Programming Interface
Implementing network software is an essential part ofunderstanding computer networks
The place to start when implementing a networkapplication is the interface exported by the network
Since most network protocols are implemented insoftware (especially those high in the protocol stack), and nearly all computer
systems implement their network protocols as part of theoperating system, exported by the network, means
interface that the OS provides to its networkingsubsystem. This interface is often called the network application
programming interface (API).
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
3/11
3
Application Programming Interface
Although each operating system is free to define itsown network API (and most have), over time certainof these APIs have become widely supported
They have been ported to operating systems other
than their native system E.g. the socket interface originally provided by the
Berkeley distribution of Unixis now supported invirtually all popular operating systems.
The advantage of industry-wide support for a singleAPI is that applications can be easily ported from oneOS to another, and that developers can easily writeapplications for multiple OSs
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
4/11
4
Application Programming Interface
It is important to keep in mind, however, thatapplication programs typically interact withmany parts of the OS other than the network
E.g. they read and write files, fork concurrentprocesses, and output to the graphicaldisplay.
Just because two systems support the samenetwork API does not mean that their filesystem, process, or graphic interfaces arethe same
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
5/11
5
Two Separate Concerns
Each protocol provides a certain set ofservices
API provides a syntax by which thoseservices can be invoked in this particular OS
The implementation is then responsible formapping the tangible set of operations and
objects defined by the API onto the abstractset of services defined by the protocol
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
6/11
6
What is a Socket
Socket is the point where a local applicationprocess attaches to the network.
The interface defines operations for creatinga socket, attaching the socket to the network,sending/receiving messages through thesocket, and closing the socket.
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
7/117
Socket Programming
Sockets are normally used in the interprocess communication based on clientserver model
The client needs to know of the existence of and the address of the server, butthe server does not need to know the address of (or even the existence of) theclient prior to the connection being established
Once a connection is established, both sides can send and receive information
The system calls for establishing a connection are somewhat different for theclient and the server The two processes each establish their own socket.
The steps involved in establishing a socket on the clientside are as follows:
Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data. There are a number of ways to do this, but the simplest is to use
the read() and write() system calls.
The steps involved in establishing a socket on the serverside are as follows:
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
8/118
Socket Programming
Create a socket with the socket() system call
Bind the socket to an address using the bind() system call.
For a server socket on the Internet, an address consists of a portnumber on the host machine
Listen for connections with the listen() system call Accept a connection with the accept() system call
Send and receive data
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
9/119
Socket Types Each socket needs a port number on a host Port numbers are 16 bit unsigned integers The lower numbers are reserved for standard
services e.g. the port number for the FTP serveris 21.
It is important that standard services be at thesame port on all computers so that clients willknow their addresses.
However, port numbers above 2000 aregenerally available
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
10/1110
Socket Types There are two widely used socket types:
stream sockets
datagram sockets
Stream sockets treat communications as acontinuous stream of characters, while datagramsockets have to read entire messages at once
Stream sockets use TCP (Transmission ControlProtocol), which is a reliable, stream orientedprotocol, and datagram sockets use UDP (UserDatagram Protocol), which is unreliable and messageoriented.
November 3 2011Courtesy: Elsevier Inc 2010
-
8/3/2019 Lec23b CSC339-3!11!11 Linux Delivered
11/1111
Sample code #include
This header file contains definitions of a number of data types used in system calls. These typesare used in the next two include files.
#include The header file socket.h includes a number of definitions of structures needed for sockets.
#include The header file in.h contains constants and structures needed for internet domain addresses.
void error(char *msg) {
perror(msg);exit(1);}
This function is called when a system call fails. It displays a message about the error on stderrand then aborts the program.
November 3 2011Courtesy: Elsevier Inc 2010