Lab No: 3 Date: Chat Server Objectives
Lab No: 3 Date: Chat Server Objectives
Lab No: 3 Date: Chat Server Objectives
CHAT SERVER
Objectives
Introduction
Writing a chat application with popular web applications stacks has traditionally been very hard.
It involves polling the server for changes, keeping track of timestamps, and it’s a lot slower than
it should be. Sockets have traditionally been the solution around which most real-time chat systems
are architected, providing a bi-directional communication channel between a client and a server.
A chat server is a computer dedicated to providing the processing power to handle and maintain
chatting and it users. This means that the server can push messages to clients and also support
multiple clients to initiate the conversation. To develop a chat server model, it is important to
understand the different types of server and the mode of communication supported.
Half-Duplex Operation
Technologies that employ half-duplex operation are capable of sending information in both
directions between two nodes, but only one direction or the other can be utilized at a time. This is
a fairly common mode of operation when there is only a single network medium (cable, radio
frequency and so forth) between devices. For example, in conventional Ethernet networks, any
device can transmit, but only one may do so at a time. For this reason, regular Ethernet networks
are often said to be “half-duplex”.
Full-Duplex Operation
In full-duplex operation, a connection between two devices is capable of sending data in both
directions simultaneously. Full-duplex channels can be constructed either as a pair of simplex links
or using one channel designed to permit bidirectional simultaneous transmissions. A full-duplex
link can only connect two devices, so many such links are required if multiple devices are to be
connected together.
Types of Server
There are two types of servers.
Iterative Server − this is the simplest form of server where a server process serves one
client and after completing the first request, it takes request from another client.
Meanwhile, another client keeps waiting. In other words, an iterative server iterates
through each client, handling it one at a time.
Concurrent Servers − this type of server runs multiple concurrent processes to serve many
requests at a time because one process may take longer and another client cannot wait for
so long. The simplest way to write a concurrent server under UNIX is to fork a child process
to handle each client separately. An alternative technique is to use threads instead (i.e.,
light-weight processes).
Client:
1. Include the necessary header files.
2. Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
3. Initialize the socket and set its attribute set. Assign the sin_family to AF_INET, sin_addr to
“127.0.0.1", sin_port to dynamically assigned port number.
4. Connect to server using connect () function to initiate the request.
5. Fork the process to create child process which is an exact copy of the calling process (parent
process).
6. If the process is child read the message from the console and send it to the server. If the process
is parent receive the message from the server and display it.
7. If the received message is “BYE” terminate the connection (kill the process).
8. Close the socket.
Note : Operations for UDP follows the same process but make necessary changes in the
system calls.
Lab exercise
1. Write two separate C programs using UNIX socket APIs illustrate full duplex mode chat
application between a single client and server using connection oriented service. Display
PID and PPID of both parent and child processes.
2. Write two separate C programs using UNIX socket APIs illustrate half duplex mode chat
application between a single client and server connection less service in which the server
estimates and prints all permutations of a string sent by the client.
3. Write two separate C programs (one for server and other for client) using socket APIs, to
implement the following connection-oriented client-server model.
i. The user at the client side sends an alphanumeric string to the server.
ii. The child process at the server sorts the numbers of the alphanumeric string in ascending
order. The parent process at the server sorts the characters of the alphanumeric string in
descending order.
iii. Both the processes send the results to the client along with its corresponding process ID.
Sample Output:
At the client side:
Input string: hello451bye7324
At the server side:
Output at the child process of the server: 1234457
Output at the parent process of the server: yollheeb
Additional exercise
1. Write a C program to simulate a menu driven calculator using client server architecture
that performs the following.
The client prompts the user with the options as listed below
1. Add/Subtract two integers
2. Find the value of ‘x’ in a linear equation
3. Multiply two matrices
4. Exit
Based on the user input the client prompts the user to enter required data. The client sends
the option chosen and the relevant data to the server. The server performs the required
operation and sends the result to the client. Note that if option 1 is selected, the server
provides result of both addition and subtraction of the two integers.
[OBSERVATION SPACE – LAB 3]