Network Lab Manual
Network Lab Manual
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
As shown above, the command with -i flag provides information on the interfaces. lo stands for
loopback interface.
3. ping
ping www.google.com
^C
A healthy connection is determined by a steady stream of replies with consistent times. Packet loss is
shown by discontinuity of sequence numbers. Large scale packet loss indicates problem along the
path.
Experiment 2
To familiarize and understand the use and functioning of System Calls used for
Operating system and network programming in Linux.
1. ps
This command tells which all processes are running on the system when ps runs.
ps -ef
This command gives processes running on the system, the owners of the processes and the names of
the processes. The above result is an abridged version of the output.
2. fork
This system call is used to create a new process. When a process makes a fork system call, a new
process is created which is identical to the process creating it. The process which calls fork is called
the parent process and the process that is created is called the child process. The child and parent
processes are identical, i.e, child gets a copy of the parent's data space, heap and stack, but have
different physical address spaces. Both processes start execution from the line next to fork. Fork
returns the process id of the child in the parent process and returns 0 in the child process.
#include<stdio.h>
void main()
int pid;
pid = fork();
if(pid > 0)
else
printf(“Iam child\n”);
The parent process prints the first statement and the child prints the next statement.
3. exec
New programs can be run using exec system call. When a process calls exec, the process is
completely replaced by the new program. The new program starts executing from its main function.
A new process is not created, process id remains the same, and the current process's text, data, heap,
and stack segments are replaced by the new program. exec has many flavours one of which is execv.
execv takes two parameters. The first is the pathname of the program that is going to be executed. The
second is a pointer to an array of pointers that hold the addresses of arguments. These arguments are
the command line arguments for the new program.
4. wait
When a process terminates, its parent should receive some information reagarding the process like the
process id, the termination status, amount of CPU time taken etc. This is possible only if the parent
process waits for the termination of the child process. This waiting is done by calling the wait system
call. When the child process is running, the parent blocks when wait is called. If the child terminates
normally or abnormally, wait immedaitely returns with the termination status of the child. The wait
system call takes a parameter which is a pointer to a location in which the termination status is stored.
5. exit
When exit function is called, the process undergoes a normal termination.
6. open
This system call is used to open a file whose pathname is given as the first parameter of the function.
The second parameter gives the options that tell the way in which the file can be used.
open(filepathname , O_RDWR);
This causes the file to be read or written. The function returns the file descriptor of the file.
7. read
The above function reads sizeof(buffer) bytes into the array named buffer. If the end of file is
encountered, 0 is returned, else the number of bytes read is returned.
8. write
1. Creating a socket
This sytem call creates a socket and returns a socket descriptor. The domain parameter specifies a
communication domain; this selects the protocol family which will be used for communication. These
families are defined in <sys/socket.h>. In this program the AF_INET family is used. The type
parameter indicates the communication semantics. SOCK_STREAM is used for tcp connection while
SOCK_DGRAM is used for udp connection. The protocol parameter specifies the protocol used and is
always 0. The header files used are <sys/types.h> and <sys/socket.h>.
Experiment 3
Implementation of Client-Server communication using Socket Programming and
TCP as transport layer protocol
Aim: Client sends a string to the server using tcp protocol. The server reverses the string and returns
it to the client, which then displays the reversed string.
Description:
This function call creates a socket and returns a socket descriptor. The domain parameter specifies a
communication domain; this selects the protocol family which will be used for communication. These
families are defined in <sys/socket.h>. In this program, the domain AF_INET is used. The socket has
the indicated type, which specifies the communication semantics. SOCK_STREAM type provides
sequenced, reliable, two-way, connection based byte streams. The protocol field specifies the protocol
used. We always use 0. If the system call is a failure, a -1 is returned. The header files used are
sys/types.h and sys/socket.h.
struct sockaddr_in {
u_short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8]; /*unused, always zero*/
};
struct in_addr {
u_long s_addr;
};
Example
Why htons is used ?. Numbers on different machines may be represented differently ( big-endian
machines and little-endian machines). In a little-endian machine the low order byte of an integer
appears at the lower address; in a big-endian machine instead the low order byte appears at the higher
address. Network order, the order in which numbers are sent on the internet is big-endian. It is
necessary to ensure that the right representation is used on each machine. Functions are used to convert
from host to network form before transmission- htons for short integers and htonl for long integers.
The binary value of the dotted decimal IP address is stored in the field when the function returns.
This is optional in the case of client and we usually do not use the bind function on the client side.
A server is identified by an IP address and a port number. The connection operation is used on the
client side to identify and start the connection to the server.
6. writing to a socket
In the case of TCP connection writing to a socket can be done using the write system call
This system call creates a socket and returns a socket descriptor. The domain field used is AF_INET.
The socket type is SOCK_STREAM. The protocol field is 0. If the system call is a failure, a -1 is
returned. Header files used are sys/types.h and sys/socket.h.
This call is used to specify for a socket the protocol port number where it will wait for messages. A
call to bind is optional on the client side, but required on the server side. The first field is the socket
descriptor of local socket. Second is a pointer to protocol address structure of this socket. The third is
the length in bytes of the structure referenced by addr. This system call returns an integer. It is 0
for success and -1 for failure. The header files are sys/types.h and sys/socket.h.
The listen function is used on the server in the connection oriented communication to prepare a socket
to accept messages from clients.
The accept function is used on the server in the case of connection oriented communication to accept a
connection request from a client.
The first field is the descriptor of server socket that is listening. The second parameter addressp points
to a socket address structure that will be filled by the address of calling client when the function
returns. The third parameter addrlen is an integer that will contain the actual length of address
structure of client. It returns an integer that is a descriptor of a new socket called the connection socket.
Server sockets send data and read data from this socket. The header files used are sys/types.h and
sys/socket.h.
Algorithm
Client
1. Create socket
3. Read the string to be reversed from the standard input and send it to the server
Read the matrices from the standard input and send it to server using socket
4. Read the reversed string from the socket and display it on the standard output
Read product matrix from the socket and display it on the standard output
Server
6. Read the string using the connection socket from the client
Client Program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
bzero(&server, sizeof(server) );
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &server.sin_addr);
printf("%s\n", buffer);
close(fd);
}
Server Program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
char buffer[100];
bzero(&server, sizeof(server) );
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = htonl(INADDR_ANY);
listen(sd,5);
read(data,buffer, sizeof(buffer));
len = strlen(buffer);
for( i =0; i<= len/2; i++)
{
temp = buffer[i];
buffer[i] = buffer[len - 1-i];
buffer[len-1-i] = temp;
}
close(data);
close(sd);
}
Output
Server
Client