You are on page 1of 35

System Programming and Computer Control

CT047-3-2-SPCC

Client-Server Model: Socket Interface


Level-2

Prepared by: KL First Prepared on: 9-09-07 Last Modified on: Quality checked by: xxx Copyright 2007 Asia Pacific Institute of Information Technology

Topic & Structure of the lesson


Client-Server Model Socket Interface

CT014-1 Hardware, Systems Software and Networks

Key Terms you must be able to use


If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams:-

Active close Active open Application program Application programming interface (API) Client Client-server model Concurrent client Concurrent server Connectionless iterative server Connection-oriented concurrent server

Datagram socket Iterative client Iterative server Process Raw socket Server Socket Socket interface Stream socket

CT014-1 Hardware, Systems Software and Networks

Client-Server Model: Socket Interface


CT014-1 Hardware, Systems Software and Networks

Client and Server Programs


Client-server program are written in languages such as C, C++, Java, Lab View and Perl. This type of programming is very involved and requires and advanced knowledge of programming as well as knowledge of the particular language.

CT014-1 Hardware, Systems Software and Networks

Client-Server Model
Relationship Concurrency

Processes

CT014-1 Hardware, Systems Software and Networks

Relationship
A computer runs a program to either request a service from another computer or provide a service to another computers.

Application programs, running at two end computers and communicating with each other, are needed.
Enabling communication between two application programs, one running at the local site and the other running at the remote site. Many question arise when we want to implement the approach. Q. Should both the applications be able to request services, or should they just do one. A. An application program, called the client, running on the local machine request a service from another application program, called the server, running on the remote machine.
CT014-1 Hardware, Systems Software and Networks

Client-server model

CT014-1 Hardware, Systems Software and Networks

Relationship
Q. Should an application program provide services only to one specific application program installed somewhere in an internet, or should it provide services for any application program that request this service? A. A server providing service for any client, not a particular client.

Q.
A.

When should an application program be running all the time or just when there is a need for the service? A client program, which request a service, should run only when it is needed. The server program, which provides a service, should run all the time because it does not know when its services will be needed.

CT014-1 Hardware, Systems Software and Networks

Relationship
Q. Should there be only one universal application program that can provide any type of service a user wants? Or should there be one application program for each type of service? A. In the internet, services needed frequently and by many users have specific client-server application programs such Internet Explore, Firefox, Mozilla and so on. We do have separate client-server application programs that allow users to access files or send email such windows mail, FTP, office outlook and so on. Services that are more customized, we should have one generic application program that allows users to access the services available on a remote computer.

CT014-1 Hardware, Systems Software and Networks

Client-server relationship

CT014-1 Hardware, Systems Software and Networks

Client
A client is a program running on the local machine requesting service from a server. A client program is started by the user and terminates when the service is complete.

A client opens the communication channel using the IP address of the remote host and the well-known port address of the specific server program running on that machine. This is called an active open. The client sends its request and receives a response. The whole process is finite and eventually comes to an end.
The client closes the communication channel with an active close.
CT014-1 Hardware, Systems Software and Networks

Server
A server is a program running on the remote machine and providing service to the clients. When it starts, it opens the door for incoming requests from clients, but it never initiates a service until it is requested to do so. This is called a passive open. A server program is an infinite program. It waits for incoming requests from clients. When a request arrives, it responds to the request.

CT014-1 Hardware, Systems Software and Networks

Concurrency
Concurrency in Clients Clients can be run on a machine either iteratively or concurrently. Running clients iteratively means running them one by one; one client must start, run and terminate. Concurrent clients; that is, two or more clients can run at the same time.

CT014-1 Hardware, Systems Software and Networks

Concurrency
Concurrency in Servers An iterative server can process only one request at a time; it receives a request, processes it, and sends the response to the requestor before it handle another request. A concurrent server can process many requests at the same time.

CT014-1 Hardware, Systems Software and Networks

Connectionless iterative server

UDP are normally iterative. Server processes one request at a time.


CT014-1 Hardware, Systems Software and Networks

Connection-oriented concurrent server

TCP are normally concurrent. Server can serve many clients at the same time.
CT014-1 Hardware, Systems Software and Networks

Processes
Most operating system, including UNIX, distinguish a program from a process.

Program and process are related to each other, they are not same thing.
In UNIX, a program is source code. It defines all the variables and actions to be performed on those variables. A process, on the other hand, is an instance of a program. When the OS can create several processes from one program, which means several instances of the same program are running at the same time (concurrently). Memory is allocated for each process separately.
CT014-1 Hardware, Systems Software and Networks

Socket Interface
Sockets Connectionless Iterative Server

Connection-Oriented Server

CT014-1 Hardware, Systems Software and Networks

Socket Interface
The Socket Interface was originally based on UNIX and defines a set of system calls (Procedures). Communication structure need in socket programming and it is define in the operating system as a structure with five fields. 1. Family The protocol group: IPv4, IPv6, UNIX domain protocolsl 2. Type Stream socket, Packet Socket, or raw socket. 3. Protocol Set to zero for TCP and UDP 4. Local socket address A combination of the local IP address and the port address. 5. Remote socket address A combination of remote IP address and the port address.
CT014-1 Hardware, Systems Software and Networks

Socket structure

CT014-1 Hardware, Systems Software and Networks

Socket types

CT014-1 Hardware, Systems Software and Networks

Socket types
The Socket Interface defines three types of sockets: the stream socket, the packet socket, and the raw socket.

A stream socket is designed to be used with a connectionoriented protocol such as TCP. It is connect one application program to another across the Internet. A Datagram Socket is designed to be used with a connectionless protocol such as UDP. It send a message from one application to another across the Internet. Raw Socket Some protocols such as ICMP or OSPF that directly use the services of IP use neither stream socket nor datagram socket.
CT014-1 Hardware, Systems Software and Networks

Connectionless Iterative Server


Iterative client-server communication using UDP and datagram sockets. The server pay no attention to the other packets. The packets from one client or from many client, are store in a queue, waiting for service. They are processed one by one by in order of arrival. The server uses one single well-known port; All the packets arriving at this port wait in line to be served.
CT014-1 Hardware, Systems Software and Networks

Socket interface for connectionless iterative server

CT014-1 Hardware, Systems Software and Networks

Connectionless Iterative Server Function


Clients
1. Create a socket Asks the OS to create a socket. There is no need to binding here. The OS normally fills in the information in the socket. 2. Repeat a) Send asks the OS to send a request. b) Receive asks the OS to wait for the response and deliver it when it has arrived. 3. Destroy When the client has no more requests, it asks the OS to destroy the socket.
CT014-1 Hardware, Systems Software and Networks

Server
1. 2. Create a socket Asks the OS to create a socket Bind Asks the OS to enter information in the socket related to the server. This is called binding the server socket. Repeat a) Received a request asks the OS to wait for a request b) Process Process by the server. c) Send Response to sent to the client.

3.

Socket interface for connection-oriented concurrent server

CT014-1 Hardware, Systems Software and Networks

Connection-Oriented Concurrent Server


Concurrent client-server communication using Iterative TCP and stream socket. This server serves many client at the same time. It is a stream of bytes that could arrive in several segments, and the response could occupy several segments. The connection remains open until the entire stream is processed, and then the connection is terminated. The server must have one buffers for each connection. It use parent and child servers. A parent server use well known port and running infinitely and accepting connection from clients is called a parent server. The parent server creates a child server and an ephemeral port and lets the child server handle the client. It free itself so that it can wait for another connection and server can serve several clients concurrently.
CT014-1 Hardware, Systems Software and Networks

Connection Oriented Concurrent Server Function Server Clients


1. Create a socket Asks the OS to create a socket. 2. Connect asks the OS to make a connection. 3. Repeat a) Write Client sends a stream of bytes to be sent to the server. b) Read The client receives a stream of bytes from the server. 4. Destroy After the client has finished, it asks the operating system to destroy the socket . The connection is also closed.
CT014-1 Hardware, Systems Software and Networks

1. 2.

3. 4.

Create a socket Asks the OS to create a socket Bind Asks the OS to enter information in the socket related to the server. This is called binding the server socket. Listen Asks OS to listen to the client. TCP is a connection orientated protocol. Repeat a) Create a child - OS create temporary child process and assigns the duty of serving the client to the child. The parent process it free to listen for new clients. b) Create a new socket - .A new socket is created to be used by the child process. c) Repeating Child repeats the following steps. Read child reads a stream of bytes Process Processes the stream of bytes Write Child writes the results as a stream of bytes to the connection. d) Destroy socket After the client has been served, the child process asks the OS to destroy the temporary socket

Quick Review Questions


Refer to tutorial question.

CT014-1 Hardware, Systems Software and Networks

Summary Client-Server Model: Socket Interface

In the client-server model, the client runs a program to request a service and the server runs a program to provide the service. These two programs communicate with each other. One server program can provide services for many client programs. Clients can be run either iteratively (one at a time) or concurrently (many at a time).

Servers can handle clients either iteratively (one at a time) or concurrently (many at a time).
CT014-1 Hardware, Systems Software and Networks

Summary Client-Server Model: Socket Interface

A connectionless iterative server uses UDP as its transport layer protocol and can serve one client at a time. A connection-oriented concurrent server uses TCP as its transport layer protocol and can serve many clients at the same time. When the operating system executes a program, an instance of the program, called a process, is created.

If two application programs, one running on a local system and the other running on the remote system, need to communicate with each other, a network program is required.
CT014-1 Hardware, Systems Software and Networks

Summary Client-Server Model: Socket Interface

The socket interface is a set of declarations, definitions, and procedures for writing client-server programs.

The communication structure needed for socket programming is called a socket.


A stream socket is used with a connection-oriented protocol such as TCP. A datagram socket is used with a connectionless protocol such as UDP. A raw socket is sued by protocols such as ICMP or OSPF that directly use the services of IP.
CT014-1 Hardware, Systems Software and Networks

Question and Answer Session

Q&A
CT014-1 Hardware, Systems Software and Networks

Next Session
Lab View

CT014-1 Hardware, Systems Software and Networks

You might also like