Sie sind auf Seite 1von 11

Courtesy: Elsevier Inc 2010

COMSATS Institute of Information Technology, Islamabad

Lecture 23

November 3 2011

Courtesy: Elsevier Inc 2010

November 3 2011

Application Programming Interface


Implementing network software is an essential part of understanding computer networks The place to start when implementing a network application is the interface exported by the network Since most network protocols are implemented in software (especially those high in the protocol stack), and nearly all computer systems implement their network protocols as part of the operating system, exported by the network, means interface that the OS provides to its networking subsystem. This interface is often called the network application programming interface (API).
2

Courtesy: Elsevier Inc 2010

November 3 2011

Application Programming Interface

Although each operating system is free to define its own network API (and most have), over time certain of 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 Unix is now supported in virtually all popular operating systems. The advantage of industry-wide support for a single API is that applications can be easily ported from one OS to another, and that developers can easily write applications for multiple OSs
3

Courtesy: Elsevier Inc 2010

November 3 2011

Application Programming Interface


It is important to keep in mind, however, that application programs typically interact with many parts of the OS other than the network E.g. they read and write files, fork concurrent processes, and output to the graphical display. Just because two systems support the same network API does not mean that their file system, process, or graphic interfaces are the same

Courtesy: Elsevier Inc 2010

November 3 2011

Two Separate Concerns


Each protocol provides a certain set of services API provides a syntax by which those services can be invoked in this particular OS The implementation is then responsible for mapping the tangible set of operations and objects defined by the API onto the abstract set of services defined by the protocol

Courtesy: Elsevier Inc 2010

November 3 2011

What is a Socket
Socket is the point where a local application process attaches to the network. The interface defines operations for creating a socket, attaching the socket to the network, sending/receiving messages through the socket, and closing the socket.

Courtesy: Elsevier Inc 2010

November 3 2011

Socket Programming

Sockets are normally used in the interprocess communication based on client server model The client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client 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 the client and the server The two processes each establish their own socket. The steps involved in establishing a socket on the client side 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 server side are as follows:

Courtesy: Elsevier Inc 2010

November 3 2011

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 port number on the host machine Listen for connections with the listen() system call Accept a connection with the accept() system call Send and receive data

Courtesy: Elsevier Inc 2010

November 3 2011

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 server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available

Courtesy: Elsevier Inc 2010

November 3 2011

Socket Types

There are two widely used socket types:


stream sockets datagram sockets

Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (User Datagram Protocol), which is unreliable and message oriented.
10

Courtesy: Elsevier Inc 2010

November 3 2011

Sample code

#include <sys/types.h> This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files. #include <sys/socket.h> The header file socket.h includes a number of definitions of structures needed for sockets.

#include <netinet/in.h> 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 stderr and then aborts the program.

11

Das könnte Ihnen auch gefallen