You are on page 1of 12

Client Server Model

• The client machine (or the client process) makes the request
for some resource or service, and the server machine (the
server process) handles the request and sends the response
(result) back to the client.

• In database server, the client queries the server for records


from the database, and the server looks up the records and
responds to the client.
• The user on the client machine may not even know what
machine in the network actually has the database.
• In fact, the database may be distributed across several
machines on the network.
Client Server Model
• The Client needs to know the existence and
the address of the server.
• However, the server does not need to know
the existence or address of the client prior to
the connection.
• Once a connection is established, both sides
can send and receive information.
How to establish a connection?
• Both client and server will construct a
socket.
• A socket is one end of an interprocess
communication channel.
• The process to establish a socket on the
client side is different than the process to
establish a socket on the server side.
How to establish a socket on the
client side
• Create a socket using the socket() system
call.
• Connect the socket to the address of the
server using the connect() system call
• Send and receive data. You can use read()
and write() system calls. However, there are
different ways to send and receive data.
How to establish a socket on the
server side
• Create a socket using 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 of the host machine.
• Listen for the connections using the listen() system
call.
• Accept a connection using the accept() system call
• Send and receive data. You can use read() and write()
system calls. However, there are different ways to send
and receive data.
Address domain and the socket
type
• The client and server processes can communicate
with each other only if their sockets are in the
same domain and of the same type.
• Two widely used address domains are:
 Unix domain (in which two processes share a
common file system).
 Internet domain (in which two processes run on
any two hosts on the Internet)
Unix and Internet domains
• The address of a socket in the Unix domain is a
character string, an entry in the file system.
• The address of a socket in the Internet domain is
the Internet address (IP address) of the host
machine.
• Each socket needs a port number (16 bits unsigned
integers) on the host.
• The lower numbers are reserved in Unix for
standard services (e.g. 21 is used as the port
number for the FTP server).
• Generally, port number above 2000 are available.
Socket types
• Two widely used socket types are:
• Stream socket: treats communications as a
continuous stream of characters.It uses TCP
• Datagram socket: reads entire message at
one time. It uses UDP.
Sample Client and Server codes
• Files (client and server) are posted on the Internet.
• Download these two files (server.c and client.c)
• Compile them into two executable files, server and client.
• How to run server?
 Start the server first.
 If your server is running on a machine called Merlin, you
need to pass the port number as an argument (any
number between 2000 and 65535).
 If the port number is already in use, the server will let
you know and exit. Then select another port number.
 To run server, type server 3000
How to run the client
• To run the client, you have to pass two
arguments:
 The name of the host on which the server is
running
 The port number on which the server is
listening for connections.
 To run client, type client Merlin 3000
A Server on the Internet domain
/* A simple server in the internet domain using TCP The port
number is passed as an argument */

#include <stdio.h>
#include <sys/types.h> // def. of data types used in system calls
#include <sys/socket.h> //def. of structures needed for sockets
#include <netinet/in.h> // constants and structures for Internet
// domain addresses
void error(char *msg) // func. Is called when system calls fail
{
perror(msg);
exit(1);
}
Server Code
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen; // sockfd, newsockfd=>file
// descriptor, portno stores the port number on which the server
// accepts connections, clilen=> size of the address of the client, n=>
no. of char to read or write
char buffer[256];
struct sockaddr_in serv_addr, cli_addr; //sockaddr_in => structure
containing Internet address
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

You might also like