Indus institute of Technology & EngineeringAdvance Computing Networks

Advance Computer Networks
Practical Manual
4/16/2012
Vishal Shah
(08BCER059)

TABLE OF CONTENT

Sr. No / Subject / Date / Sign
1 / TO STUDY ABOUT VARIOUS NETWORKING COMMANDS.
2 / IMPLEMENTING CLINET /SERVER MODEL USING STREAM SOCKET.
3 / IMPLEMENTING CLINET /SERVER MODEL USING DATAGRAM SOCKET.
4 / IMPLEMENTING SLIDING WINDOW PROTOCOL.
5 / IMPLEMENTING DIJKSA’S SHORTEST PATH ROUTING ALGORITHM.
6 / IMPLEMENTING DISTANCE VECTOR ROUTING ALGORITHM.
7 / IMPLEMENTING FTP TRANSFER PROGRAM.
8 / TO COMPARE IPV4 AND IPV6.
9 / TO DETERMINE THE CLASS OF IP ADDRESS FROM THE INPUT BINARY NOTATION.
10 / TO DETERMINE THE HARDWARE ADDRESS CORRESPONDING TO THE INPUT IP ADDRESS.
11 / TO IMPLEMENT THE ROUTING ALGORITHM.
12 / TO IMPLEMENT CLIENT/SERVER PROGRAM.
13 / TO DETERMINE ENCRYPTION AND DECRYPTION OF STRINGS.
14 / TO IMPLEMENT DATA LINK LAYER.

PRACTICAL-1

AIM: TO STUDY ABOUT VARIOUS NETWORKING COMMANDS.

1)Ping(Packet Internet Gopher):-

Ping is one of the most commonly used and well known commands.

Ping allows a user to ping another network IP address. this can be to determine ,if the network is able to communicate with the network.

It sends ICMP echo-request packets to network hosts.

Syntax:-

ping [-+] [-a] [- Ill] [-r count] [[-j host -list] | [-k host -list]]

[-w time out] Destination –list.

Example:-

  1. Ping 192.168.1.1 –t /all

2)Trace route:-

Lets you trace packets and find if your connection is broken beyond. Your IP address.

This command is very useful for distinguishing network or router issues. If the domain does not work or is not available you can trace route an IP.

The tracert command in MS-DOS and Windows or the trace route command in Unix and Linux and variants is another commonly used network command to help determine network related issues or slowdowns. Using this command you can view a listing of how a network packet travels through the network and where it may fail or slow down. Using this information you can determine the computer, router, switch or other network device possibly causing your network issues.

Syntax: -

trace route [-d] [-F] [-I] [-n] [-v] [-x] [-f first_ttl] [-g gateway [-g gateway] | -r] [-i iface] [-m max_ttl] [-p port] [-q nqueries] [-s src_addr] [-t tos] [-w wait time] host [packetlen]

Example:-

  1. Trace route

3)Ipconfig /if config:-

Ipconfig is a MS-DOS utility that can be used from MS-DOS and a MS-DOS shell to display the network settings currently assigned and given by a network. This command can be utilized to verify a network connection as well as to verify your network settings.

4)Net stat:-

The net stat command is used to display the TCP/IP network protocol statistics and information.

Syntax:-

netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts]

Example:-

Net stat

5)Telnet:-

Telnet connects to a machine at the specified ports it find out whether that machine server is working right.

Syntax:-

telnet hostname

Example:-

telnet tech.blogspot.com

6)ARP:-

Displays add and remove app interconnection from network devices.

Syntax:-

ARP –d inet –addr [if -addr]

Example:-

Arp –a

7)Hostname:-

Display the hostname of the machine. The command is being run on addr information about the terms host name can be found on our hostname dictionary definition.

Syntax:-

Hostname

Example:-

Hostname

8)FTP:-

To connect to ftp server simply run ftp command and specified the host.

Syntax:-

FTP hostname(port)

Command Purpose

Ls list files

Quit log off ftp server

Bin set binary transfer mode.

9)Wget:-

Wget used to download file from world wide web.

Syntax:-

Wget url-for-file

10)Nslookup:-

To x request a chat with another use this command.

To find all the IP address for given domain name.

Example:-

Nslookup

PRACTICAL-2

AIM: IMPLEMENTING CLINET /SERVER MODEL USING STREAM SOCKET.

SW/HW: - PC, Windows OS, Turbo C.

Theory/Commands:-

The steps involved in establishing a socket on theclientside are as follows:

  1. Create a socket with thesocket()system call
  2. Connect the socket to the address of the server using theconnect()system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use theread()andwrite()system calls.

The steps involved in establishing a socket on theserverside are as follows:

  1. Create a socket with thesocket()system call
  2. Bind the socket to an address using thebind()system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with thelisten()system call
  4. Accept a connection with theaccept()system call. This call typically blocks until a client connects with the server.
  5. Send and receive data

SERVER CODE:-

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <sys/wait.h>

#include <signal.h>

/* the port users will be connecting to */

#define MYPORT 3490

/* how many pending connections queue will hold */

#define BACKLOG 10

void sigchld_handler(int s)

{

while(wait(NULL) > 0);

}

int main(int argc, char *argv[ ])

{

/* listen on sock_fd, new connection on new_fd */

int sockfd, new_fd;

/* my address information */

struct sockaddr_in my_addr;

/* connector’s address information */

struct sockaddr_in their_addr;

int sin_size;

struct sigaction sa;

int yes = 1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

{

perror("Server-socket() error lol!");

exit(1);

}

else

printf("Server-socket() sockfd is OK...\n");

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)

{

perror("Server-setsockopt() error lol!");

exit(1);

}

else

printf("Server-setsockopt is OK...\n");

/* host byte order */

my_addr.sin_family = AF_INET;

/* short, network byte order */

my_addr.sin_port = htons(MYPORT);

/* automatically fill with my IP */

my_addr.sin_addr.s_addr = INADDR_ANY;

printf("Server-Using %s and port %d...\n", inet_ntoa(my_addr.sin_addr), MYPORT);

/* zero the rest of the struct */

memset(&(my_addr.sin_zero), '\0', 8);

if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)

{

perror("Server-bind() error");

exit(1);

}

else

printf("Server-bind() is OK...\n");

if(listen(sockfd, BACKLOG) == -1)

{

perror("Server-listen() error");

exit(1);

}

printf("Server-listen() is OK...Listening...\n");

/* clean all the dead processes */

sa.sa_handler = sigchld_handler;

sigemptyset(&sa.sa_mask);

sa.sa_flags = SA_RESTART;

if(sigaction(SIGCHLD, &sa, NULL) == -1)

{

perror("Server-sigaction() error");

exit(1);

}

else

printf("Server-sigaction() is OK...\n");

/* accept() loop */

while(1)

{

sin_size = sizeof(struct sockaddr_in);

if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)

{

perror("Server-accept() error");

continue;

}

else

printf("Server-accept() is OK...\n");

printf("Server-new socket, new_fd is OK...\n");

printf("Server: Got connection from %s\n", inet_ntoa(their_addr.sin_addr));

/* this is the child process */

if(!fork())

{

/* child doesn’t need the listener */

close(sockfd);

if(send(new_fd, "This is a test string from server!\n", 37, 0) == -1)

perror("Server-send() error lol!");

close(new_fd);

exit(0);

}

else

printf("Server-send is OK...!\n");

/* parent doesn’t need this*/

close(new_fd);

printf("Server-new socket, new_fd closed successfully...\n");

}

return 0;

}

OUTPUT:-

./serverprog

Server-socket() sockfd is OK...

Server-setsockopt() is OK...

Server-Using 0.0.0.0 and port 3490...

Server-bind() is OK...

Server-listen() is OK...Listening...

Server-sigaction() is OK...

Server-accept() is OK...

Server-new socket, new_fd is OK...

Server: Got connection from 203.106.93.94

Server-send() is OK...!

Server-new socket, new_fd closed successfully...

CLIENT CODE:-

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

// the port client will be connecting to

#define PORT 3490

// max number of bytes we can get at once

#define MAXDATASIZE 300

int main(int argc, char *argv[])

{

int sockfd, numbytes;

char buf[MAXDATASIZE];

struct hostent *he;

// connector’s address information

struct sockaddr_in their_addr;

// if no command line argument supplied

if(argc != 2)

{

fprintf(stderr, "Client-Usage: %s the_client_hostname\n", argv[0]);

exit(1);

}

if((he=gethostbyname(argv[1])) == NULL)

{

perror("gethostbyname()");

exit(1);

}

else

printf("Client-The remote host is: %s\n", argv[1]);

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

{

perror("socket()");

exit(1);

}

else

printf("Client-The socket() sockfd is OK...\n");

// host byte order

their_addr.sin_family = AF_INET;

// short, network byte order

printf("Server-Using %s and port %d...\n", argv[1], PORT);

their_addr.sin_port = htons(PORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

// zero the rest of the struct

memset(&(their_addr.sin_zero), '\0', 8);

if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)

{

perror("connect()");

exit(1);

}

else

printf("Client-The connect() is OK...\n");

if((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)

{

perror("recv()");

exit(1);

}

else

printf("Client-The recv() is OK...\n");

buf[numbytes] = '\0';

printf("Client-Received: %s", buf);

printf("Client-Closing sockfd\n");

close(sockfd);

return 0;

}

OUTPUT:-

./clientprog 203.106.93.94

...

[bodo@bakawali testsocket]$ ./clientprog bakawali

Client-The remote host is: bakawali

Client-The socket() sockfd is OK...

Server-Using bakawali and port 3490...

Client-The connect() is OK...

Client-The recv() is OK...

Client-Received: This is the test string from server!

Client-Closing sockfd

PRACTICAL-3

AIM: IMPLEMENTING CLINET /SERVER MODEL USING DATAGRAM SOCKET.

SW/HW: - PC, PC, Windows OS, Turbo C.

Theory/Commands:-

Datagram sockets are also called connectionless. They are unreliable. If you send a datagram, it may arrive. It may arrive out of order. If it arrives, the data within the packet will be error-free. You don't have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed. They are generally used for packet-by-packet transfers of information. Sample applications: tftp, bootp, etc.

Datagram sockets use a protocol called UDP (User Datagram Protocol)P is a connectionless,unreliable,datagram protocol. Some popular applications are built using UDP : DNS ,NFS and SNMP for example.

Unlike Stream sockets ,the client does not establish a connection with server. Instead, the client just sends a datagram to the server using the sendtofunction , which requires the address of the destination (the server) as a parameter. Similarly the server does not accept a connection from a client. Instead the server just calls the recvfrom function, which waits until data arrives from some client. recvfrom returns the protocol address of the client , along with the datagram, so the server can send a response to the correct client.

SERVER CODE:-

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

/* the port users will be connecting to */

#define MYPORT 4950

#define MAXBUFLEN 500

int main(int argc, char *argv[])

{

int sockfd;

/* my address information */

struct sockaddr_in my_addr;

/* connector’s address information */

struct sockaddr_in their_addr;

int addr_len, numbytes;

char buf[MAXBUFLEN];

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)

{

perror("Server-socket() sockfd error lol!");

exit(1);

}

else

printf("Server-socket() sockfd is OK...\n");

/* host byte order */

my_addr.sin_family = AF_INET;

/* short, network byte order */

my_addr.sin_port = htons(MYPORT);

/* automatically fill with my IP */

my_addr.sin_addr.s_addr = INADDR_ANY;

/* zero the rest of the struct */

memset(&(my_addr.sin_zero), '\0', 8);

if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)

{

perror("Server-bind() error lol!");

exit(1);

}

else

printf("Server-bind() is OK...\n");

addr_len = sizeof(struct sockaddr);

if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)

{

perror("Server-recvfrom() error lol!");

/*If something wrong, just exit lol...*/

exit(1);

}

else

{

printf("Server-Waiting and listening...\n");

printf("Server-recvfrom() is OK...\n");

}

printf("Server-Got packet from %s\n", inet_ntoa(their_addr.sin_addr));

printf("Server-Packet is %d bytes long\n", numbytes);

buf[numbytes] = '\0';

printf("Server-Packet contains \"%s\"\n", buf);

if(close(sockfd) != 0)

printf("Server-sockfd closing failed!\n");

else

printf("Server-sockfd successfully closed!\n");

return 0;

}

CLIENT CODE:-

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

/* the port users will be connecting to */

#define MYPORT 4950

int main(int argc, char *argv[ ])

{

int sockfd;

/* connector’s address information */

struct sockaddr_in their_addr;

struct hostent *he;

int numbytes;

if (argc != 3)

{

fprintf(stderr, "Client-Usage: %s <hostname> <message>\n", argv[0]);

exit(1);

}

/* get the host info */

if ((he = gethostbyname(argv[1])) == NULL)

{

perror("Client-gethostbyname() error lol!");

exit(1);

}

else

printf("Client-gethostname() is OK...\n");

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)

{

perror("Client-socket() error lol!");

exit(1);

}

else

printf("Client-socket() sockfd is OK...\n");

/* host byte order */

their_addr.sin_family = AF_INET;

/* short, network byte order */

printf("Using port: 4950\n");

their_addr.sin_port = htons(MYPORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

/* zero the rest of the struct */

memset(&(their_addr.sin_zero), '\0', 8);

if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)

{

perror("Client-sendto() error lol!");

exit(1);

}

else

printf("Client-sendto() is OK...\n");

printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));

if (close(sockfd) != 0)

printf("Client-sockfd closing is failed!\n");

else

printf("Client-sockfd successfully closed!\n");

return 0;

}

PRACTICAL-4

AIM: IMPLEMENTING SLIDING WINDOW PROTOCOL.

SW/HW:-PC, Windows OS, Turbo C.

Theory/Commands:-

In stop -and -wait ARQ , the sender sends a frame and simply waits for an acknowledgment from the receiver. In other words ,for a sender there is only one outstanding frame , that is sent and waiting to be acknowledged. This is not a good use of transmission medium. To improve the efficiency , multiple frames should be in transition while waiting for acknowledgment. In other words we need to let more than one frames be outstanding.

In Go-Back-N ARQ , we can send up to W frames without waiting for acknowledgments. We keep a copy of these frames until the acknowledgments arrive. This method is also called pipeline protocol. This procedure requires additional features to be added to stop-and-wait ARQ.

Sender’s window:

At the sender , to hold the outstanding frames until they are acknowledged, we use the concept of a window. At any instant , the frames that fall within the sender's window are assumed to be sent but not yet acknowledged.

In the following figure all the frames to the left of the window are those that have already been acknowledged and can be purged.

Those to the right of the window cannot be sent until the window slides over them. Those fall inside are permitted to send. We keep the sent frames in the

window until they are acknowledged. The window slides to include new unsent frames frames when correct acknowledge are received.

The size of the window is at most 2m-1.When ever the acknowledgment arrives the window slides by one.

The receiver's window:

The receiver also maintains the window. At any instant of time, the sequence numbers fall within the receiver's window can be accepted. Whenever a frame arrives at the receiver , its sequence number is checked against the window. If the sequence number

SERVER CODE:-

#include “stdio.h “
#include “sys/types.h “
#include “sys/socket.h “
#include “netinet/in.h “
#include “arpa/inet.h “
#include “string.h “
#include “stdlib.h “
#include “arpa/inet.h “
#include “unistd.h “
#define SIZE 4
main()
{
int id,lfd,i,j,status,n;
char str[20],frame[20],temp[20],ack[20];
socklen_t len;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id < sin_family="AF_INET;" sin_port="htons(8565);" s_addr="htonl(INADDR_ANY);" len="sizeof(&client);" lfd="accept(id,(struct" i="0;" n="strlen(frame);" j="0;j" status="="-1)" j="0;;)" 4="="0)" n="strlen(frame);" j="0;j" id="socket(AF_INET,SOCK_STREAM,0))="="-1)" sin_family="AF_INET;" sin_port="htons(8565);" c="connect(id,(struct"> y 0-- > n)",str);
scanf("%d",&choice);
if(!choice)
write(id,"-1",sizeof("-1"));
else
{
printf("Enter The Sequence Of Frame Where Error Has Occured: ");
scanf("%s",err);
write(id,err,sizeof(err));
read(id,str,20);
printf("\n\n Received The Retransmitted Frames %s\n\n",str);
}
}
close(id);
return 0;
}
OUTPUT:-

SERVER:
[NPLab@localhost ~]$ cc slides21.c
[NPLab@localhost ~]$ ./a.out
Enter The Text: HELLO
Transmitting Frames
0123
Transmission Is Successful
Transmitting Frames
4
Transmission Is Successful
EXITING
CLIENT:
[NPLab@localhost ~]$ cc slidec21.c
[NPLab@localhost ~]$ ./a.out
Received HELL0123
Want To Report An Error? (1-- > y 0-- > n)0
Received O4
Want To Report An Error? (1-- > y 0-- > n)0
EXITING

PRACTICAL-5

AIM: IMPLEMENTING DIJKSA’S SHORTEST PATH ROUTING ALGORITHM.

SW/HW: - PC, Windows OS, Turbo C.

Theory/Commands:-

Dijkstra's algorithm works on the principle that the shortest possible path from the source has to come from one of the shortest paths already discovered. A way to think about this is the "explorer" model--starting from the source, we can send out explorers each travelling at a constant speed and crossing each edge in time proportional to the weight of the edge being traversed. Whenever an explorer reaches a vertex, it checks to see if it was the first visitor to that vertex: if so, it marks down the path it took to get to that vertex. This explorer must have taken the shortest path possible to reach the vertex. Then it sends out explorers along each edge connecting the vertex to its neighbors.
It is useful for each vertex of the graph to store a "prev" pointer that stores the vertice from which the "explorer" came from. This is the vertex that directly precedes the current vertex on the path from the source to the current vertex.
The pseudocode for Dijkstra's algorithm is fairly simple and reveals a bit more about what extra information needs to be maintained. Vertices will be numbered starting from 0 to simplify the pseudocode.

SOURCE CODE:-

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<string.h>

#include<math.h>

#define IN 99

#define N 6

int dijkstra(int cost[][N], int source, int target);

int dijsktra(int cost[][N],int source,int target)

{

int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;

char path[N];

for(i=1;i< N;i++)

{

dist[i] = IN;

prev[i] = -1;

}

start = source;

selected[start]=1;

dist[start] = 0;

while(selected[target] ==0)

{

min = IN;

m = 0;

for(i=1;i< N;i++)

{

d = dist[start] +cost[start][i];

if(d< dist[i]&selected[i]==0)

{

dist[i] = d;

prev[i] = start;

}

if(min>dist[i] & selected[i]==0)

{

min = dist[i];

m = i;

}

}

start = m;

selected[start] = 1;

}

start = target;

j = 0;

while(start != -1)

{

path[j++] = start+65;

start = prev[start];

}

path[j]='\0';

strrev(path);

printf("%s", path);

return dist[target];

}

main()

{

int cost[N][N],i,j,w,ch,co;

int source, target,x,y;

printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");

for(i=1;i< N;i++)

for(j=1;j< N;j++)

cost[i][j] = IN;

for(x=1;x< N;x++)

{

for(y=x+1;y< N;y++)

{

printf("Enter the weight of the path between node %d and %d: ",x,y);

scanf("%d",&w);

cost [x][y] = cost[y][x] = w;

}

printf("\n");

}

printf("\nEnter The Source:");

scanf("%d", &source);

printf("\nEnter The target");

scanf("%d", &target);

co = dijsktra(cost,source,target);

printf("\nShortest Path: %d",co);

}

OUTPUT:-

PRACTICAL-6

AIM: IMPLEMENTING DISTANCE VECTOR ROUTING ALGORITHM.

SW/HW: -PC, Windows OS, Turbo C.

Theory/Commands:-