78
U6CSA19 NETWORKS LAB

Lab Manuel

Embed Size (px)

Citation preview

Page 1: Lab Manuel

U6CSA19 NETWORKS LAB

Page 2: Lab Manuel

U6CSA19 NETWORKS LAB

VI SEM ECE

LIST OF EXPERIMENTS

Sl.No EXPERIMENTS PAGE NO

1 1. Simulation of ARP / RARP.

2 Write a program that takes a binary file as input and performs bit stuffing and CRC Computation.

3 Develop an application for transferring files over RS232

4 Simulation of Sliding-Window protocol

5 Simulation of BGP / OSPF routing protocol

6 Develop a Client – Server application for chat

7 Develop a Client that contacts a given DNS Server to resolve a given host name

8 Write a Client to download a file from a HTTP Server

9&10 Study of Network Simulators like NS2/Glomosim / OPNET

Page 3: Lab Manuel

EX:NO:1

Simulation of ARP / RARP

Aim:

To write a c program to create a socket.

Software used:

PC 2-nos with Turbo c

Algorithm:

Server: 1. Start

2. Create a shared memory segment and get its id using shmget() ;

3. Create a shared memory painter and attach it to the shared memory using shmat().

4. A table containing machine address and its equivalent IP address is created and it is shared in the shared memory segment.

5. Display contents of shared memory using its pointer.

6. Detach pointer from the shared memory using shmdt().

7. Stop.

Client:

1. Start.

2. Create the shared memory pointer which refers to the same memory segment that server has created using shmget() .

3. Create options to find ARP, RARP and exit.

Page 4: Lab Manuel

4. If ARP is requested, then compare the given IP address in the shared memory segment and print the machine address.

5. If RARP is requested, then compare the given machine address in the ARP table and display the equivalent IP address.

6. Repeat step 4 and 5 depending upon the user's choice.

7. Exit.

PROGRAM:

ARP Server

#include<stdio.h>

#include<sys/types.h>

#include<sys/shm.h>

#include<string.h>

main()

{

int shmid, a, i;

char *ptr, *shmptr;

shmid=shmget(3000,10,IPC_CREAT | 0666);

shmptr=shmat(shmid,NULL,0);

ptr=shmptr;

for(i=0;i<3;i++)

{

puts("enter the mac");

Page 5: Lab Manuel

scanf("%s",ptr);

a=strlen(ptr);

printf("string length:%d",a);

ptr[a]= ' ' ;

puts("enter ip");

ptr=ptr+a+1;

scanf("%s",ptr);

ptr[a]='\n' ;

ptr= ptr+a+1;

}

ptr[strlen(ptr)]= '\0';

printf("\n ARP table at serverside is=\n%s", shmptr);

shmdt(shmptr);

}

ARP table at serverside is

a.b.c.d 1.2.3.4

e.f.g.h 5.6.7.8

i.j.k.l 9.1.2.3

ARP Client

Page 6: Lab Manuel

#include<stdio.h>

#include<string.h>

#include<sys/types.h>

#include<sys/shm.h>

main()

{

int shmid,a;

char *ptr, *shmptr;

char ptr2[51], ip[12], mac[26];

shmid=shmget(3000,10,0666);

shmptr=shmat(shmid,NULL,0);

puts("the arp table is");

printf("%s",shmptr);

printf("\n1.ARP\n 2.RARP\n 3.EXIT\n");

scanf("%d",&a);

switch(a)

{

case 1:

puts("enter ip address");

scanf("%s",ip);

ptr=strstr(shmptr, ip);

Page 7: Lab Manuel

ptr-=8;

sscanf(ptr,"%s%*s",ptr2);

printf("mac addr is %s",ptr2);

break;

case 2:

puts("enter mac addr");

scanf("%s",mac);

ptr=strstr(shmptr, mac);

sscanf(ptr,"%*s%s",ptr2);

printf("%s",ptr2);

break;

case 3:

exit(1);

}

}

Sample Input Output:

Page 8: Lab Manuel

The arp table isa.b.c.d  1.2.3.4e.f.g.h   5.6.7.8i.j.k.l     9.1.2.3

1.ARP2.RARP3.EXITenter your choice: 1enter ip address: 1.2.3.4mac addr is a.b.c.d

enter your choice:2enter mac address: e.f.g.hip addr is 5.6.7.8

Page 9: Lab Manuel

RESULT:

Thus the c program of ARP/RARP is verified and successfully.

Page 10: Lab Manuel

EX:NO:2

Bit stuffing and CRC Computation.

Aim: To write a c program to implement bit stuffing for the given bits of information and cyclic redundancy check.

Software used: PC 2-nos with Turbo c

Algorithm:

Bit stuffing

Step1: Start the program.

Step 2: Include all the header files.

Step 3: Declare two files pointers for opening the input file in read mode and the output in write mode.

Step 4: Read the content of an input file.

Step 5: If the bit is 1, then check for four consecutive 1,s.

Step 6: If so then stuff a bit 0 at that position (i.e. after five consecutive 1’s)

Step 7: open the output file in write mode and print the stuffed string.

Step 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<string.h>

main()

{

Page 11: Lab Manuel

int i=0,j=0,k,d;

char a[50];

do

{

printf("\n enter the choice 1.stuff 2.destuff 3.exit");

scanf("%d",&d);

switch(d)

{

case 1:

printf("enter the data..");

scanf("%s",a);

for(i=0;i<strlen(a);i++)

{

if(a[i]=='1')

j++;

else

j=0;

if(j==5)

{

for(k=strlen(a);k>i;k--)

{

Page 12: Lab Manuel

a[k+1]=a[k];

}

a[i+1]='0';

}

}

printf("%s",a);

break;

case 2:

printf("\n enter the data");

scanf("%s",a);

j=0;

for(i=0;i<strlen(a);i++)

{

if(a[i]=='1')

j++;

else

j=0;

if(j==5)

{

i=i+1;

for(k=i;k<strlen(a);k++)

Page 13: Lab Manuel

a[k]=a[k+1];

a[k]='\0';

i=i-1;

}

}

printf("\n the unstuffed data is %s",a);

break;

case 3:

break;

}

}

while(d<4);

}

Sample Input Output:

"bit.c"60L, 644C written[csea56@localhost csea56]$ cc bit.c[csea56@localhost csea56]$ ./a.out

Enter the choise 1.stuff 2.destuff 3.exit1Enter the data..1011111101010111111010Enter the choice 1.stuff 2.destuff 3.exit2Enter the data 10111111010

The unstuffed data is 10111111010enter the choice 1.stuff 2.destuff 3.exit

Page 14: Lab Manuel

CRC:

Algorithm:

Step 1: Declare int crc 16, SHIFT_CRC, shift Byte, Byte_SIZE as global variables.

Step 2: Input the data in a file.

Step 3: perform the crc computation using cal CRC 16 ().

Step 4: In cal CRC 16 each character from input shifted with shift-byte where value 987.

Step 5: Output of step 4 and step 5 are exclusive.

Step 6: store in a temporary variable.

Step 7: Byte value is now left_shifted by 1.

Step 8: The loop is repeated for the Byte_size.

Step 9: The computed crc is display as output in screen.

Program

#include<stdio.h>

int i.data[10].dl.gen[10],gl,temp[10],c;

void left_shift()

{

for(i=0;i<gl-1;i++)

Page 15: Lab Manuel

temp(i)=temp[i+1];

if(c<d1)

temp[gl-1]=data[c];

else

temp[gl-1]=0

}

void xor()

{

if(temp[0]==0

{

left_shift();

c++;

for(i=0;i<gl;i++)

{

if(gen(i)==temp(i)

temp(i)=0

else

temp[i]=1;

}

}

Page 16: Lab Manuel

main()

{

int j;

printf("1.generate2.check\nchoice:");

scanf("%d",&j);

printf("Enter the length of data:");

scanf("%d",&dl);

printf("Enter the data:");

for(i=0;i<dl;i++)

scanf("%d";&data[i]);

printf("Enter the length of generator:");

scanf("%d",&gl);

printf("Enter the generator:");

for(i=0;i<gl;i++)

temp[i]=data[i]

if(j==1)

{

for(c=4;c<dl+gl-2;c++)

{

xor();

Page 17: Lab Manuel

printf("crc:");

for(i=0;i<dl;i++)

printf"%d",data[i]);

}

else

{

for(c=4;c<dl;c++)

{

xor();

left_shift();

}

printf("crc");

}

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

printf("%d"'temp[i]);

printf("\n");

}

Sample Input Output:

[csea56@localhost cse156]$ cc crc.c[csea56@localhost cse156]$./a.out

Page 18: Lab Manuel

1.generate2.checkchoide:1Enter the length of data:6Enter the data:101101Enter the length of generator:4Enter the generator:1101crc:101101001

Page 19: Lab Manuel

Result:

Thus the c program for bit stuffing and CRC was verified and written successfully.

Page 20: Lab Manuel

EX:NO:3

Transferring Files over Rs232

Aim: To write a c program to transferring files over RS232

Software Used: PC 2-nos with Turbo c

Algorithm:

Server:

1. Start.

2. Open the file recvfile.txt in write mode.

3. Receive the port number and establish socket connection.

4. Listen to client’s request using system call listen() .

5. While the message size is not zero, receive the client message and write it in to a

file.

6. Close the socket connection,

7. Stop.

Client:

1. Start.

2. Pass server address and file name as command line arguments.

3. Open the file and establish connection using socket().

4. Using memset(), get the size of client address.

5. Connect to server using connect()system call.

6. While not end of file, print the contents into the file.

7. Close the socket connection.

8. Stop.

Page 21: Lab Manuel

Syntax:

1. socket()

socket(protocol family, type of socket, protocol);

2. connect()

connect(client socketid, client address, length of server address);

3. send()

send(int s, void $buf, size_tlen, int flags);

4. recv()

recv(int s, void $buf, size_tlen, int flags);

5. listen()

listen(server socket, protocol type for number of connection) ;

6. accept()

accept(socket id, client information, client size);

RS232 Server Program:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/socket.h>

#include<sys/types.h>

#include<arpa/inet.h>

#define MAX 5

#define BUF 256

int main(int argc, char **argv)

Page 22: Lab Manuel

{

int sersock, clisock, portno;

struct sockaddr_in echoseraddr,echocliaddr;

char echobuffer[BUF];

unsigned int clilen, recvmsgsize;

FILE *fp=fopen(“recvfile.txt”,”w”);

portno=atoi(argv[1]);

sersock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

memset(&echoseraddr,0,sizeof(&echoseraddr));

echoseraddr.sin_family=AF_INET;

echoseraddr.sin_addr.s_addr=htonl(INADDR_ANY);

echoseraddr.sin_port=htons(portno);

bind(sersock,(struct sockaddr*)&echoseraddr,sizeof(echoseraddr));

printf(“\nServer waiting for client on port %d”, portno);

listen(sersock,MAX);

clilen=sizeof(echocliaddr);

clisock=accept(sersock,(struct sockaddr *)&echocliaddr, &clilen);

recvmsgsize=1;

while(recvmsgsize>0)

Page 23: Lab Manuel

{

recvmsgsize=recv(clisock,echobuffer,BUF,0);

echobuffer[recvmsgsize]=’\0’;

fprintfp(fp,”%s”,echobuffer);

}

printf(“\nFile received and socket closed”);

close(clisock);

fclose(fp);

return 0;

}

RS232 Client Program:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<unistd.h>

#include<arpa/inet.h>

#define BUF 256

Page 24: Lab Manuel

int main(int argc, char **argv)

{

int sock, portno;

struct sockaddr_in echoseraddr;

char *serIP, *filename, echobuffer[32];

FILE *fp;

serIP=argv[1];

filename=argv[2];

portno=atoi(argv[3]);

fp=fopen(filename,”r”);

sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

memset(&echoseraddr,0,sizeof(&echoseraddr));

echoseraddr.sin_family=AF_INET;

echoseraddr.sin_addr.s_addr=inet_addr(serIP);

echoseraddr.sin_port=htons(portno);

connect(sock,(struct sockaddr *)&echoseraddr, sizeof(echoseraddr));

while(!feof(fp))

{

fgets(echobuffer,BUF,fp);

Page 25: Lab Manuel

send(sock,echobuffer,strlen(echobuffer),0);

}

printf(“\nThe %s send over RS232”,filename);

close(sock);

return 0;

}

Sample Input Output:

In Server Side:

cc rsser.c

./a.out 8222

Server waiting for client on port 8222

File received and socket closed

cat recvfile.txt

this is rs232 program

In Client Side:

cc rscli.c

./a.out 197.168.2.100 rsinput.txt 8222

The rsinput.txt send over RS232

cat > rsinput.txt

this is rs232 program

Page 26: Lab Manuel
Page 27: Lab Manuel

RESULT:

Thus the program for transferring files over RS232 was verified and written successfully.

Page 28: Lab Manuel

EX:NO:4

Sliding-Window protocol

Aim:

To implement the sliding window server and client using c program.

Software used:

PC 2-nos with Turbo c

Algorithm:

Step 1: start the program.

Step 2: Open the input file in read mode.

Step 3: Read the size of the window

Step 4: Select randomly the number of packets is to be transferred.

Step 5: Read the content of the input file.

Step 6: Transfer the packet until it reaches the maximum defined size.

Step 7: Resume the window size and repeat the above two steps until packets in.

Step 8: Close the file.

Step 9: Stop the program.

Program:

#include<stdio.h>

#include<stdlib.h>

main()

int i,m,n,j,w,1;

Page 29: Lab Manuel

char c;

FILE*f;

f=fopen("text.txt","r");

printf("window size");

scanf("%d",&n);

m=n;

while(!fof(f))

{

i=rand()%n+1;

j=i;

1=i;

if(m>i)

{

m=m-i;

if(m>0)

{

printf("\n");

while(i>0 & !feof(f))

{

Page 30: Lab Manuel

c=getc(f);

printf("%c",c);

i--;

}

printf("\n%d transferred"j);

if(j>3)

printf("\n 1 acknowledgement received");

else

printf("\n acknowledgement received",j+1);

}

}

m=m+j-1;

}

}

Sample Input and Output:

Page 31: Lab Manuel

[csea56host csea56] cc slide07.c

[csea56host csea56]./a.out

window size3

we

2 transferred

ack received

1

1 transferred

ack received

co

2 transferred

ack received

me

3 transferred

ack received

1 transferred

ack received

Page 32: Lab Manuel

Result:

Thus the c program of ARP/RARP is verified and successfully.

Page 33: Lab Manuel

EX:NO:5

Simulation of BGP / OSPF routing protocol.

Aim:

To write a program for open shortest path between server and destination Node.

Software Required:

PC 2 nos with Turbo C.

Algorithm:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the variable functions.

Step 4: Get the Number of nodes and the cost of the edge between various nodes.

Step 5: Enter the source and destination nodes.

Step 6: Find the shortest path between source and destination node.

Step 7: Stop the program execution.

Program:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class path

{ int a[10][10],c[10[10],key[10][10],num,min,i,j,k;

Page 34: Lab Manuel

public:

void findpath();

void read();

void output(int,int);

void out(int i, int j);

};

void path::read()

{ cout<<”Number of nodes:”;

cin>>num;

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

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

{ if(i==j)

a[i][j]=0;

else

{ cout<<”The cost for[“<<i<<”,”<<j<<”];

cin>>a[i][j];

} c[i][j]=a[i][j];

key[i][j]=0;

}}

void path::findpath()

{ int t1,t2,t3;

for(k=1;k<=num;k++)

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

Page 35: Lab Manuel

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

{ t1=c[i][k];

t2=c[k][j];

t3=c[i][j];

if(t1!=0 &&t2!=0 && (t3==0||t1+t2<t3))

{ c[i][j]=t1+t2;

key[i][j]=k;

}}}

void path::output(int i, int j)

{

min=0;

if(c[i][j]==0)

{ cout<<”no path exist”;

return;

} else

{ cout<<”The path is :”<<i;

out(i,j);

cout<<end1<<”cost is :”<<min;

cout<<”\n”;

}}

void path :: out(int i, int j)

{ if(i==j)

return;

Page 36: Lab Manuel

if(key[i][j]==0)

{ cout<<”->”<<j;

min+=a[i][j];

} else

{

out(i,key[i][j]);

out(key[i][j],j);

}}

void main()

{ clrscr();

int ch=1,n1,n2;

path p;

p.read();

p.findpath();

cout<<end1<<”1.Shortest path”<<end1;

cout<<”2.Exit”<<end1;

while(ch!=2)

{ cout<<end1<<”choice…..”;

cin>>ch;

switch(ch)

{ case 1:

cout<<”enter the source node”;

cin>>n1;

Page 37: Lab Manuel

cout<<”enter the destination node”;

cin>>n2;

p.output(n1,n2);

break;

case 2:

exit(0);

}}

getch();

}

Sample Output:

Number of nodes : 3

The cost for [1,2] : 1

The cost for [1,3] : 4

The cost for [2,1] : 2

The cost for [2,3] : 2

The cost for [3,1] : 2

The cost for [3,2] : 1

1.shortest path

2.exit

Choice……….. 1

Page 38: Lab Manuel

Enter the source code : 1

Enter the destination code : 3

The path is 1 -> 2 ->3

Cost is 3

Choice……….. 2

Page 39: Lab Manuel

Result:

Thus the program for the simulation of OSPF routing protocol was executed.

Page 40: Lab Manuel

EX:NO:6

Client – Server application for chat

Aim:

To write a program to develop a Client-Server application for chat using TCP.

Software Required:

PC 2-nos with Turbo C.

Algorithm:

Client:

1.Start the program

2. To create a socket in client to server.

3. The client establishes a connection to the server.

4. The client accept the connection and to send the data from client to server and vice versa

5. The client communicate the server to send the end of the message

6. Stop the program.

Server:

1. Start the program

2. To create a socket in server to client

3. The server establishes a connection to the client.

4. The server accept the connection and to send the data from server to client and vice Versa

5. The server communicate the client to send the end of the message

6. Stop the program.

Page 41: Lab Manuel

Program:

Client:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>

char buf[80];struct sockaddr myname;

void replyBack(FILE *fp, int sockfd) {char sendline[1000], recvline[1000];printf("Enter your echo: \n");while(fgets(sendline,1000,stdin) != NULL) {    write(sockfd,sendline,sizeof(sendline));    if(read(sockfd,recvline,1000) == 0) {        printf("str_cli: server terminated prematurely");        exit(-1);    }    fputs(recvline, stdout);}}

main() {int sock, adrlen, cnt;

sock = socket(AF_UNIX, SOCK_STREAM, 0);if(sock < 0) {    printf("client socket failure%d\n", errno);    printf("client: ");    exit(1);}

myname.sa_family = AF_UNIX;strcpy(myname.sa_data, "/tmp/billb");adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);

Page 42: Lab Manuel

if(connect(sock, &myname, adrlen) < 0) {    printf("client connect failure %d\n", errno);    perror("client: ");    exit(1);}

replyBack(stdin,sock);exit(0);}

Server:

#include <stdio.h>#include <errno.h>#include <signal.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>

struct sockaddr myname;char buf[80];

void echo(int sockfd) {ssize_t n;int write_err;char buf[1000];char * send_start_pos;while(1) {    bytes_in = read(sockfd, buf, 1000);    if(bytes_in < 1) {        if(errno == EINTR)            continue;        break;    }    bytes_remaining = bytes_in;    send_start_pos = buf;    write_err = 0;

Page 43: Lab Manuel

    while((bytes_remaining > 0) && !(write_err)) {        bytes_out = write(sockfd, send_start_pos,                           bytes_remaining);        if(bytes_out < 0) {            if(errno == EINTR)                continue;            write_err = 1;            break;        }    bytes_remaining -= bytes_out;    send_start_pos += bytes_out;    }    if(write_err)        break;}}

main() {int sock, new_sd, adrlen, cnt;

sock = socket(AF_UNIX, SOCK_STREAM, 0);if(sock < 0) {    printf("server socket failure %d\n", errno);    perror("server: ");    exit(1);}

myname.sa_family = AF_UNIX;strcpy(myname.sa_data, "/tmp/billb");adrlen = strlen(myname.sa_data) + sizeof(myname.sa_family);

unlink("/tmp/billb"); /*defensive programming */if(bind(sock, &myname, adrlen) < 0) {    printf("server bind failure%d\n", errno);    perror("server: ");    exit(1);}

if(listen(sock, 5) < 0) {

Page 44: Lab Manuel

    printf("server listen failure %d\n", errno);    perror("server: ");    exit(1);}

while(1) {    if(new_sd = accept(sock, &myname, &adrlen) < 0) {        printf("server accept failure %d\n", errno);        perror("server: ");        exit(1);    }

    printf("Socket address in server %d is %s, %s\n",         getpid(), myname.sa_data, myname.sa_data);

    if(fork() == 0) {        close(sock);        echo(new_sd);        exit(0);    }    close(new_sd);}}

Page 45: Lab Manuel

Result:

Thus the program for client-server application for chat was executed.

Page 46: Lab Manuel

EX:NO:7

DOMAIN NAME SYSTEM

Aim:

To perform DNS server and client program.

Software Required:

PC 2-nos with Turbo C

Algorithm:

Step 1: start the program excution.

Step 2: include header file,define max data size and port.

Step 3: enter the domain name.

Step 4: get the domain name.

Step 5: if it is not found then error on domain name.

Program:

DNS Server

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/socket.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<error.h>

Page 47: Lab Manuel

#include<netdb.h>

#define MAX 100

#define ipaddr"127.0.0.1"

#define port 4001

typedef struct sockaddr SA;

struct dns

{ char dname[25];

char ipaddr[25];

};

int main()

{ int cfd;

char buff[MAX];

struct socket_in server;

struct dns r;

cfd=socket(AF_INET<SOCK_STREAM,0);

bzero(&server,sizeof(server));

server.sin_family=AF_INET;

server.sin_port=htons(port);

inet_pton(AF_INET,ipaddr,&server.sin_addr);

connect((cfd,(SA*)&server ,sizeof(server));

printf("enter the domain name");

scanf("%s",r,dname);

write(cfd,&r,sizeof(struct dns));

Page 48: Lab Manuel

bzero(&r,sizeof(struct dns ));

read(cfd,&r,sizeof(struct dns));

printf("ipaddr = %s",r, ipaddr);

close(cfd);

}

DNS Client

#include<stdio.h>

#include<string.h>

#include<sys/socket.h>

#include<netinet/in.h>

int main()

{ int csd,cport,len;

char sendmsg[20],recvmsg[20],rc[20];

struct socket_in servaddr;

printf(“\n enter the port address”);

scanf(“%d”,&cport);

csd=socket(AF_INET<SOCK_STREAM,0);

if(csd<0)

printf(“ERROR:socket creation failed”);

servaddr.sin_family=AF_INET;

servaddr.sin_port=htons(port);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

Page 49: Lab Manuel

printf(“\n cannot connect”);

else

printf(“ \n connected”);

printf("enter the domain name");

scanf("%s",sendmsg);

sendmsg(csd,sendmsg,20,0);

recvmsg(csd,rc,20,0);

printf("\n the corresponding ip address for given domain name is:”);

printf(“%s”,rc);}

Output:

Dns server output:

dsp@dsp-desktop:~$ cc dnsser.c

dsp@dsp-desktop:~$ ./a.out

enter the domain name: www.annauniv.edu

ipaddr= 10.0.0.1

Dns client output:

dsp@dsp-desktop:~$ cc dnscli.c

dsp@dsp-desktop:~$ ./a.out

Page 50: Lab Manuel
Page 51: Lab Manuel

Result:

Thus the program for domain name system was verified and written successfully.

Page 52: Lab Manuel

EX:NO:8

Downloading a file from a HTTP Server

Aim:

To download a file from a HTTP Server.

Software Required:

PC Nos-2 with Turbo C

Algorithm:

1. Set the command line argument.

2. Check the command line arguments and allocate yes or no for the rest of argv[1]=’1’;

3. Copy ‘1’ info host and info integers.

4. Otherwise assign post to argv[1]

5. Print the host and the request.

6. Create a socket and connect the socket.

7. Print the download page into a buffer.

8. Open a file in a write mode, write the download page into the fill.

9. Close the socket and the file.

Program

#include<stdio.h>

#include<stdlib.h>

#include<netdb.h>

Page 53: Lab Manuel

#include<netinet/in.h>

#include<arpa/inet.h>

#include<sys/types.h>

#include<sys/socket.h>

#define size 100

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

{

struct sockaddr_in sock;

struct hostent *hp,port;

char *req,*hostname,*cp;

FILE *local;

char buff[size];

int con,i,l,nrv,sd;

if(argc!=3)

{

printf(“\nUsage: %s<server>filename”,argv[0]);

exit(1);

}

if(cp=strchr(argv[1],’/’))

{

*cp=’\0’;

l=strlen(argv[1]);

hostname=malloc(l+1);

Page 54: Lab Manuel

strcpy(hostname,argv[1]);

*cp=’/’;

l=strlen(cp);

req=malloc(l+1);

strcpy(req,cp);

}

else

{

hostname=argv[1];

req=”/”;

}

printf(“\nHost=%s\nReq= %s”,hostname,req);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

{

perror(“\nCannot open socket”);

exit(1);

}

bzero(&sock,sizeof(sock));

sock.sin_family=AF_INET;

con=inet_pton(AF_INET, argv[1], &sock);

sock.sin_port=htons(80);

con=connect(sd,(struct sockaddr *)&sock, sizeof(sock));

Page 55: Lab Manuel

if(con<0)

{

perror(“\nConnection failed”);

exit(1);

}

sprintf(buff,”Get HTTP:%s//1.1\r\nHost: %s\r\nConnection: class\r\n\r”, req, hostname);

printf(“Buff=%s\n”, buff);

l=strlen(buff);

local=fopen(argv[2],”w”);

write(sd,buff,1);

do

{

nrv=read(sd,buff,size);

if(nrv>0)

{

for(i=0;i<nrv;i++)

putc(buff[i],local);

}

else break;

}

while(1);

close(sd);

Page 56: Lab Manuel

fclose(local);

return 0;

}

Sample Input Output:

cc webpage.c./a.out http:/197.168.2.3/z :hi.txt hello.txtHost=http:Connection failed: Connection refusedReq= /197.168.2.3/z :hi.txt

.

Result:

Thus the program was executed for downloading a file from a HTTP Server.

Page 57: Lab Manuel

EX:NO:9&10

Study of Network Simulators like NS2/Glomosim / OPNET

Aim:

To study the network simulators like NS2/Glomosim/OPNET.

Theory:

A discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. Simulation of protocols (TCP, routing, multicast...) over networks (wireless, Wired, satellite). Ns began as a variant of the REAL network simulator in 1989 and have evolved substantially over the past few years. In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI. Currently ns development is support through DARPA with SAMAN and through NSF with CONSER, both in collaboration with other researchers including ACIRI. Ns have always included substantial contributions from other researchers, including wireless code from the UCB Daedelus and CMU Monarch projects and Sun Microsystems. For documentation on recent changes, see the version 2 change log. While we have considerable confidence in ns, ns is not a polished and finished product, but the result of an on-going effort of research and development. In particular, bugs in the software are still being discovered and corrected. Users of ns are responsible for verifying for themselves that their simulations are not invalidated by bugs. We are working to help the user with this by significantly expanding and automating the validation tests and demos. Similarly, users are responsible for verifying for themselves that their simulations are not invalidated because the model implemented in the simulator is not the model that they were expecting. The ongoing Ns Manual should help in this process. NS is Object Oriented Tcl (OTcl) script interpreter that has a simulation event scheduler and network component object libraries, and network setup module libraries. To run a simulation network, a user should write an OTcl script that initiates an event scheduler, sets up the network topology using the network objects and the plumbing functions in the library, and tells traffic sources when to start and stop transmitting packets through the event scheduler. Another major component of NS

Page 58: Lab Manuel

beside network objects is the event scheduler. An event is NS is a packet ID that is unique for a packet with scheduled time and the pointer to an object that handles the event. All the network components that need to spend some simulation time handling a packet (i.e. need a delay) use the event scheduler by issuing an event for the packet and waiting for the vent to be fired to itself before doing further action handling the packet. Another use of an event scheduler is timer. It can generally implemented in split language programming using C++ and TCL.

Split-Language Programming

Ns separate the data path implementation from control path implementations. In order to reduce packet and event processing time, the event scheduler and the basic network component objects in the data path are written and compiled using C++.

Implementation Of Node and Link

General, architecture of NS a user can be thought of standing at the left bottom corner, designing and running simulations in Tcl using the simulator objects in the OTcl library. The event scheduler and most of the network components are implemented in C++ and available to OTcl through an OTcl linkage that is implemented using tclcl. When a simulation is finished, NS produces one or more text-based output that files that contain detailed simulation data. The data can be used for simulation analysis or as and input to a graphical simulation display too called Network Animator (NAM) that is developed as a part of VINT project.

Page 59: Lab Manuel

OPNET

Page 60: Lab Manuel

A common presentation of the OPNET simulator (OPNET Modeler) is Provided . OPNET is very large and powerful software with wide variety of possibilities . Enables the possibility to simulate entire heterogeneous networks with various protocols . Development work was started in 1986 by MIL3 Inc. (nowadays OPNET Technologies Inc.) . Originally the software was developed for the needs of military, but it has grown to be a world leading commercial network simulation tool . OPNET is quite expensive for commercial usage but there are also free licenses for educational purposes.

OPNET is a high level event based network level simulation tool . Simulation operates at “packet-level” .Originally built for the simulation of fixed networks . OPNET contains a huge library of accurate models of commercially available fixed network hardware and protocols . Nowadays, the possibilities for wireless network simulations are also very Wide . Accurate radio transmission pipeline stage for the modeling of the physical layer (radio interface) . The simulator has a lot of potentiality, but there exists typically a lack of the recent wireless systems . Much of the work considering new technologies must be done by Oneself .OPNET can be used as a research tool or as a network design/analysis tool (end user). The threshold for the usage is high for the developer, but low for the end user.

The structure of OPNET

OPNET consists of high level user interface, which is constructed from C and C++ source code blocks with a huge library of OPNET specific functions • Hierarchical structure, modeling is divided to three main domains:

• Network domain

• Networks + sub-networks, network topologies, geographical coordinates, mobility

• Node domain

• Single network nodes (e.g., routers, workstations, mobile devices…)

Page 61: Lab Manuel

• Process domain

• Single modules and source code inside network nodes (e.g., data traffic source model)

With OPNET it is also possible to run external code components (External System Domain, ESD)

The Various Tools of OPNET

• Source code editing environment

• Network model editor

• Node model editor

• Process model editor

• Antenna pattern editor

• Modulation curve editor (SNR – BER behavior)

• Packet format editor

• Analysis configuration tool

• Simulation tool

• ICI editor (Interface Control Information)

• Probe model tool (organization of result collection)

• Link model editor (properties of fixed link models)

• Path model editor (for routing and modeling virtual circuits)

• Demand model editor (wide scale application modeling)

• OPNET Animation viewer

GloMoSim

Page 62: Lab Manuel

Global Mobile Information System Simulator (GloMoSim) is a scalable simulation environment for large wireless and wireline communication networks [1]. GloMoSim uses a parallel discrete-event simulation capability provided by Parsec. GloMoSim simulates networks with up to thousand nodes linked by a heterogeneous communications capability that includes multicast, asymmetric communications using direct satellite broadcasts, multi-hop wireless communications using ad-hoc networking, and traditional Internet protocols. The following table lists the GloMoSim models currently available at each of the major layers:

VTT TECHNICAL RESEARCH CENTRE OF FINLAND

The node aggregation technique is introduced into GloMoSim to give signi¯cant bene¯ts to the simulation performance. Initializing each node as a separate entity inherently limits the the scalability because the memory requirements increase dramatically for a model with large number of nodes. With node aggregation, a single entity can simulate several network nodes in the system. Node aggregation technique implies that the number of nodes in the system can be increased while maintaining the same number of entities in the simulation. In GloMoSim, each entity represents a geographical area of the simulation. Hence the network nodes which a particular entity represents are determined by the physical position of the nodes.

Page 63: Lab Manuel

Use of GloMoSim Simulator

After successfully installing GloMoSim, a simulation can be started by executing the following command in the BIN subdirectory.

./glomosim < inputfile >

The Visualization Tool

GloMoSim has a Visualization Tool that is platform independent because it is coded in Java. To initialize the Visualization Tool, we must execute from the java gui directory the following: java GlomoMain. This tool allows to debug and verify models and scenarios; stop, resume and step execution; show packet transmissions, show mobility groups in di®erent colors and show statistics. The radio layer is displayed in the Visualization Tool as follows: When a node transmits a packet, a yellow link is drawn from this node to all nodes within it's power range. As each node receives the packet, the link is erased and a green line is drawn for successful reception and a red line is drawn for unsuccessful reception. No distinction is made between di®erent packet types (ie: control packets vs. regular pacekts, etc)

Basic Configuration File (Config.in)

The main con¯guration parameters for setting up an scenario are de¯ned in the CONFIG.IN file. These parameters are the following:

The default values of these parameters in the CONFIG.IN file are:

SIMULATION-TIME 15M

Page 64: Lab Manuel

SEED 1

TERRAIN-DIMENSIONS (2000, 2000)

NUMBER-OF-NODES 30

The NODE-PLACEMENT parameter can be assigned the following values: RANDOM (nodes are placed randomly within the physical terrain), UNIFORM (based on the number of nodes in the simulation, the physical terrain is divided into a number of cells. Within each cell, a node is placed randomly), GRID (Node placement starts at 0,0 and are placed in grid format with each node GRID-UNIT away from its neighbors, the number of nodes has to be square of an integer) and FILE (Position of nodes is read from NODE- PLACEMENT-FILE). The default values of these parameters in the CONFIG.IN file and an example of the NODES.INPUT file are:

# NODE-PLACEMENT FILE

# NODE-PLACEMENT-FILE ./nodes.input

# NODE-PLACEMENT GRID

# GRID-UNIT 30

# NODE-PLACEMENT RANDOM

NODE-PLACEMENT UNIFORM

# Example of the NODES.IN file

# Format: nodeAddr 0 (x, y, z)

# The second parameter is for consistency with the mobility trace format

0 0 (20.2, 0.9, 0.11)

Page 65: Lab Manuel

1 0 (20.3, 30.8, 0.01)

2 0 (20.4, 60.7, 0.12)

If the MOBILITY parameter is set to NONE then there is no movement of nodes in the model.

Result:

Thus the network simulators such as NS2/Glomosim / OPNET were studied.

Page 66: Lab Manuel