CN and PW
CN and PW
1| Page
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
2| Page
//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
Data\n"); ---------\n");
Program 3:
4| Page
5| Page
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>
6| Page
listen(sockfd,5);
if((pid=fork())<0) printf("error");
7| Page
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
//printf("\n\t%s",buffer); }
exit(0); } close(newsockfd); }
8| Page
CLIENT
#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;
9| Page
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
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
12 | P a g e
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
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
Css
p,table,li, {
15 | P a g e
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
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
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
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
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
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
<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
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
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 .JS
var cid="p3";
24 | P a g e
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
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
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
.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
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
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
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
//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
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
34 | P a g e