Upload
ramakrishna-chowdary
View
274
Download
2
Embed Size (px)
Citation preview
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 1
UNIX
Shell ProgrammingLab Record
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a Shell script to display Prime numbers in a given
range.
UNIX Source Code :
echo Enter the range for prime number.read mn=2echo The prime numbers till $m are as follows:echo $nwhile [ $n -le $m ]do
n=`expr $n + 1`t=`echo $n /2 |bc`i=2flag=0while [ $i -le $t ]do
temp=`expr $n % $i` if [ $temp -eq 0 ] then flag=1 break fi
i=`expr $i + 1`doneif [ $flag -eq 0 -a $n -le $m ]then
echo $n fi
done
OUTPUT:
Page | 2
1. Display Prime Numbers
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a Shell script to find the factorial of a given number.
UNIX Source Code :
echo “Enter any number to find factorial.”read nfact=1i=1while [ $i -le $n ]do fact=`expr $fact \* $i` i=`expr $i + 1`doneecho “Factorial of $n is $fact”
OUTPUT:
Page | 3
2. Factorial of a given Number
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a Shell script to display Fibonacci series.
UNIX Source Code :
echo Enter the range to find Fibonacci series.read na=0b=1echo $aecho $bwhile [ $n -gt 2 ]do c=`expr $a + $b` echo $c a=$b b=$c n=`expr $n - 1`done
OUTPUT:
Page | 4
3. Fibonacci series
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim :Write a Shell script to find whether the given number is
palindrome or not.
UNIX Source Code :
echo Enter any number.read numa=$numrev=0while [ $num -ne 0 ]do rem=`expr $num % 10` rev=`expr $rev \* 10 + $rem` num=`expr $num / 10`doneif [ $a -eq $rev ]then echo The given number is a Palindrome.else echo The given number is not a Palindrome.fi
OUTPUT:
Page | 5
4. Palindrome
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a Shell Script for Multiplication table.
UNIX Source Code :
echo “Enter the number”read necho “Enter the limit”read lecho “MULTIPLICATION TABLE”echo “---------------------------------”i=1while [ $i –le $l ]doa=` expr $n \* $i `echo “$n * $i = $a”i= ` expr $i + 1 `done
OUTPUT:
Page | 6
5. Multiplication
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a Shell Script to find Greatest among three
Numbers.
UNIX Source Code :
echo “Enter the first number”read aecho “Enter the second number”read becho “Enter the third number”read cif [ $a -gt $b -a $a -gt $c ]
thenecho “$a is greater”else if [ $b -gt $c ]
then echo “$b is greater”
elseecho “$c is greater”
fi fi
OUTPUT:
Page | 7
6. Greatest among three Numbers
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 8
UNIX
Perl ProgrammingLab Record
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to demonstrate Arithmetic Operators in Perl.
print "enter first number:\n";
$n1=<stdin>;
print "enter second number:\n";
$n2=<stdin>;
print "enter ur choice:1:add,2:sub,3:mul,4:div,5:modulo\n";
$n3=<stdin>;
if($n3==1)
{
$n4=$n1+$n2;
print "sum of two numbers is:$n4\n";
}
elsif($n3==2)
{
$n4=$n1-$n2;
print "difference of two numbers is:$n4\n";
}
elsif($n3==3)
{
$n4=$n1*$n2;
print "multiplication of two numbers is:$n4\n";
}
elsif($n3==4)
{
$n4=$n1/$n2;
Page | 9
7. Arithmetic Operators in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
print "division of the given numbers is:$4\n";
}
elsif($n3==5)
{
$n4=$n1%$n2;
print "modulo of two numbers is:$n4\n";
}
else
{
print "enter correct choice:\n";
}
OUTPUT:
Page | 10
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: write a program to demonstrate string handling functions in perl
print "\n 1.join\n\t 2.Uppercase\n\t\t 3.Lowercase\n\t\t\t
4.Hexa-value\n\t\t\t\t 5.length\n\n Enter your Choice:";
$ch=<stdin>;
if($ch==1)
{
print "Enter First String:";
$str1=<stdin>;
print "Enter Second String for Join::";
$str2=<stdin>;
$result=join('.',$str1,$str2);
print "the joined String is:$result";
}
elsif($ch==2)
{
print "Enter any String:";
$str=<stdin>;
$uppercase=uc($str);
print "uppercase of given string is:$uppercase";
}
elsif($ch==3)
{
print "Enter any String:";
$str=<stdin>;
$lowercase=lc($str);
Page | 11
8. String Handling Functions in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
print "lower case of given string is:$lowercase";
}
elsif($ch==4)
{
print "Enter any String:";
$str=<stdin>;
$hex_val=hex($str);
print "Hexa decimal value of given string is:$hex_val";
}
elsif($ch==5)
{
print "Enter any String:";
$str=<stdin>;
$length1=length($str);
print "length of given string is:$length1\n";
}
else
{
print "Choose correct Choice";
}
OUTPUT:
Page | 12
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: write a program to demonstrate Fibonacci series in perl.
print"enter the num:";
$n=<stdin>;
$a=o;
$b=1;
print"$a\t$b\t";
for($i=1;$i<=$n;$i++)
{
$c=$a+$b;
$a=$b;
$b=$c;
print"$c\t";
}
OUTPUT:
Page | 13
9. Fibonacci Series in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: write a program to demonstrate Reading and Writing a data into a File.
open(READ,"<abc.dat");
$str=<READ>;
print "The file contains:$str";
close(READ);
open(WRITE,">>abc.dat");
print "Enter any string to write in to the file:";
$var=<stdin>;
print WRITE $var;
close(WRITE);
open(IN,"<abc.dat");
$var=<IN>;
print $var;
close(IN);
OUTPUT:
Page | 14
10. File Handling in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: write a program that takes the date from the user in the format DD-MM-YY and checks it is valid or not.
print "Enter the date in the format of [DD-MM-YY]:";
$date=<stdin>;
if($date=~m/((0[1-9]|1[0-9]|2[0-9]|3[0-1])-(0[1-9]|1[0-2])-([0-9]
[0-9]))/)
{
print "The given date is Valid:$date";
}
else
{
print "The given date is invalid:$date";
}
OUTPUT:
Page | 15
11. Pattern Matching in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: write a program to demonstrate Substitution in Perl using pattern matching
print "enter a string\n"; $name=<stdin>;print "enter a string for substitution\n"; $n1=<stdin>;print "enter string to substitution\n"; $n2=<stdin>;if($n1=~m/$n2/i)
{print "Replace not Needed(both strings are same)!";print "$name";}
else{$name=~s/$n1/$n2/g;print $name;}
OUTPUT:
Page | 16
12. Substitution in Perl
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 17
UNIX
PHP ProgrammingLab Record
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to illustrate predefined number functions in php.
<?php
print "number \t\t sqrt \t\t sqare \t\t cube \t\t quade\n";
print "------ \t\t ---- \t\t ----- \t\t ---- \t\t -----\n";
for($i=1;$i<=10;$i++)
{
$root=round(sqrt($i),2);
$sqr=pow($i,2);
$cube=pow($i,3);
$quad=pow($i,4);
print "$i \t$root \t\t $sqr \t\t $cube \t\t $quad\n";
}
?>
OUTPUT:
Page | 18
1. Number Functions in php
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to demonstrate String functions in php.
<?php
$var1="Pinky ";
$var2="Lucky";
$l=strlen($var1);
$c=strcmp($var1,$var2);
print "length of '$var1' is: $l\n";
print "comparision of '$var1' and $var2 is: $c\n";
print "sub string of '$var1' from 0 index and 4 charecters below:\n";
print substr($var1,0,4);
print "\n";
print " '$var1' is converted into lower case below:\n";
print strtolower($var1);
print "\n";
print " '$var2' is converted into upper case below:\n";
print strtoupper($var2);
print "\n";
print " '$var1': after applying trim below:\n";
print trim($var1);
print "\n";
?>
Page | 19
2. String Functions in php
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
OUTPUT:
Page | 20
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to illustrate Array functions in php.
<?php
$days=array("sun"=>1,"mon"=>2,"tue"=>3,"wed"=>4);
$day=array(1,2,3,4);
print "befor unset of 3 in day array are:\n";
foreach($day as $b)
print "$b\n";
print "after unset of 3 we get below:\n";
unset($day[2]);
foreach($day as $a)
print "$a\n";
if(array_key_exists("sun",$days))
{
print "sun is exists in days array\n";
}
if(in_array("3",$day))
{
print "3 exists in day\n";
}
if(is_array($day))
{
Page | 21
3. Array functions in php
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
print "day is array\n";
}
$s=sizeof($day);
print "size of $day is: $s\n";
foreach($days as $d=>$n)
print "key is: $d and value is: $n\n";
?>
OUTPUT:
Page | 22
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write program to illustrate Sort functions in php.
asort ( ):
<?php
$a=array(“mon”=>1,”tue”=>5,”wed”=>2);
asort($a);
foreach($a as $b=>$c)
print “$b = > $c\n”;
?>
OUTPUT:
ksort ( ):
<?php
$a=array(“Pinky”=>2,”Lucky”=>1,”Pinku”=>3);
Page | 23
4. Sort Functions in php
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
ksort($a);
foreach($a as $b=>$c)
print “$b = > $c\n”;
?>
OUTPUT:
rsort ( ):
<?php
$a=array(2,1,4,3,6);
rsort($a);
foreach($a as $b)
print “$b\n”;
?>
OUTPUT:
Page | 24
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
arsort ( ):
<?php
$a=array(“mon”=>1,”tue”=>5,”wed”=>2);
arsort($a);
foreach($a as $b=>$c)
print “$b = > $c\n”;
?>
OUTPUT:
krsort ( ):
<?php
$a=array(“zat”=>2,”bat”=>1,”apt”=>3);
ksort($a);
Page | 25
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
foreach($a as $b=>$c)
print “$b = > $c\n”;
?>
OUTPUT:
Page | 26
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to demonstrate form handling in PHP.
Details.html:
<html>
<head>
<TITLE> :Student Details: </TITLE>
</HEAD>
<BODY>
<h2>STUDENT DETAILS FORM</h2>
<form name=f1 action="details.php" method="POST">
Student Name:<input type="text" name="sname"><br>
Section:<input type="text" name="sec"><br>
gender:<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">FeMale<br>
Course:<select name="qual">
<option>MCA</option>
<option>MBA</option>
<option>BCA</option>
</select><br>
Address:<textarea rows="5" cols="20"
Page | 27
5. Form Handling in php
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
name="add"></textarea></br>
<input type=submit value=SUBMIT>
<input type=reset value=CANCEL>
</form>
</BODY>
</HTML>
Details.php:
<html>
<head>
<title>Processing Student deatils</title>
</head>
<body>
<?php
$name=$_POST["sname"];
$section=$_POST["sec"];
$gender=$_POST["gender"];
$course=$_POST["qual"];
$address=$_POST["add"];
?>
<table border=1>
<caption>STUDENT DETAILS</caption>
<tr>
<td>Name</td>
<td><?php print("name"); ?></td>
</tr>
Page | 28
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
<tr>
<td>Section</td>
<td><?php print("section"); ?></td>
</tr>
<tr>
<td>Gender</td>
<td><?php print("gender"); ?></td>
</tr>
<tr>
<td>Course</td>
<td><?php print("course"); ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php print("address"); ?></td>
</tr>
</table>
</body>
</html>
OUTPUT of Details.html:
Page | 29
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
OUTPUT of Details.php:
Page | 30
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 31
UNIX
IPC ProgrammingLab Record
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a program to create two processes, the parent
process sends a word to child process. The child process reads the word and display.
A) Parent writes and Child reads:
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<errno.h>
int main()
{
pid_t pid;
char line[20];
int n,fd[2];
if(pipe(fd)<0)
{
perror("Error in Creation of Pipe");
exit(1);
}
if((pid = fork()) < 0)
perror("unable to create the child process");
Page | 32
1. Demonstration of Pipe as IPC
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
else if(pid > 0 )
{ write(fd[1],"hello world",15); }
else
{
n = read(fd[0],line,20);
printf("bytes %d %s",n,line);
}
}
B) child writes and parent reads:
#include <stdio.h>
#include<sys/pipes.h>
#include<error.h>
#include<sys/ipc.h>
main()
{
int fd[2],n;
char buff[15];
pid_t pid;
if(pipe(fd)<0)
{
perror("unable to create pipe\n");
exit(1);
}
if((pid=fork())<0)
{
perror("unable to create child");
exit(0);
}
if(pid==0)
Page | 33
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
write(fd[1],"Hello PIPE",15);
else
{
n=read(fd[0],buff,15);
printf("data read is %s ",buff);
printf("no of bytes read is %d",n);
}
}
A) Expected OUTPUT of Parent writes and Child reads Program:
B) Expected OUTPUT of Child writes and Parent reads Program:
Page | 34
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write a program to demonstrate named pipes.
#include <stdio.h>
#include<sys/types.h>
#include<sys/pipes.h>
#include<error.h>
#include<sys/ipc.h>
main()
{
char buff[15];
int fd,n;
pid_t pid;
if((mknod("first",010666,0))<0)
perror(“Named pipe creation is fail”);
if(fd=open("first",O_RDWR))<0);
perror(“Failed to open the pipe”);
if((pid=fork())<0)
{
Page | 35
2. Named Pipes
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
perror("unable to create child");
exit(1);
}
if(pid>0)
{
write(fd,"hello world",15);
}
else
{
n=read(fd[0],buff,15);
printf("data read is %s ",buff);
printf("no of bytes read is %d",n);
}
}
Expected OUTPUT:
Page | 36
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write program to demonstrate message queue.
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<errno.h>
#include<unistd.h>
struct data
{
long mtype;
char myext[100];
}msg,msg1;
main()
{
int key=555;
int qid,pid;
Page | 37
3. Message queue
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
qid=msgget((key_t)key,IPC_CREAT|0666);
if((pid=fork())<0)
{ perror(“Error in creation of child process”);exit(1); }
if(pid>0)
{
msg.mtype=1;
strcpy(msg.mtext,”pinky”);
if(msgsnd(qid,&msg,sizeof(msg.mtext),0)<0)
{
perror(“parent process unable to send the message”);
exit(1);
}
}
else
{
if(msgrcv(qid,&msg1,sizeof(msg1.mtext),1,0)<0)
{
perror(“child process unable to receive the message”);
exit(1);
}
else
{
Printf(“mtype=%d mtext=%s”,msg1.mtype,msg1.mtext);
Page | 38
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
}
}
}
Expected OUTPUT:
Page | 39
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim : Write program to demonstrate Semaphores.
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<errno.h>
#include<unistd.h>
#define numloops 20
union semun
{
int val;
struct semid_ds *buf;
Page | 40
4. Semaphor
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
unsigned short *array;
struct seminfo* _buf;
};
int main()
{
int sem_set_id;
union semon sem_val;
int child_pid;
int i;
struct sembuf sem_op;
int rc;
struct timespec delay;
sem_set_id=semget(IPC_PRIVATE,1,0600);
if(sem_set_id==-1)
{
perror(“Error in creation”);
exit(1);
}
printf(“semaphore is created %d”,sem_set_id);
sem_val.val=0;
rc=semctl(sem_set_id,0,SETVAL,sem_val);
child_pid=fork();
switch(child_pid)
{
case -1:
perror(“Error in creation of process”);
exit(1);
Page | 41
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
case 0:
for(i=0;i<numloops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“consumer:%d\n”,i);
fflush(stdout);
}
break;
default:
for(i=o;i<numloops;i++)
{
printf(“producer: %d\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
sleep(1);
}
}
return 0;
}
Page | 42
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Expected OUTPUT:
Aim : Write a program to demonstrate the concept of shared memory using parent and child process.
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<errno.h>
#define SIZE 1024
main()
{
int shmid,pid,nread;
char *msg;
Page | 43
5. Shared Memory
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
key_t key=9955;
shmid=shmget(kwy,1024,IPC_CREAT|0666);
if(shmid<0)
{
perror(“shared memory not created”);
exit(1);
}
msg=shmat(shmid,0,0);
if((pid=fork())<0)
{
perror(“error in creating process”);
exit(0);
}
strcpy(msg,”pinky”);
if(pid==0)
{
nread=read(0,msg,SIZE);
}
else
write(1,msg,SIZE);
}
Expected OUTPUT:
Page | 44
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 45
UNIX
Socket ProgrammingLab Record
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a socket program for connection oriented iterative client.
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h> #include<errno.h>
main( int argc, char *argv[])
{
int sockfd,s,n;
struct sockaddr_in servaddr;
char buff1[max],buff2[max];
if(sockfd=socket(AF_INET,SOCK_STREAM,0)<0)
perror(" socket creation failed ");
Page | 46
1. Connection Oriented Client
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons( atoi(argv[1]));
servaddr.sin_addr.s_addr=inet_addr("192.068.001.215");
if(s=connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
perror("Request failed");
for(;;)
{
write(1,"Enter message:",15);
n=read(0,buff,max);
send(sockfd,buff1,max,0);
write(1,"client received",20);
write(1,buff2,n);
}
close(sockfd);
exit(1);
}
Expected OUTPUT:
Page | 47
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a socket program for connection oriented echo server.
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h>
main( int argc, char *argv[])
{
int sockfd,newsockfd,i,n,cli_len,max=80;
Page | 48
2. Connection Oriented Echo Server
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
struct sockaddr_in serv_addr, cli_addr;
char buff[max];
if(sockfd=socket(AF_INET,SOCK_STREAM,0)<0)
perror(" socket creation failed ");
bzero((char)*&serv_addr,sizeof(serv_addr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons( atoi(argv[1]));
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr)) <0)
perror(" binding failed ");
listen(sockfd,5);
printf( " server in waiting...... \n\n");
for(;;)
{
cli_len=sizeof(clia_ddr);
if(newsockfd =accept(sockfd,(struct sockaddr *)&cli_addr,&cli_len)<0)
write(1,"server accept ! error \n",24);
if((pid=fork())==0)
{
close(sockfd);
for(i=0;i<=10;++i)
{
write(1," server recieved: ",18);
n=recv(newsockfd,buff,max,0);
write(1,"buff,n);
write(1,"Enter message:",15);
read(0,buff,max);
Page | 49
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
send(newsockfd,buff,max,0);
}
close(newsockfd);
exit(0);
}
}
}
Expected OUTPUT:
Aim: Write a socket program for connection less echo server.
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h>
short portno;
Page | 50
3. Connectionless Echo Server
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
main( int argc, char *argv[])
{
int sockfd,numbytes,addr_len;
struct sockaddr_in serv_addr, cli_addr;
char buff[max];
if(argc!=2)
{
printf("usage:server<protno>");
exit(1);
}
if(sockfd=socket(AF_INET,SOCK_DGRAM,0)==-1)
{
printf("server socket ");
exit(1);
}
servaddr.sin_family=AF_INET;
portno=atoi(argv[1]);
servaddr.sin_port=htons(portno);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(struct sockaddr)) <0)
{
perror(" binding failed ");
exit(1);
}
addr_len=sizeof(cli_addr);
if((numbytes=recvfrom(sockfd,buff,sizeof(buff),0,
(struct sockaddr*)&cli_addr,&(addr_len)))<0)
{
Page | 51
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
perror("recvfrom");
exit(1);
}
printf( "Listener: \n\n");
printf("got packet from %s \n",inet_ntoa(cli_addr.sin_addr));
printf("\n packet is %d bytes long \n",numbytes);
printf("packet contains:%s\n",buff);
exit(0);
}
Expected OUTPUT:
Aim: Write a socket program for connection less echo client.
#include<stdio.h>
#include<sys/socket.h>
#include<arpha/intet.h>
#include<sys/types.h>
Page | 52
4. Connectionless Echo Client
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
#include<netinet/in.h>
#include<errno.h>
#define MAX 80
short portno;
main( int argc, char *argv[])
{
int sockfd,s,i,n;
struct sockaddr_in serv_addr;
char buff1[max],buff2[max];
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin.family=AF_INET;
serv_addr.sin_addr.s_addrinet_aaddr("127.0.0.1");
serv_addr.sin_port=htons(atoi(argv[1]));
s=connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
if(s<0)
{
printf(" error\n ");
}
for(;;)
{
writer(1," enter a message: ",15);
n=read(0,buff,MAX,0);
write(1," client recieved ",18);
write(1,buff2,n);
}
close(sockfd);
exit(0);
Page | 53
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
}
Expected OUTPUT:
Aim: Write a socket program connection less datagram client.
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
Page | 54
5. Connection less Client
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
#include<netdb.h>
short portno;
int main( int argc, char *argv[])
{
int sockfd,numbytes,addr_len;
struct sockaddr_in serv_addr,cliaddr;
char buff[100];
if(argc!=4)
{
printf(' usage: client<portno><hostname> <message> \n");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM))==-1)
{
printf(" server socket ");
}
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(0);
cliaddr.sin_addr.s_addr=htonl(atoi(01);
if(bind(sockfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr))==-1)
{
perror(" bind error ");
exit(1);
}
portno=atoi(argv[1]);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(portno);
servaddr.sin_addr.s_addr=inet+addr(argv[2]);
Page | 55
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
numbytes=sendto(sockfd.argv[3],strlen(argv[3]),0,
(struct sockaddr *)&servaddr,sizeof(servaddr));
if(numbytes <0)
printf("\n client: send to error \n ");
exit(1);
printf(" talker:\n ");
printf(" send %d number of bytes to %s \n",
numbytes,net_ntoa(servaddr.sin_addr));
exit(0);
}
Expected OUTPUT:
Aim: Write a socket program program to stream time client.
#include<stdio.h>
#include<sys/socket.h>
Page | 56
6. Stream Time Client
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<time.h>
short portno;
char *host;
main( int argc, char *argv[])
{
int sockfd,s,i,addlen,j,newfd;
struct sockaddr_in serv_addr;
char buff[512],*st;
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
host=argv[2];
serv_addr.sin_port=htons(portno);
serv_addr.sin.family=AF_INET;
serv_addr.sin_addr.s_addr=htonl("0L");
addlen=sizeof(serv_addr);
connect(sockfd,(struct sockaddr*)&serv_addr,addlen);
j=read(sockfd,buff,sizeof(buff));
write(1,buff,j);
}
Expected OUTPUT:
Page | 57
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a socket program for stream time server.
#include<stdio.h>
Page | 58
7. Stream Time Server
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
#include<sys/socket.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<time.h>
main( int argc, char *argv[])
{
int sockfd,i,cli_len,newfd;
struct sockaddr_in serv_addr,cli_addr;
char buff[512],*st;
long t;
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_port=htons(atoi(argv[1]));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(01);
bind(sockfd,(struct sockaddr *)& serv_addr.sizeof(serv_addr));
listen(sockfd,5);
printf(" server is waiting for client request... \n\n");
cli_len=sizeof(cli_addr);
newfd=accept(sockfd,(struct sockaddr *)& cli_addr,&cli_len);
for(;;)
{
cli_len=fork();
if( cli_len==0)
{
close(sockfd);
t=time(&t);
st=(char *)ctime(&t);
Page | 59
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
strcpy(buff,st);
write(newfd,buff,strlen(buff));
}
}
close(newfd);
}
Expected OUTPUT:
Aim: program for datagram time client.
Page | 60
8. Datagram Time Client
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h>
#include<string.h>
#include<time.h>
main( int argc, char *argv[])
{
int sockfd,i,addlen,j;
struct sockaddr_in serv_addr, cli_addr;
char buff[512],*st;
long t;
if(sockfd=socket(AF_INET,SOCK_DGRAM.0)<0)
{
perror(" socket not created ");
exit(1);
}
cli_addr.sin_port=htons(0);
cli_addr.sin_family=AF_INET;
cli_addr.sin_addr.s_addr=htonl(01);
if(bind(sockfd,(struct sockaddr *)&cli_addr,sizeof(cli_addr))<0);
{
perror(" bind error ");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htns(atoi(argv[1]));
Page | 61
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
serv_addr.sin_addr.s_addr=htnl(inet_addr(argv[2]));
addrlen=sizeof(serv_addr);
if(recvfrom(sockfd,buff,512,0,(struct sockaddr *)&serv_addr,&addrlen))<0)
{
perror(" error message ");
exit(1);
}
nbytes=strlen(buff);
if(write(1,buff,nbytes)!=nbytes)
{
perror (" write error ");
exit(1);
}
}
Expected OUTPUT:
Page | 62
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
UNIX
Python ProgrammingLab Record
Page | 63
1. Math Functions in Python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a python program for math functions in python.
x=-12
y=12.6
a=abs(x)
c=round(y)
d=complex(x,y)
e=pow(2,5)
print "absolute of x:",a
print "(division ,modulo):",divmod(x,y)
print "round value of (y):",c
print "complex value of (x and y)",d
print "power is",e
OUTPUT:
Page | 64
2. Arithmetic operators in python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a python program for Arithmetic Operators in python.
a=5
b=10
c=a+b
d=a-b
e=a*b
f=a/b
g=a%b
h=a**b
print "sum is",c
print "diff is",d
print "mul is",e
print "div is",f
print "mod is",g
print "pow is",h
OUTPUT:
Page | 65 3. Nested-if-Else Condition in Python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write program to illustrate the Nested-if-else Condition in Python.
s3=11
s6=13
n3=25
if s3 > s6 :
if s3 > n3 :
print ' ”s3” is greater ',s3
elif(s6>n3):
print ' “s6” is greater ',s6
else:
print ' “n3” is greater ',n3
OUTPUT:
Page | 66 4. String Functions in Python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to demonstrate string functions in Python.
str=raw_input()
print ' str to capitalize: ',str.capitalize()
print ' count of “o” appear in str: ',str.count('o',0,29)
print ' index of “mm” in str is: ',str.find('mm',0,29)
print ' “wel” is starting from first in str is:
',str.startswith('wel',0,29)
print ' “ing” is ending of str: ',str.endswith('ing',0,29)
print ' str`` is alphabets: ',str.isalpha()
print ' str is lower: ',str.islower()
print ' str is upper: ',str.isupper()
print ' str is Space: ',str.isspace()
print ' str is title: ',str.istitle()
print ' str length is: ',len(str)
print ' str in lower case: ',str.lower()
print ' str in upper: ',str.upper()
str1=str.replace('welcome','learned')
print ' After replace String:”welcome” with “learned” is:
',str1
print ' Original string is: ',str
Page | 67
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
OUTPUT:
Page | 68
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to illustrate Lists in Python.
list1=['h','e','l','l','o']
print ' first letter in list1 is: ',list1[0]
print ' last but one letter in list1 is: ',list1[-2]
print ' 0-2 index letters in list1 is: ',list1[0:3]
list2=['w','o','r','l','d']
list3=list1+list2
print ' combined list of list1 and list2 ',list3
print ' “l” in list3 count is: ',list3.count('l')
OUTPUT:
Page | 69
5. Lists in Python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Aim: Write a program to illustrate Classes in Python.
B) Creation of Class:
[lucky@localhost ~]$ python
Python 2.7 (r27:82500, Sep 16 2010, 18:03:06)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class FooClass(object):
... """my very first class: FooClass"""
... version=0.1
... def _init_(self, nm=' Pinky Lucky '):
... """Constructor"""
... self.name=nm
... print ' Created a class instance for ',nm
... def showname(self):
... """display instance attribute and class name"""
... print ' your name is: ',self.name
... def showver(self):
... """display class(static) attribute"""
... print self.version
... def addMe2Me(self,x):
... """apply + operation to argument"""
Page | 70
6. Classes in Python
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
... return x + x
...
>>>
B) Creation of Instance for Class:
>>> Fool = FooClass()
>>> Fool._init_()
Created a class instance for Pinky Lucky
>>> Fool.showname()
your name is: Pinky Lucky
>>> Fool.showver()
0.1
>>> print Fool.addMe2Me(5)
10
>>> print Fool.addMe2Me('virus')
virusvirus
>>>
OUTPUT:
Page | 71
MCA 2nd Year 2nd Semester – 2011 UINX-PROGRAMMING_LAB-
Page | 72