cs0313-networking lab laboratory manual iii … lab.pdf · cs0313-networking lab . laboratory...
TRANSCRIPT
CS0313-NETWORKING LAB
LABORATORY MANUAL
III CSE
V SEMESTER
ACADEMIC YEAR: 2012-2013
SCHOOL OF COMPUTER SCIENCE & ENGINEERING
SRM UNIVERSITY,
SRM NAGAR, KATTANKULATHUR-603203.
KANCHEEPURAM DISTRICT.
1
SCHOOL OF COMPUTER SCIENCE & ENGINEERING
SRM UNIVERSITY,
SRM NAGAR, KATTANKULATHUR-603203.
SUBJECT : NETWORKING LAB SUBJECT CODE : CS0313
SEMESTER : V CLASS : III CSE
HOURS/WEEK : 6 HOURS
EX. NO NAME OF THE EXPERIMENTS PAGE NO
1 Network Primitives 3
2 Socket creation 10
3 Client server application in TCP 11
4 Client server application in UDP 12
5 Simulation of echo 14
6 Simulation of ping 16
7 Simulation of talk command 18
8 File transfer 20
9 Remote command execution 22
10 Simulation of ARP 24
11 Case study of routing algorithms 26
12 Web page downloading 27
13 Hamming codes 29
14 RSA Algorithm 31
2
Exp: 1 NETWORK PRIMITIVES
Aim To study the basic unix commands.
Socket:
It creates an end point for communication.
a) Synopsis
#include<sys/types.h>
#include<sys/socket.h>
int socket( int domain, int type, int protocol);
b) Description:
The domain parameter specifies a communication domain, this selects
the protocol family which will be needed for communication. These families are defined in <sys/socket.h>. Some of the format are:
Name Purpose Man Page
PF_UNIX Local UNIX(Y)
PF_LOCAL Communication
PF_INET IPV4IP ip(y)
PF_INET IPV6IP
PF_IPX IPX_NOVELL
PROTOCOLS
3
The socket has the indicated type which specifies the communication semantics. The various types are SOCK_STREAM, SOCK_PACKET, SOCK_DGRAM, SOCK, SEO PACKET, SOCK_RAW, SOCK_ROM.
The protocol parameter specifies a particular
protocol to be used within a socket. Normally a single protocol exists to support a particular socket type within a given protocol family(U_TCP,I_UDP).
c) Return Value
One is returned if an error occurs otherwise the return value is a description referring the socket.
d) Errors
EDROTON O SUPPORT, EA7N O SUPPORT, EN7ILE, EN7TLE, E ACCESS, ENO BU77 (OR) ENOMEN, EINYAL etc other errors may be generated by underlying module.
Bind:
This function is to associate a socket with a port.
a) Synopsis:
#include<sys/types.h>
#include<sys/socket.h>
int bind( int fd, struct sock add ncy_addr,int addrlen)
b) Description:
The fd parameter is the socket file descriptor returned by the socket() call. The ney_addr parameter is a pointer to struct sock addr. The addrlen parameter is set to size of (struct sockaddr).
c) Return Value:
-1 is returned if an error occurs otherwise the value is if the bind is successful.
4
Connect:
It initiates a connection on a socket & is used to connect to an IP address on a defined port.
a) Synopsis:
#include<sys/types.h>
#include<sys/socket.h>
int connect( int fd, struct sockaddr*servaddr, int addrlen)
b) Description:
The fd parameter is the socket file descriptor returned by the socket() call. The servaddr parameter is a pointer to struct sock addr that contains the destination IP address & port. The addrlen parameter is set to sizeof (struct sockaddr).
c) Return value:
If the connection succeds o is returned, on error -1 is returned & error set approximately.
d) Error:
EBADF, E FAULT, E NOT SOCK, SIS CONN, E CONN REFUSED, E TIMED OUT, ENETUREACH, E ADDRIN USE, E IN PROGRESS.
Listen:
a) Synopsis:
#include<sys/socket.h>
int listen( int s, int backlog);
b) Description:
To accept connections, a socket is first created a willingness to accept incoming connection & a queue limit for incoming connections are accepted with accept(). Listen call applies only to socket of types SOCKET_STREAM & SOCK_SEQPACKET.
c) Return value:
On success, 0 is returned. On error -1 is returned & error is set
5
approximately.
d) Error:
The various errors that can occur are E ADDR INUSE, EBAO7,ENOTSOCK & EOPNOT SUPP.
Accept:
It accepts job sent to destination.
a) Synopsis:
#include<sys/socket.h>
int accept(int fd, void *addr, int addrlen)
b) Description:
It instructs the printing systems to accept print jobs to the specified destinations. The fd parameter is the socket file description returned by the listen() call. The addr parameter is a pointer to struct sockaddr is where you can determine which host is calling you from which port.
Send:
It is used to send data over stream sockets or connected datagram sockets.
a) Synopsis:
#include<sys/types.h>
#include<sys/socket.h>
int send( int fd const void*msg, int len, int flag)
b) Description
The fd parameter is the socket description where you want to send data to the message parameter is a pointers to the data you want to send.
c) Return value
It returns the number of bytes send out & it will return -1 on error.
6
Send to:
It is used to send data over unconnected datagram sockets.
a) Synopsis:
#include<sys/types.h>
#include<sys/socket.h>
b) Description:
The fd parameter is the socket where you want to send data to the message parameter is a pointer to the data you want to send. The length of the data you want to send.
c) Return Value:
It returns the number of bytes send out and it will return -1 on error.
Recv:
It is used to send data over stream sockets or connected datagram sockets.
a) Synopsis:
#include<sys/types.h>
int (recv( int fd, void*buff, int len, unsigned, int flags));
b) Description
The fd parameter is the socket description file to read from. The buff parameter is the buffer to read the information into. The len parameter is the mean length of buffer. The flag parameter is set to 0.
c) Return value
It returns the number of bytes received & it will return -1 on error.
Close:
It is used to close the socket connection on your socket description.
a) Synopsis:
close(fd);
7
b) Description:
When we call close(), there will be no more writes or reads & if we try to read or write. We will receive an error. The connection on the fd, socket file description is closed using this command.
Ping:
Send ICMP ECHO_REQUEST packets to host.
a) Synopsis:
Ping[-AaDdfnoQqRrv][-c count][i-wait][-l preload][-M Mask time][-t time out]
[-z to s] host
Ping[-AaDdfLnoQqRrv][-c count][-I iface][-i wait][-l preload][-M Mask time][-mtt]
[-p policy][-p pattern][-s src addr][-s packetsize][-T tt][-t time out][-z to s][n cost-group]
b) Description:
The ping utility uses the ICMP protocols mandatory ECHO_REQUEST datagram,to client an ICMP ECHO_REQUEST from a host or gateway.
Recv From:
It is used to send data over unconnected datagram sockets.
a) Synopsis:
#include<sys/types.h>
#include<sys/socket.h>
int recvfrom( int fd,void vbuff, int len, unsigned int flags, struct sockaddr*from, int from len)
b)Description:
The fd parameter is the socket descriptior to read from. The buff parameter is
the buffer to read information into. The len parameter is the max length of the
buffer.
8
c) Return value:
It returns the number of bytes received and it will return -1 on error.
System:
It passes a command to the shell.
Library: Standard C library( lib C,-lc)
a) Synopsis:
#include<stdlib.h>
int system( const char*string)
b) Description:
The system () function hands the argument string to the command
interpreter sh(1). The calling process waits for the shell to finish executing the
command , ignoring SIGINT & SIGQUIT & blocking SIGCHLD.
If string is a null pointer, system () will return non-zero if the command interpreter sh(1) is available and zero if it is not.
.
Result
Thus the above list of primitive has been studied.
9
Exp: 2 Socket creation
Aim To write a program to create a socket.
Algorithm 1. Create a socket function & store. Its value in a variable has
S=Socket(PF_INET,SOCK_STREAM,0);
2. If the value is less than zero then there has been some error in socket
creation & display error messages & exit.
3. End of program.
Output
Socket is created
Result The given program for the creation of socket has been written & executed
successfully.
10
Exp: 3 Client server application in TCP
Aim To create a program to implement client-server application using TCP
Algorithm Server side
1. Initialize the structure with two variables i.e add & sadd.
2. Binding the server, if return value is negative.
3. Now accept the data from client & check the returned value.
4. End the server program.
Client side
1. The socket connection is checked.
2. The client is connected to server using the IP address & required text is
given.
3. The receiving text is received in server side.
4. end of client program.
Output Client side
Enter the IP address 172.0.0.1
Enter the data hello
Server side
Server received hello
Result The given program for client-server application using TCP has been written &
executed successfully.
11
Exp: 4 Client server application in UDP
Aim To write a program to implement simple client-server application using
UDP.
Algorithm Server side
1. Initialize the structural variables.
2. Create a socket server side & bind it.
3. If value is less than zero, than display error message & exit.
4. Receive the data from client side by command else print “receive error”.
5. When the data properly received,print the data received from the server side.
6. Close the socket.
7. End
Client side
1. Create the socket in the client side using socket creation command & send
error if error in creation.
2. Get the IP address from user.
3. Connect the socket with the information initialized with the help of structure
variables.
4. Send the input by the user to the server side.
5. Close the socket.
12
Output Client side
Enter the IP address 127.0.0.1
Enter the data SRM
Server side
Server received SRM
Result
Thus the program for client-server application using UDP was written &
executed successfully.
13
Exp: 5 Simulation of echo
Aim To write a C-program for simulation of echo using UDP.
Algorithm 1) Create a socket.
2) Take IP address. Execute bind.
3) Check for error.
4) Execute send and receive for getting data from client.
5) Print error if not getting.
6) End the program.
14
Output
Enter the IP address 127.0.0.1
Enter the data Chetan
Receiver received: Chetan
Result
Thus the program for simulation of echo using UDP was executed successfully.
15
Exp: 6 Simulation of ping
Aim To write a program to simulate the ping program.
Algorithm Server side
1. Start program & initialize the structure with two structure variables
cadd, sadd.
2. Create a socket using the socket creating command. Initialize the server
address structure.
3. Bind the server information.
4. Accept the data from the client.
5. If the server is getting successfully, print the data sends. Close the
socket.
6. End the program.
Client side
1. Create the socket in client side.
2. Initialize the client address structure.
3. Connect the client to the server by getting the IP address.
4. If successfully connected , ask the server to enter the data.
5. Send data to server if value is negative, display error & exit.
6. Close socket & end the program.
16
Output
CLIENT :
Enter the IP address 127.0.0.1
CONNECTION ESTABLISHED
Enter the data SRM
SERVER:
PINGING
Server received SRM
Result
Thus the program for simulation of Ping command was written & executed
successfully.
17
Exp: 7 Simulation of talk command
Aim
To write a program for chat between server and client.
Algorithm
Server side
1) Create a socket & bind it.
2) Receive message from client.
3) Print the message.
4) Send message to client
5) If message is “bye” then end the program.
Client side
1) Create a socket.
2) Get the IP address.
3) Send message to server
4) Receiver message from client.
5) If message is “bye” then end the program.
18
Output Enter the IP address 127.0.0.1
Client: hi
Server: hi
Client: how are you?
Server: fine,merci.
Client:bye
thanku
Result
Thus the program for simulation of talk command was written & executed successfully
19
Exp: 8 File transfer
Aim
To write & execute File transfer program.
Algorithm Server side
1) Create a socket & initialize all structure variables.
2) Bind the values, if value is (-)ve then report & exit.
3) Listen & accept from the client.
4) Open the file and store the received data into the file using Fwrite in the write mode.
5) Print the string & close the socket.
6) End the program.
Client side
1) Create a socket, if the value is negative then report error & exit.
2) Open an existing file & write the contents as string & close the file.
3) Initialize all the structure variables.
4) Send the string to the server side and check the value. If it is -1 then report error and exit.
5) Close the socket and exit.
20
Output
Client side
enter the IP address 127.0.0.1
enter the file name to send cse.c
enter the file to save srm.c
Server side
waiting……….receiving file and writing into srm.c file successfully.
Result
Thus the program for file transfer was written & executed successfully.
21
Exp: 9 Remote command execution
Aim
To write a program for remote command execution.
Algorithm
Server side
1) Create a socket using socket creation command, if value is negative then report error & exit.
2) Initialize all the variables and bind the values.
3) Accept the client side connection.
4) Get the command from user & write it for client side where it is to invoked.
5) If value is less then zero then display error and exit.
6) Close the socket and end the program.
Client side
1) Create a socket and if value returned is negative then report error and exit.
2) Initialize all the structures variables & connect the client side to the server side.
3) Read the command send by server to be executed in the client side.
4) Invoke the system command to execute the command.
5) Close the socket and end the program.
22
Output
Server side
enter the command: ls
Client side
enter the ip address 127.0.0.1
ls
srm.c file.c ping.c udp.c tcp.c
Result
Thus the program for remote command execution was written & executed successfully.
23
Exp: 10 Simulation of ARP
Aim
To write a program for simulation of ARP.
Algorithm
1) Include necessary header file
#include<arpa/inet.h>
2) Take the IP address from the user
3) Check wheather the IP address is valid.
4) Concatenating the IP address with ping –c 1
5) Simulation is done using system(a) which runs the syntax ping –c 1 172.16.6.7.
6) End the program.
24
Output
Enter the IP address: 172.16.6.7
Ping 172.16.6.7(172.16.6.7): 56 data bytes
----------172.16.6.7 ping statistics--------------------
1 packet transmitted, 1 packet received,0% packet loss
round trip min/avg/max=0.481/0.481/0.481 ms
Result
Thus the program for simulation of ARP was written & executed successfully.
25
Exp: 11 Case study of routing algorithms
Aim
To study the various routing algorithms
Description 1. Distance vector routing algorithm
2. Flooding
3. Link state routing algorithm
Result Thus the study about the various routing algorithms has been completed successfully.
26
Exp: 12 Web page downloading
Aim
To download a webpage using Java
ALGORITHM:
1. Accept URL from user
2. Make an object for URL
3. Print the protocol, port host and form
4. Get the file mentioned in URL and download it line-by-line.
5. Display the contents of the file after download.
27
OUTPUT:
Enter url
HYPERLINK "http://203.199.200.86/admission.php"http://203.199.200.86/admission.php
protocol:http
port: -1
host: 203.199.200.86
form: HYPERLINK "http://203.199.200.86/admissiion.php"http://203.199.200.86/admissiion.php
file downloaded
RESULT: The webpage is successfully downloaded and the contents are displayed and verified.
28
Expt:13 HAMMING CODE
AIM: To calculate the hamming codeword corresponding to the data word sent to the server.
ALGORITHM:
SERVER
1. Declare the required variables.
2. Initialise family, port and address with parameters.
3. Use bind, recvfrom commands with parametrs.
4. Receive the data word and convert to the hamming code.
5. Terminate the connection.
CLIENT
1. Declare the required variables.
2. Initialise family, port and address with parameters.
3. Use sendto command with parameters.
4. Display the new corrected data word.
5. Terminate the connection
29
OUTPUT:
//sender side
Socket is created
Enter server address 127.0.0.1
Enter the data word : 0
0
1
1
New string : 0011010
Sent
//receiver side
Socket is created
Binding successful
Received
The received string is:0011010
The syndrome is: 110
The new string is: 0011010
Result: Thus the corrupted bits are corrected using hamming code method.
30
Expt:14 RSA ALGORITHM
AIM:
To write a program for encrypting and decrypting the message using RSA Algorithm.
ALGORITHM:
1. Declare the required variables.
2. Select any two large prime no.s p & q.
3. Compute n = pq and z = (p-1)(q-1).
4.Select 'e' relatively prime to z.
5. Compute 'd'. d*e mod z = 1.
6. Print the encrypting key {e,n}.
7. Print the decrypting key {d,n}.
8. Accept the plain text from the user.
9. Compute cypher text using p = (data^e) mod n.
10. Decrypt the cypher text using p = c^d mod n.
11. Print the encrypted and decrypted value.
31
32
Output:
Enter the prime numbers p and q:3 11
Enter the value of e: 7
The encryption key is:7,33
The decryption key is 3,33
Enter the plain text: 20
Cipher text:26
Plain text:20
RESULT: Encrypting and Decrypting of the message using RSA algorithm is successfully performed and the values are displayed and verified.