Upload
roshan-thomas
View
222
Download
0
Embed Size (px)
Citation preview
8/3/2019 CN and PW
1/34
8/3/2019 CN and PW
2/34
7th semester CN and PW programs HarishSethumadhavan
Program 2:
Write a program for frame sorting technique used in buffers.
//Frame sorting#include#include#define fsize 3#define pno 15
//Structure of a packettypedefstruct packet{char data[fsize+1];
int seq_num;
}packet;
//Array of packets and a message of max. length = no. of packets*max. frame size of1 packetpacket send[pno],rec[pno];char msg[pno*fsize];
//Max. no. of frames generatedint max_frames;
//UDFvoid make_frames();void shuffle_frames();
void receive_frames();void sort_frames();
int main(){system("clear");printf(" FRAME SORTING
PROGRAM\n");printf("Enter the message:");gets(msg);
//Make framesmake_frames();
//Shuffleshuffle_frames();
//Receive framesreceive_frames();
}
//Used to sort the framesvoid sort_frames()
2 | P a g e
8/3/2019 CN and PW
3/34
7th semester CN and PW programs HarishSethumadhavan
{int min,i,j;packet temp;for(i=0;i the position of string ptr in msg//modifying the base ptr of tmsg to use strcpywhile( i total no. of rand nos. gen so far
3 | P a g e
8/3/2019 CN and PW
4/34
7th semester CN and PW programs HarishSethumadhavan
int t,k=0,i;
//temp. array to ensure that rand() doesn't gen same number twiceint t_rand[max_frames+1];
for(i=0;i
8/3/2019 CN and PW
5/34
7th semester CN and PW programs HarishSethumadhavan
Write a program for distance vector algorithm to find suitable path forTransmission.
//:dv.c#include
#includestruct 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 TheNumber of Nodes\n");scanf("%d",&n);printf("Enter The Cost Matrix(999=infinity):\n");
for(i=0 ;i
8/3/2019 CN and PW
6/34
7th semester CN and PW programs HarishSethumadhavan
} 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 clientsend the file name and to make the server send back the contents of therequested file if present.
SERVER
#include
#include
#include
#include
#include
#include
#include
#define SERV_TCP_PORT 65323
6 | P a g e
8/3/2019 CN and PW
7/34
7th semester CN and PW programs HarishSethumadhavan
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))
8/3/2019 CN and PW
8/34
7th semester CN and PW programs HarishSethumadhavan
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 | P a g e
8/3/2019 CN and PW
9/34
7th semester CN and PW programs HarishSethumadhavan
}
CLIENT
#include
#include
#include
#include
#include
#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))
8/3/2019 CN and PW
10/34
7th semester CN and PW programs HarishSethumadhavan
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 IPCchannels.
CLIENT
#include#include#include#include#include#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
8/3/2019 CN and PW
11/34
7th semester CN and PW programs HarishSethumadhavan
SERVER
#include#include#include
#include#include#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)
8/3/2019 CN and PW
12/34
7th semester CN and PW programs HarishSethumadhavan
int gcd(long m,long n){
long r;while(n!=0){
r=m%n;m=n;n=r;
}return m;}
int mult(unsignedint x,unsignedint y,unsignedint n){ unsignedlongint k=1; int j; for(j=1;j
8/3/2019 CN and PW
13/34
7th semester CN and PW programs HarishSethumadhavan
for(i=0;i
8/3/2019 CN and PW
14/34
7th semester CN and PW programs HarishSethumadhavan
cout
8/3/2019 CN and PW
15/34
7th semester CN and PW programs HarishSethumadhavan
printf(" \nSecond | Packet Received | Packet Sent | Packet Left | PacketDropped \n");for(i=0;icap){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 useexternal style sheet, ordered list, table, borders, padding, color, and the tag.
Css
p,table,li,{
15 | P a g e
8/3/2019 CN and PW
16/34
7th semester CN and PW programs HarishSethumadhavan
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
Lab program1 This header is 36 pt
This header is blue
This paragraph has a left margin of 50 pixels
Name Email
16 | P a g e
8/3/2019 CN and PW
17/34
7th semester CN and PW programs HarishSethumadhavan
TSB Singh Prakash S manojKumar
This is a text. This is a text. This is a text. This is a text. This isa text. This is a text. This is a text. This is a text. This is a text. Thisis a text.
Program 2:
Develop and demonstrate a XHTML file that includes Javascript scriptfor the following problems:a) Input: A number n obtained using promptOutput: The first n Fibonacci numbersb) Input: A number n obtained using prompt
Output: A table of numbers from 1 to n and their squares using alert
2a
8/3/2019 CN and PW
18/34
7th semester CN and PW programs HarishSethumadhavan
{document.write(a+"
"+b+"
");
for(i=3;i
2b
Program 3
Develop and demonstrate a XHTML file that includes Javascript script
that uses functions for the following problems:a) Parameter: A stringOutput: The position in the string of the left-most vowelb) Parameter: A numberOutput: The number with its digits in the reverse order
3a
18 | P a g e
8/3/2019 CN and PW
19/34
7th semester CN and PW programs HarishSethumadhavan
Vowel position
Vowel Position
var pos=0;function func(str){
pos=str.value.search(/[aeiouAEIOU]/);if(pos>=0)alert("Position is:"+(pos+1));else alert("No vowels found!");
}
Enter text:
3b
Reverse of a numberfunction disp(num){var alphaExp = /^[0-9]+$/;
if(!num.value.match(alphaExp)){
alert("Input should be positive numeric");returnfalse;
}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);
}
19 | P a g e
8/3/2019 CN and PW
20/34
7th semester CN and PW programs HarishSethumadhavan
Reverse of a number
Enter a number :
Program 4
a) Develop and demonstrate, using Javascript script, a XHTMLdocument that collects the USN ( the valid format is: A digit from 1 to 4followed by two upper-case characters followed by two digits followed bytwo upper-case characters followed by three digits; no embedded spacesallowed) of the user. Event handler must be included for the form element
that collects this information to validate the input. Messages in the alertwindows 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
4a program
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();returnfalse;
}else {
returntrue;}
}
function checkUSN(){
var usn=document.getElementById("USN");if(isCorrect(usn)){alert("Valid USN :)");returntrue;
}
else{alert("The USN is invalid!");returnfalse;
20 | P a g e
8/3/2019 CN and PW
21/34
7th semester CN and PW programs HarishSethumadhavan
}
}
USN :
4b
4b program
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! ");
returnfalse;}elsereturntrue;
}
function isPerfect(sem){
if(sem.value.length==0 || sem.value8){sem.select();alert("Invalid Semester! ");returnfalse;
}
elseif(sem.value.search(/^[1-8]$/)==0)
returntrue;}function checkUSN(){
var usn=document.getElementById("USN");var sem=document.getElementById("sem");if(isCorrect(usn) && isPerfect(sem)){
alert("Valid inputs :)");returntrue;
21 | P a g e
8/3/2019 CN and PW
22/34
7th semester CN and PW programs HarishSethumadhavan
}returnfalse;
}
USN : Sem :
Program 5
a) Develop and demonstrate, using Javascript script, a XHTMLdocument that contains three short paragraphs of text, stacked on top ofeach other, with only enough of each showing so that the mouse cursorcan be placed over some part of them. When the cursor is placed over theexposed part of any paragraph, it should rise to the top to becomecompletely visible.b) Modify the above document so that when a paragraph is moved fromthe top stacking position, it returns to its original position rather than tothe bottom.
5a HTML
Paragraph stacking program
Stacking paragraphs (5A)
If you can see me, congrats your program is working...
This is the second para.
This is the third para.
22 | P a g e
8/3/2019 CN and PW
23/34
7th semester CN and PW programs HarishSethumadhavan
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
8/3/2019 CN and PW
24/34
7th semester CN and PW programs HarishSethumadhavan
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
Paragraph stacking program
Stacking paragraphs (5B)
If you can see me, congrats your program is working...This is the second para.This is the third para.
5B .JS
var cid="p3";
24 | P a g e
8/3/2019 CN and PW
25/34
7th semester CN and PW programs HarishSethumadhavan
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
1VI07CS028MayurVemana [email protected]
25 | P a g e
8/3/2019 CN and PW
26/34
7th semester CN and PW programs HarishSethumadhavan
XSL
VTU Students Descriptions USN:
Name:
College:
Branch:
Year of Join:
E-Mail:
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
8/3/2019 CN and PW
27/34
7th semester CN and PW programs HarishSethumadhavan
XML
1VI07CS020 Harish VIT CSE 2007 [email protected]
XSL
VTU Students' Description
USN:
Name:
College:
Branch:
Year of Join:
E-Mail:
PROGRAM 7A
27 | P a g e
8/3/2019 CN and PW
28/34
7th semester CN and PW programs HarishSethumadhavan
.PL
#!/usr/bin/perl
use CGI':standard'; # 7a.pl
print "content-type:text/html","\n\n";
print "\n";
print " About this server \n";
print " About this server ","\n";
print "Server name :",$ENV{'SERVER_NAME'},"
";
print "Running on port :",$ENV{'SERVER_PORT'},"
";
print "Server Software :",$ENV{'SERVER_SOFTWARE'},"
";
print "CGI-Revision :",$ENV{'GATEWAY_INTERFACE'},"
";
print "\n";
exit(0);
PROGRAM 7B
HTML
PL
#!/usr/bin/perl
use CGI':standard';print header(); #7b.plprint start_html(-title=>"7b");$c=param('com');@a=`$c`;for $d(@a){
print $d,"
";}print end_html();
28 | P a g e
8/3/2019 CN and PW
29/34
7th semester CN and PW programs HarishSethumadhavan
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]"),"
";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/perluse CGI ':standard';
print header();print start_html(-title=>"WebPage Counter");
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
8/3/2019 CN and PW
30/34
7th semester CN and PW programs HarishSethumadhavan
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 "Result of the insert operation ";
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 "NameAge";
while ( ($name,$age)=$qh->fetchrow())
{ print "$name$age";}
print "";$qh->finish();$dbh->disconnect();print"";
HTML
Name :
Age :
30 | P a g e
8/3/2019 CN and PW
31/34
7th semester CN and PW programs HarishSethumadhavan
PROGRAM 11
PROGRAM 12
PROGRAM 13
Search for a person by Name
Name:
Address 1:
Address 2:
E-mail:
31 | P a g e
8/3/2019 CN and PW
32/34
7th semester CN and PW programs HarishSethumadhavan
Search for valuesName:
8/3/2019 CN and PW
33/34
7th semester CN and PW programs HarishSethumadhavan
PROGRAM 14
Search for Books by title name
Accession number:
Title:
Author:
Edition:
Publisher:
Search for a bookTitle:
8/3/2019 CN and PW
34/34
7th semester CN and PW programs HarishSethumadhavan
$var=mysql_query("SELECT * FROM books WHERE title like '%$n%'");
echo"";echo"Accession Number Title Author
EditionPublisher";
while (($arr=mysql_fetch_row($var))){echo "$arr[0] $arr[1] $arr[2]
$arr[3]$arr[4] ";}echo"";
mysql_free_result($var);}
mysql_close($dbh);
?>