Lecture 23
November 3 2011
November 3 2011
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
November 3 2011
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
November 3 2011
November 3 2011
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.
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:
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
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
November 3 2011
Socket Types
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
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.
11