0% found this document useful (0 votes)
196 views34 pages

CN and PW

The document contains code for 6 programs related to computer networks and programming concepts: 1) A CRC error detection program using CRC-16. 2) A frame sorting program that simulates packet transmission over a network. 3) A distance vector routing algorithm program to find optimal paths between nodes. 4) A client-server program using TCP sockets where the client requests a file and the server sends it if present. 5) The client-server program from #4 modified to use message queues as IPC channels instead of sockets. 6) A placeholder for a 6th program.

Uploaded by

Roshan Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views34 pages

CN and PW

The document contains code for 6 programs related to computer networks and programming concepts: 1) A CRC error detection program using CRC-16. 2) A frame sorting program that simulates packet transmission over a network. 3) A distance vector routing algorithm program to find optimal paths between nodes. 4) A client-server program using TCP sockets where the client requests a file and the server sends it if present. 5) The client-server program from #4 modified to use message queues as IPC channels instead of sockets. 6) A placeholder for a 6th program.

Uploaded by

Roshan Thomas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

7th semester CN and PW programs Harish Sethumadhavan

COMPUTER NETWORKS PROGRAMS Program 1:


Write a program for error detecting code using CRC-CCITT (16- bits). //CRC 16 bit program #include<stdio.h> #include<string.h> char gp[]="11000000000000101"; //char gp[]="10010"; int crc(char *input,char *output,char *gp,int mode) { int j,k; strcpy(output,input); if(mode) { for(j=1; j<strlen(gp); j++) strcat(output,"0"); } for(j=0; j<strlen(input); j++) if(*(output+j) == '1') for(k=0; k<strlen(gp); k++) { if (((*(output+j+k) =='0') && (gp[k] == '0') || (*(output+j+k) == '1') && (gp[k] == '1'))) *(output+j+k)='0'; else *(output+j+k)='1'; } for(j=0; j<strlen(output); j++) if(output[j] == '1') return 1; return 0; } int main() { system("clear"); printf(" CRC-16 \n"); char input[50],output[50]; char recv[50]; printf("Enter the input message in binary:\n"); scanf("%s",input); crc(input,output,gp,1); printf("The transmitted message is: %s%s\n",input,output+strlen (input)); printf("Enter the recevied message in binary: \n"); scanf("%s",recv); if(!crc(recv,output,gp,0)) printf("\n No error in data\n"); else printf("\n Error in data transmission has occurred\n"); }

1| Page

7th semester CN and PW programs Harish Sethumadhavan

Program 2:
Write a program for frame sorting technique used in buffers. //Frame sorting #include <stdio.h> #include <string.h> #define fsize 3 #define pno 15 //Structure of a packet typedef struct packet { char data[fsize+1]; int seq_num; }packet; //Array of packets and a message of max. length = no. of packets*max. frame size of 1 packet packet send[pno],rec[pno]; char msg[pno*fsize]; //Max. no. of frames generated int max_frames; //UDF void make_frames(); void shuffle_frames(); void receive_frames(); void sort_frames(); int main() { system("clear"); printf(" PROGRAM\n"); printf("Enter the message:"); gets(msg); //Make frames make_frames(); //Shuffle shuffle_frames(); //Receive frames receive_frames();

FRAME SORTING

//Used to sort the frames void sort_frames()

2| Page

7th semester CN and PW programs Harish Sethumadhavan


{ int min,i,j; packet temp; for(i=0;i<max_frames-1;i++) { min=i; for(j=i+1;j<max_frames;j++) if( rec[j].seq_num < rec[min].seq_num) min=j; //Swap the min th and i th packet temp.seq_num=rec[min].seq_num; strcpy(temp.data,rec[min].data); rec[min].seq_num=rec[i].seq_num; strcpy(rec[min].data,rec[i].data); rec[i].seq_num=temp.seq_num; strcpy(rec[i].data,temp.data); } }

//Make frames and simulate sending data void make_frames() { int i=0,k=0; //k -> seq. no. of the packet and i -> the position of string ptr in msg //modifying the base ptr of tmsg to use strcpy while( i<strlen(msg) ) { //copy the msg. to frame strcpy(send[k].data,&msg[i]); send[k].data[fsize]='\0'; //set the seq. no. send[k].seq_num=++k; //increment base ptr through i i+=fsize;

} max_frames=k;

printf("Making frames....\n"); //Print the frames created printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",send[i].seq_num,send[i].data); }

Data\n"); ---------\n");

void shuffle_frames() { //t -> rand no. gen and k -> total no. of rand nos. gen so far

3| Page

7th semester CN and PW programs Harish Sethumadhavan


int t,k=0,i; //temp. array to ensure that rand() doesn't gen same number twice int t_rand[max_frames+1]; for(i=0;i<max_frames;i++) t_rand[i]=0; while(k<max_frames) { t=rand()%max_frames; if( t_rand[ t ] == 0 ) { t_rand[t]=1 ; //Store the t 'th' packet from send to receive strcpy(rec[k].data,send[t].data); rec[k].seq_num=send[t].seq_num; k++; } } } void receive_frames() { //Print the frames received int i; printf("Receiving frames....\n"); //Print the frames created printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",rec[i].seq_num,rec[i].data); printf("Sorting...\n"); sort_frames(); printf("Sorted packets:\n"); printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",rec[i].seq_num,rec[i].data); } Data\n"); ---------\n");

Data\n"); ---------\n");

Program 3:
4| Page

7th semester CN and PW programs Harish Sethumadhavan


Write a program for distance vector algorithm to find suitable path for Transmission. //:dv.c #include<stdio.h> #include<string.h> struct node { int dist[20]; int dest[20]; int nexthop[20]; }rt[10]; int main() { system("clear"); int dmat[20][20],i,j,k,choice=1; int n=i=j=k=0,count=0; printf(" DISTANCE VECTOR ALGORITHM\nEnter The Number of Nodes\n"); scanf("%d",&n); printf("Enter The Cost Matrix(999=infinity):\n"); for(i=0 ;i<n;i++) for(j=0;j<n;j++) { scanf("%d",&dmat[i][j]); if(i==j)dmat[i][i]=0; rt[i].dist[j]=dmat[i][j]; rt[i].dest[j]=j; rt[i].nexthop[j]=j; } do { count=0; for(i=0 ;i<n;i++) for(j=0;j<n;j++) for(k=0;k<n; k++) if(rt[i] .dist[j]> dmat[i][k]+rt[k] .dist[j]) { rt[i] .dist[j]=rt[i] .dist[k]+rt[k] .dist[j]; rt[i].nexthop[j]=k; count++; } }while (count!=0); for(i=0;i<n;i++) { printf("State Value For Router %d Is\n",i+1); printf("\t\tDestination | Next Hop | Distance \n"); for(j=0;j<n;j++) { printf("\t\t %d %d %d", rt[i].dest[j] +1,rt[i].nexthop[j]+1,rt[i].dist[j]); if(rt[i].dist[j]==999)printf("(infinity)"); printf("\n"); }

5| Page

7th semester CN and PW programs Harish Sethumadhavan


} int source,dest,dist; printf("Enter a positive number to traverse between routers:"); scanf("%d",&choice); while(choice) { system("clear"); printf("Enter source and destination:"); scanf("%d%d",&source,&dest); source--; dest--; if( rt[source].dist[dest] == 999 ) printf("No link available!!!\n"); else { dist=rt[source].dist[dest]; printf("%d -> %d",source+1,rt[source].nexthop[dest]+1); source=rt[source].nexthop[dest]; while(source!=dest) { printf(" -> %d",rt[source].nexthop[dest]+1); source=rt[source].nexthop[dest]; } printf("\nTotal distance = %d \n",dist); } printf("Enter positive number to traverse between routers:"); scanf("%d",&choice); } }

Program 4:
Using TCP/IP sockets, write a client server program to make the client send the file name and to make the server send back the contents of the requested file if present.

SERVER
#include<sys/types.h> #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h>

#define SERV_TCP_PORT 65323

6| Page

7th semester CN and PW programs Harish Sethumadhavan


int main() { int pid,sockfd,bytes,newsockfd,nbytes,clilen,fd; struct sockaddr_in cli_addr,serv_addr; char buffer[100];

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("socket error");

bzero((char *)&serv_addr,sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); serv_addr.sin_port=htons(SERV_TCP_PORT);

if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0) printf("bind error");

listen(sockfd,5);

for(; ;) { clilen=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);

if(newsockfd<0) printf("server accept error");

if((pid=fork())<0) printf("error");

7| Page

7th semester CN and PW programs Harish Sethumadhavan

else if(pid==0) { close(sockfd); bytes=recv(newsockfd,&buffer,50,0); printf("%s",buffer); fd=open(buffer,O_RDONLY); if(fd==-1) { printf("\nerror"); strcpy(buffer,"file does not exist"); printf("\n%s %d\n",buffer,strlen(buffer)); send(newsockfd,buffer,strlen(buffer)+1,0); } else

while((nbytes=read(fd,buffer,sizeof(buffer)))>0) { buffer[nbytes]='\0'; send(newsockfd,buffer,nbytes+1,0);

//printf("\n\t%s",buffer); }

exit(0); } close(newsockfd); }

8| Page

7th semester CN and PW programs Harish Sethumadhavan

CLIENT

#include<sys/types.h> #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h>

#define SERV_TCP_PORT 65323 #define SERV_HOST_ADDR "127.0.0.1" int main() { int sockfd,newsockfd,clilen,bytes,i; struct sockaddr_in serv_addr; char buffer[100],name;

bzero((char *)&serv_addr,sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR); serv_addr.sin_port=htons(SERV_TCP_PORT);

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("socket error");

if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0) printf("cli connect error");

9| Page

7th semester CN and PW programs Harish Sethumadhavan


printf("enter the filename\n"); scanf("%s",buffer); send(sockfd,buffer,sizeof(buffer),0); while((bytes=recv(sockfd,buffer,sizeof(buffer),0))>0) { write(1,buffer,bytes); } }

Program 5:
Implement the above program using as message queues or FIFOs as IPC channels. CLIENT #include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { int readfd,writefd; ssize_t n; char buff[512]; printf("Trying to connect to server....\n"); writefd=open(FIFO1,O_WRONLY,0); readfd=open(FIFO2,O_RDONLY,0); printf("Connected.....\n"); printf("Enter the filename to request from server:"); scanf("%s",fname); write(writefd,fname,strlen(fname)); printf("waiting for server to reply......\n"); while((n=read(readfd,buff,512))>0) write(1,buff,n); close(readfd); close(writefd); return 0; }

10 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


SERVER #include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { int readfd,writefd,fd; ssize_t n; char buff[512]; if(mkfifo(FIFO1,PERMS)<0) printf("cant create FIFO files\n"); if(mkfifo(FIFO2,PERMS)<0) printf("Cant create FIFO files\n"); printf("Waiting for connection request......\n"); readfd=open(FIFO1,O_RDONLY,0); writefd=open(FIFO2,O_WRONLY,0); printf("Connection Established......\n"); read(readfd,fname,255); printf("client has requested files %s \n ",fname); if((fd=open(fname,O_RDWR))<0) { strcpy(buff,"FILE DOES NOT EXIST....\n"); write(writefd,buff,strlen(buff)); } else { while((n=read(fd,buff,512))>0) write(writefd,buff,n); } close(readfd); unlink(FIFO1); close(writefd); unlink(FIFO2); }

Program 6:
Write a program for simple RSA algorithm to encrypt and decrypt the data. //RSA algorithm #include<math.h> #include<stdlib.h> #include<stdio.h>

11 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


int gcd(long m,long n) { long r; while(n!=0) { r=m%n; m=n; n=r; } return m; } int mult(unsigned int x,unsigned int y,unsigned int n) { unsigned long int k=1; int j; for(j=1;j<=y;j++) k=(k*x)%n; return (unsigned int)k; } int main() { char msg[100]; unsigned long p=0,q=0,n=0,e=0,d=0,phi=0; unsigned long nummes[100]={1}; unsigned long encrypted[100]={1},decrypted[100]={1}; unsigned long i=0,j=0;long nofelem=0; system("clear"); printf("\n RSA ALGORITHM\nEnter The Message To Be Encrypted:\n"); gets(msg); printf("\nEnter value of p and q:(give p=13 and q=17)\n"); scanf("%ld%ld",&p,&q); n=p*q; phi=(p-1)*(q-1); for(i=2;i<(phi/2);i++) if(gcd(i,phi)==1) break; e=i; for(i=2;i<phi;i++) if((e*i-1)%phi==0)break; d=i; nofelem=strlen(msg); for(i=0;i<nofelem;i++) nummes[i]=(int)msg[i]; printf("e:%ld,d:%ld\n",e,d); for(i=0; i<nofelem;i++) { encrypted[i] =mult(nummes[i],e,n); } printf("\n Encrypted message\n");

12 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


for(i=0;i<nofelem;i++) printf("%ld,",encrypted[i]); for(i=0; i<nofelem;i++) { decrypted[i]=mult(encrypted[i],d,n); } printf("\n Decrypted message\n"); for(i=0;i<nofelem;i++) printf("%c",(char)decrypted[i]); printf("\n"); return 0; }

Program 7:
Write a program for Hamming code generation for error detection and correction. #include<iostream> #include<stdlib.h> using namespace std; /*G matrix 1 0 0 0 | 1 1 0 0 1 0 0 | 1 0 1 0 0 1 0 | 0 1 1 0 0 0 1 | 1 1 1 H 1 1 0 matrix 1 0 1 | 1 0 0 0 1 1 | 0 1 0 1 1 1 | 0 0 1*/

char data[5]; int encoded[8], edata[7], syndrome[3]; int hmatrix[3][7]= {1,1,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0,1}; char gmatrix[4][8]={ "1000110", "0100101", "0010011", "0001111"}; int main() { int i,j; system("clear"); cout<<"Hamming Code --- Encoding\n"; cout<<"Enter 4 bit data : "; cin>>data; cout<<"Generator Matrix\n"; for(i=0;i<4;i++) cout<<"\t"<<gmatrix[i]<<"\n"; cout<<"Encoded Data : "; for(i=0;i<7;i++) { for(j=0;j<4;j++) encoded[i]+=((data[j]- '0')*(gmatrix[j][i]- '0')); encoded[i]=encoded[i]%2;

13 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


cout<<encoded[i]<<" "; } cout<<"\nHamming code --- Decoding\n"; cout<<"Enter Encoded bits as received : "; for(i=0;i<7;i++)cin>>edata[i]; for(i=0;i<3;i++) { for(j=0;j<7;j++) syndrome[i]=syndrome[i]+(edata[j]*hmatrix[i][j]); syndrome[i]=syndrome[i]%2; } for(j=0;j<7;j++) if ((syndrome[0]==hmatrix[0][j])&&(syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j])) break; if(j==7) cout<<"Data is error free!!\n"; else { cout<<"Error received at bit number "<<(j+1)<<" of the data\n"; edata[j]=!edata[j]; cout<<"The Correct data Should be : "; for(i=0;i<7;i++) cout<<edata[i]<<" "; } }

Program 8:
Write a program for congestion control using leaky bucket algorithm. //Program-Name ; Congestion.c #include<stdio.h> #include<string.h> int min(int x,int y) { if(x<y) return x; return y; } int main() { system("clear"); int drop=0,mini,nsec,cap,count=0,i,inp[25],process; printf(" LEAKY BUCKET\nEnter The Bucket Size\n"); scanf("%d",&cap); printf("Enter The Processing Rate\n"); scanf("%d", &process); printf("Enter The No. Of Seconds You Want To Stimulate\n"); scanf("%d",&nsec); for(i=0;i<nsec;i++) { printf(" Enter The Size Of The Packet Entering At %d sec\n",i+1); scanf("%d",&inp[i]); }

14 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


printf(" \nSecond | Packet Received | Packet Sent | Packet Left | Packet Dropped \n"); for(i=0;i<nsec;i++) { count+=inp[i]; if(count>cap) { drop=count-cap; count=cap; } printf("%d",i+1); printf("\t%d",inp[i]); mini=min(count,process); printf("\t\t%d",mini); count=count-mini; printf("\t\t%d",count); printf("\t\t%d\n",drop); drop=0; } for(;count!=0;i++) { if(count>cap) { drop=count-cap; count=cap; } printf("%d",i+1); printf("\t0"); mini=min(count,process); printf("\t\t%d",mini); count=count-mini; printf("\t\t%d",count); printf("\t\t%d\n",drop); } return 0; }

WEB PROGRAMS Program 1:


Develop and demonstrate a XHTML document that illustrates the use external style sheet, ordered list, table, borders, padding, color, and the <span> tag.

Css
p,table,li, {

15 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


font-family: "lucida calligraphy", arial, 'sans serif'; margin-left: 10pt; } p { word-spacing: 5px; } body { background-color:rgb(200,255,205); } p,li,td { font-size: 75%;} td { padding: 0.5cm; } th { text-align:center; font-size: 85%; } h1, h2, h3, hr {color:#483d8b;} table { border-style: outset; background-color: rgb(100,255,105); } li {list-style-type: lower-roman;} span { color:blue; background-color:pink; font-size: 29pt; font-style: italic; font-weight: bold; }

HTML
<?xml version = "1.0" encoding = "utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <!-- lab1.html --> <link rel="stylesheet" type="text/css" href="mystyle.css" /> <title> Lab program1 </title> </head> <body> <h1>This header is 36 pt</h1> <h2>This header is blue</h2> <p>This paragraph has a left margin of 50 pixels</p> <table border="4" width="5%"> <!-- table with name & email --> <tr> <th width="204">Name </th> <th>Email</th>

16 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


</tr> <tr> <td width="204">Dr. HNS</td> <td>[email protected]</td> </tr> <tr> <td width="204">Dr. MKV</td> <td>[email protected]</td> </tr> <tr> <td width="204">Dr. GTR</td> <td>[email protected]</td> </tr> <tr> <td width="204">Dr. MVS</td> <td>[email protected]</td> </tr> </table> <hr> <!-- horizontal line --> <ol> <!-- ordered list --> <li> TSB Singh</li> <li> Prakash S </li> <li> manojKumar</li> </ol> <p> <span>This is a text.</span> This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. <span>This is a text.</span> </p> </body> </html>

Program 2:
Develop and demonstrate a XHTML file that includes Javascript script for the following problems: a) Input: A number n obtained using prompt Output: The first n Fibonacci numbers b) Input: A number n obtained using prompt Output: A table of numbers from 1 to n and their squares using alert 2a <?xml version="1.0" ?> <html> <script type="text/javascript"> <!-var n; n=prompt("Enter n th term of Fibonacci series"); var a=0,b=1,c=1,i; document.write("The Fibonacci series are:<br/>"); if(n>1)

17 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


{ document.write(a+"<br/>"+b+"<br/>"); for(i=3;i<=n;i++) { c=a+b; a=b; b=c; document.write(c+"<br/>"); } } else if(n==1) document.write(a); else if(n==2) document.write(a+"</br>"+b); else alert("Invalid input"); //--> </script> </html>

2b
<?xml version="1.0" ?> <html> <script type="text/javascript"> <!-var n=0,i,t=null; n=prompt("Enter the n th value whose square is to be displayed:"); if(n>=1) { for(i=1;i<=n;i++) t=t+i+" - "+i*i+"\n "; alert(t); } else alert("Enter a value >= 1"); //--> </script> </html>

Program 3
Develop and demonstrate a XHTML file that includes Javascript script that uses functions for the following problems: a) Parameter: A string Output: The position in the string of the left-most vowel b) Parameter: A number Output: The number with its digits in the reverse order 3a <html>

18 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


<head> <title>Vowel position</title> </head> <body> <h2>Vowel Position</h2> <script type="text/javascript"> var pos=0; function func(str){ pos=str.value.search(/[aeiouAEIOU]/); if(pos>=0) alert("Position is:"+(pos+1)); else alert("No vowels found!"); } </script> <form method=get> <label>Enter text:<input type="text" name="str"/></label> <input type="button" value="Check" onclick="func(str);"/> </form> </body> </html>

3b
<?xml version = "1.0" encoding = "utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- lab3b.html --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head><title>Reverse of a number</title> <script type="text/javascript"> function disp(num) { var alphaExp = /^[0-9]+$/; if(!num.value.match(alphaExp)) { alert("Input should be positive numeric"); return false; } var rn=0, n= Number(num.value); while(n!=0) { r = n%10; n = Math.floor(n/10); rn = rn*10 + r; } alert("The " + num.value + " in reverse is " + rn); } </script> </head>

19 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


<body> <h2>Reverse of a number</h2> <form method=get><p> <label>Enter a number : <input type=text name=number></label> <input type="button" value="Check" onclick="disp(number)" ></p> </form> </body> </html>

Program 4
a) Develop and demonstrate, using Javascript script, a XHTML document that collects the USN ( the valid format is: A digit from 1 to 4 followed by two upper-case characters followed by two digits followed by two upper-case characters followed by three digits; no embedded spaces allowed) of the user. Event handler must be included for the form element that collects this information to validate the input. Messages in the alert windows must be produced when errors are detected. b) Modify the above program to get the current semester also (restricted to be a number from 1 to 8) 4a <html> <head>

<title> 4a program </title> <script type="text/javascript"> function isCorrect(usn) { var pos=usn.value.search(/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}\d{3}$/); if(pos!=0 || usn.value.length==0){ usn.focus(); usn.select(); return false; } else { return true; } } function checkUSN() { var usn=document.getElementById("USN"); if(isCorrect(usn)){ alert("Valid USN :)"); return true; } else{ alert("The USN is invalid!"); return false;

20 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


} } </script> </head> <body> <form name="USN_form" onsubmit="return checkUSN();"> <label> USN : <input type="text" id="USN"/> </label> <br /> <br /> <pre> <input type="submit" value="Submit"/> </pre> </form> </body> </html>

4b
<html> <head> <title> 4b program </title> <script type="text/javascript"> function isCorrect(usn) { var pos=usn.value.search(/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}\d{3}$/); if(pos!=0 || usn.value.length==0){ usn.focus(); usn.select(); alert("Invalid USN! "); return false; } else return true; } function isPerfect(sem) { if(sem.value.length==0 || sem.value<1 || sem.value>8){ sem.select(); alert("Invalid Semester! "); return false; } else if(sem.value.search(/^[1-8]$/)==0) return true;

} function checkUSN() { var usn=document.getElementById("USN"); var sem=document.getElementById("sem"); if(isCorrect(usn) && isPerfect(sem)){ alert("Valid inputs :)"); return true;

21 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


} return false;

} </script> </head> <body>

<form name="USN_form" onsubmit="return checkUSN();"> <label> USN : <input type="text" id="USN"/> </label> <br /> <br /> <label> Sem : <input type="text" id="sem"/> </label> <br /> <br /> <pre> <input type="submit" value="Submit"/> </pre> </form> </body> </html>

Program 5
a) Develop and demonstrate, using Javascript script, a XHTML document that contains three short paragraphs of text, stacked on top of each other, with only enough of each showing so that the mouse cursor can be placed over some part of them. When the cursor is placed over the exposed part of any paragraph, it should rise to the top to become completely visible. b) Modify the above document so that when a paragraph is moved from the top stacking position, it returns to its original position rather than to the bottom.

5a HTML
<?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml111/DTD/xhtml111.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Paragraph stacking program</title> <link rel="StyleSheet" type="text/CSS" href="5a_ext.css"> <script type="text/javascript" src="5a_ext.js"> </script> </head> <body> <form id="f1"> <p><h2>Stacking paragraphs (5A)</h2> </p> <p class="para1" id="p1" onmouseover="display('p1');"> If you can see me, congrats your program is working... </p> <p class="para2" id="p2" onmouseover="display('p2');"> This is the second para. </p> <p class="para3" id="p3" onmouseover="display('p3');"> This is the third para.

22 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


</p> </form> </body> </html>

5A CSS
/*5a.css*/ .para1{ border-style:solid; border-width:2px; background-color:yellow; padding:3cm; width:50px; height:50px; text-align:center; font-weight:bold; position:absolute; left:400px; top:100px; z-index:1; } .para2{ border-style:solid; border-width:2px; background-color:white; padding:3cm; width:50px; height:50px; text-align:center; font-weight:bold; color:blue; position:absolute; left:450px; top:150px; z-index:2; } .para3{ border-style:solid; border-width:2px; background-color:cyan; padding:3cm; width:50px; height:50px; text-align:center; font-weight:bold; position:absolute; left:500px; top:200px; z-index:3; }

23 | P a g e

7th semester CN and PW programs Harish Sethumadhavan

5A .JS
var cid="p3"; function display(nid) { var cs=document.getElementById(cid).style; var ns=document.getElementById(nid).style; cs.zIndex=0; ns.zIndex=4; cid=nid; }

5B CSS (SAME AS BEFORE) 5B HTML


<?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml111/DTD/xhtml111.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Paragraph stacking program</title> <link rel="StyleSheet" type="text/CSS" href="5b_ext.css"> <script type="text/javascript" src="5b_ext.js"> </script> </head> <body> <form id="f1"> <p><h2>Stacking paragraphs (5B)</h2> </p> <p class="para1" id="p1" onmouseover="display('p1');" onmouseout="disp();"> If you can see me, congrats your program is working... </p> <p class="para2" id="p2" onmouseover="display('p2');" onmouseout="disp();"> This is the second para. </p> <p class="para3" id="p3" onmouseover="display('p3');" onmouseout="disp();"> This is the third para. </p> </form> </body> </html>

5B .JS
var cid="p3";

24 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


function display(nid) { var cs=document.getElementById(cid).style; var ns=document.getElementById(nid).style; cs.zIndex=0; ns.zIndex=4; cid=nid; } function disp() { var s1=document.getElementById('p1').style; var s2=document.getElementById('p2').style; var s3=document.getElementById('p3').style; s1.zIndex=1; s2.zIndex=2; s3.zIndex=3; }

PROGRAM 6A XML
<?xml version="1.0"?> <?xml-stylesheet type="text/css" href="6a.css"?> <!--> <?xml-stylesheet type = "text/xsl" href = "6a.xsl" ?> <--> <vtu1> <vtu> <usn>1RV07CS020</usn> <name>Harish</name> <coll>RVCE</coll> <branch>CS</branch> <yoj>2007</yoj> <email>[email protected]</email> </vtu> <vtu> <usn>1BM07CS021</usn> <name>Karthik</name> <coll>BMSCE</coll> <branch>CS</branch> <yoj>2007</yoj> <email>[email protected]</email> </vtu> <vtu> <usn>1VI07CS028</usn> <name>Mayur</name> <coll>Vemana IT</coll> <branch>CS</branch> <yoj>2007</yoj> <email>[email protected]</email> </vtu>

25 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


</vtu1>

XSL
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" > <xsl:template match = "vtu1"> <h2> VTU Students Descriptions </h2> <xsl:for-each select="vtu"> <span style="font-style: italic; color: blue;"> USN: </span> <xsl:value-of select="USN" /> <br /> <span style="font-style: italic; color: blue;"> Name: </span> <xsl:value-of select="name" /> <br /> <span style="font-style: italic; color: blue;"> College: </span> <xsl:value-of select="college" /> <br /> <span style="font-style: italic; color: blue;"> Branch: </span> <xsl:value-of select="branch" /> <br /> <span style="font-style: italic; color: blue;"> Year of Join: </span> <xsl:value-of select="YOJ" /> <br /> <span style="font-style: italic; color: blue;"> E-Mail: </span> <xsl:value-of select="email"/> <br /> <br /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

CSS
vtu{ background-color: white; } usn, name, coll, branch, YOJ, email { font-size: 30; font-style: italic; color: red; display: block; }

PROGRAM 6B
26 | P a g e

7th semester CN and PW programs Harish Sethumadhavan

XML
<?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "6b.xsl" ?> <VTU> <USN> 1VI07CS020 </USN> <name> Harish </name> <college> VIT </college> <branch> CSE</branch> <YOJ> 2007 </YOJ> <email> [email protected] </email> </VTU>

XSL
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" > <xsl:template match = "VTU"> <h2> VTU Students' Description </h2> <span style="font-style: italic; color: blue;"> USN: </span> <xsl:value-of select="USN" /> <br /> <span style="font-style: italic; color: blue;"> Name: </span> <xsl:value-of select="name" /> <br /> <span style="font-style: italic; color: blue;"> College: </span> <xsl:value-of select="college" /> <br /> <span style="font-style: italic; color: blue;"> Branch: </span> <xsl:value-of select="branch" /> <br /> <span style="font-style: italic; color: blue;"> Year of Join: </span> <xsl:value-of select="YOJ" /> <br /> <span style="font-style: italic; color: blue;"> E-Mail: </span> <xsl:value-of select="email"/> <br /> <br /> </xsl:template> </xsl:stylesheet>

PROGRAM 7A
27 | P a g e

7th semester CN and PW programs Harish Sethumadhavan

.PL
#!/usr/bin/perl use CGI':standard'; print "content-type:text/html","\n\n"; print "<html>\n"; print "<head> <title> About this server </title> </head>\n"; print "<body><h1> About this server </h1>","\n"; print "Server name :",$ENV{'SERVER_NAME'},"<br>"; print "Running on port :",$ENV{'SERVER_PORT'},"<br>"; print "Server Software :",$ENV{'SERVER_SOFTWARE'},"<br>"; print "CGI-Revision :",$ENV{'GATEWAY_INTERFACE'},"<br>"; print "</body></html>\n"; exit(0); # 7a.pl

PROGRAM 7B HTML
<html> <body> <form action="http://localhost/cgi-bin/7b.pl"> <input type="text" name="com"> <input type="submit" value="Submit"> </form> </body> </html> <!-- 7b.html -->

PL
#!/usr/bin/perl use CGI':standard'; print header(); print start_html(-title=>"7b"); $c=param('com'); @a=`$c`; for $d(@a) { print $d,"<br />"; } print end_html(); #7b.pl

28 | P a g e

7th semester CN and PW programs Harish Sethumadhavan

PROGRAM 8A
#!/usr/bin/perl use CGI ':standard'; @a = ("Howz life","How you doing!", "Have a nice day", "Be good, do good."); $r = 4; $random_number = int(rand($r)); if(param) { print header(); print start_html(-title=>"User Name"); $cmd=param("name"); print b("Hello $cmd, $a[$random_number]"),"<br />"; print end_html(); } else { print header(); print start_html(-title=>"Enter user name"); print start_form(),textfield(-name=>"name"), submit(-name=>"submit",value=>"Submit"),reset(); print end_form(); print end_html(); }

8B
#!/usr/bin/perl use CGI ':standard'; print header(); print start_html(-title=>"WebPage Counter"); open(FILE,'<count.txt'); $count=<FILE>; close(FILE); $count++; open(FILE,'>count.txt'); print FILE "$count"; print b("This page has been viewed $count times"); close(FILE); print end_html();

PROGRAM 9
#!/usr/bin/perl use CGI ':standard';

29 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


print "Refresh: 1\n"; print "Content-Type: text/html\n\n"; print start_html(-title=>"Program 8"); ($s,$m,$h)=localtime(time); print br,br,"The current system time is $h:$m:$s"; print end_html();

PROGRAM 10 .PL
#! /usr/bin/perl print "Content-type: text/html\n\n"; print "<HTML><TITLE>Result of the insert operation </TITLE>"; use CGI ':standard'; use DBI; $dbh=DBI->connect("DBI:mysql:wp_020","root",""); $name=param("Name"); $age=param("Age"); $qh=$dbh->prepare("insert into table_1 values('$name','$age')"); $qh->execute(); $qh->finish(); $qh=$dbh->prepare("select * from table_1"); $qh->execute(); print "<table border size=1><tr><th>Name</th><th>Age</th></tr>"; while ( ($name,$age)=$qh->fetchrow()) { print "<tr><td>$name</td><td>$age</td></tr>"; } print "</table>"; $qh->finish(); $dbh->disconnect(); print"</HTML>";

HTML
<html> <body> <form action="http://localhost/xampp/10.pl"> Name : <input type="text" name="Name"> <br> Age :<input type="text" name="Age"> <br> <input type="submit" value="Submit"> </form> </body> </html>

30 | P a g e

7th semester CN and PW programs Harish Sethumadhavan

PROGRAM 11
<?php date_default_timezone_set('Asia/Calcutta'); $inTwoMonths = 60 * 60 * 24 * 60 + time(); setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths); if(isset($_COOKIE['lastVisit'])) { $visit = $_COOKIE['lastVisit']; echo "Your last visit was - ". $visit; } else echo "no cookie created"; ?>

PROGRAM 12
<?php session_start(); session_register("count"); if (isset($_SESSION['count'])) { $_SESSION["count"] ++; } else { $_SESSION["count"] =1; echo "<p>Counter initialized</p>\n"; } echo "<p>The counter is now <b>$_SESSION[count]</b></p>". "<p>Reload this page to increment</p>"; ?>

PROGRAM 13
<html> <body> <h1>Search for a person by Name</h1> <FORM ACTION="13.php" METHOD="POST"> <P> Name: <INPUT TYPE=text NAME="name" value=""> <BR> Address 1:<INPUT TYPE=text NAME="add1" value=""><BR> Address 2:<INPUT TYPE=text NAME="add2" value=""><BR> E-mail:<INPUT TYPE=text NAME="email" value=""><BR> <INPUT TYPE=submit VALUE="Insert"> </FORM>

31 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


<hr /> <form action="13.php" METHOD="POST"> <h3>Search for values</h3> Name: <INPUT TYPE=text NAME="search" > <BR> <input type="submit" value="Search"> </form> <?php $dbh = mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('harish') or die(mysql_error()); if(isset($_POST['name'])) { $nme = $_POST['name']; $ad1 = $_POST['add1']; $ad2 = $_POST['add2']; $eml = $_POST['email']; if($nme != "" && $ad1 != "") { $query = "INSERT INTO contacts VALUES ('$nme', '$ad1', '$ad2', '$eml')"; $result = mysql_query($query) or die(mysql_error()); } else echo "One of the field is empty";

//Searching for name if(isset($_POST['search'])) { $n=$_POST["search"]; print "Entered Name is : <B>$n </B>\n"; $var=mysql_query("SELECT * FROM contacts WHERE name like '%$n%'"); echo"<table border size=1>"; echo"<tr><th>Name</th> <th>Address 1</th> <th>Address 2</th> <th>E-mail</th></tr>"; while (($arr=mysql_fetch_row($var))) { echo "<tr><td>$arr[0]</td> <td>$arr[1]</td> <td>$arr[2]</td> <td>$arr[3]</td> </tr>"; } echo"</table>"; mysql_free_result($var); } mysql_close($dbh); ?>

32 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


</body> </html>

PROGRAM 14
<html> <body> <h1>Search for Books by title name</h1> <FORM ACTION="14.php" METHOD="POST"> <P> Accession number: <INPUT TYPE=text NAME="num" value=""> <BR> Title:<INPUT TYPE=text NAME="title" value=""><BR> Author:<INPUT TYPE=text NAME="author" value=""><BR> Edition:<INPUT TYPE=text NAME="edition" value=""><BR> Publisher:<INPUT TYPE=text NAME="publisher" value=""><BR> <INPUT TYPE=submit VALUE="Insert"> </FORM> <hr /> <form action="14.php" METHOD="POST"> <h3>Search for a book</h3> Title: <INPUT TYPE=text NAME="search" > <BR> <input type="submit" value="Search"> </form> <?php $dbh = mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('harish') or die(mysql_error()); if(isset($_POST['num'])) { $num = $_POST['num']; $title = $_POST['title']; $author = $_POST['author']; $edition = $_POST['edition']; $publisher = $_POST['publisher']; if($num != "" && $title != "") { $query = "INSERT INTO books VALUES ('$num', '$title', '$author', '$edition','$publisher')"; $result = mysql_query($query) or die(mysql_error()); } else echo "One of the field is empty";

//Searching for name if(isset($_POST['search'])) { $n=$_POST["search"]; print "Entered Name is : <B>$n </B>\n";

33 | P a g e

7th semester CN and PW programs Harish Sethumadhavan


$var=mysql_query("SELECT * FROM books WHERE title like '%$n%'"); echo"<table border size=1>"; echo"<tr><th>Accession Number</th> <th>Title</th> <th>Author</th> <th>Edition</th><th>Publisher</th></tr>"; while (($arr=mysql_fetch_row($var))) { echo "<tr><td>$arr[0]</td> <td>$arr[1]</td> <td>$arr[2]</td> <td>$arr[3]</td><td>$arr[4]</td> </tr>"; } echo"</table>"; mysql_free_result($var); } mysql_close($dbh); ?> </body> </html>

34 | P a g e

You might also like