Escolar Documentos
Profissional Documentos
Cultura Documentos
.
Ibrahimpatnam, R R District – 501 506 (A. P)
www.vibno.com
WRITE A PROGRAM THAT DESCRIBES TCP/IP CLIENT SERVER
COMMUNICATION IN DETAIL
AIM:
Program that illustrates tcp/ip client server communication in detail
PROGRAM:
#include “unp.h”
main(int argc,char **argv)
{
int listenfd,connfd;
pid_t childfd;
socklen_t clilen;
struct sock_addr_in cliaddr,servadr;
listenfd=Socket(AF_INET,SOCL_STREAM,0);
bzero(&servadr,sizeof(servaddr);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr,sin_port=htons(SERV_PORT);
bind(listenfd,(SA *) &servaddr, sizeof(servaddr));
listen(listenfd,LISTENQ);
for( ; ;)
{
clilen=sizeof(cliaddr);
connfd=accept9listenfd,(SA *) & cliaddr,&clilen);
if( (childpid=fork())==0)
close(listenfd);
str_echo(connfd);
exit(0);
}
close(connfd);
}
}
#include “unp.h”
void str_echo(int sockfd)
{
ssize_t n;
char buf[MAXLINE];
again:
while( (n=read(sockfd,buf,MAXLINE) )>0)
written(sockfd,buf,n);
if(n<0 && errno==EINTR)
goto again;
#include “unp.h”
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2)
err_quit(“usauge:tcpcli<IPaddress>”);
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(SA *) &servaddr,sizeof(servaddr));
str_cli(stdin,sockfd);
exit(0);
}
fputs(recvline,stdout);
}}
AIM:
Program that illustrates udp client server communication in detail
PROGRAM:
#include “unp.h”
#include “unp.h”
void dg(int sockfd, SA *pcliaddr, sockeln_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for( ; ; )
{
len=clilen;
n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
sendto(sockfd,mesg,n,0,pcliaddr,len);
}
}
#include “unp.h”
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2)
err_quit(“usauge:UDPcli<IPaddress>”);
bzero(servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
dg_cli(stdin,sockfd,(SA *) &servaddr,sizeof(servaddr));
#include “unp.h”
void dg_cli(FILE *fp,int sockfd,const SA *pervaddr,socklen_t servlen)
{
Int n;
char sendline[MAXLINE],recvline[MAXLINE];
while(fgets(sendline,MAXLINE,fp)! = NULL)
{
sendto(sockfd,sendline,strlen(sendline),0,servaddr,servlen);
n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
recvline[n]=0;
fputs(recvline,stdout);
}
}
AIM:
Program to perform message queues
DESCRIPTION:
MsqQ is the faster IPC
msgQ also provides synchronization for communication between two processes..
It is faster because the data transfer is done through DMA(direct mem access)..
Type is the second filtering mechanism for IPC here.(adv compared to fifo&pipe)
PROGRAM:
Save: msgrcv.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
main(int argc,char *argv[])
{
int key,id;
struct msgbuf{
long type;
char data[128];
}rcv;
rcv.type=atoi(argv[2]);//type for messageQueue
key=atoi(argv[1]);//key is used to connect to the queue
id=msgget(key,IPC_CREAT|0664);
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
main(int argc,char *argv[])
{
struct msgbuf {
long type;
char data[128];
}snd;
int key,id;
if(argc!=3)
{
printf("Invalid no of arguments\n");
return;
}
key=atoi(argv[1]);
snd.type=atoi(argv[2]);
printf("Enter the msq:");
scanf("%[^\n]",snd.data);
id=msgget(key,IPC_CREAT|0664);
if(msgsnd(id,&snd,strlen(snd.data),0))
printf("Successfully sent.....\n");
else
{
perror("");
return;
}
}
AIM:
C program that illustrate file locking using semaphores
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main(void)
{
key_t key;
int semid;
union semun arg;
if((key==ftok("sem demo.c","j"))== -1)
{
perror("ftok");
exit(1);
}
if(semid=semget(key,1,0666|IPC_CREAT))== -1)
{
perror("semget"):
exit(1);
}
arg.val=1;
if(semctl(semid,0,SETVAL,arg)== -1)
{
perror("smctl");
exit(1);
}
return 0;
}
OUTPUT:
semget
smctl
AIM:
Program for Pipe
DESCRIPTION:
Pipe is an IPC. Pipe provides communication as well as synchronization between two "related"
processes
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int filedes[2]; //argument for pipe fn....filedes[0] is for
reading...filedes[1] for writing to the pipe
char wrbuff[128],rdbuff[128]="hello this is child";
pid_t pid;
int i=pipe(filedes);//creates pipe
if (i==0)
{
printf("Pipe is Successfully created\n");
}
else
{
perror("");
}
pid=fork();
if(pid==0)
{
//child process
printf("In child process====%d\n",getppid());
read(filedes[0],rdbuff,128);//reading from read-end of pipe
printf("Msg from parent is %s\n",rdbuff);
}
else
{
//parent process
printf("In parent process\npid=====%d\n",getpid());
printf("Enter msg:");
OUTPUT:
$ cc pip.c
$ ./a.out
In child process==3564
In parent process== 3564
Enter msg:
hello
Message from parent is hello
AIM:
Program on illustrating Shared memory
DESRIPTION:
It is a PURE IPC....i.e, no synchronization is established between the two process..
It is the fastest IPC
Uses direct memory block
No extra fns for writing or reading from shared mem segment
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
main()
{
int id;
void *ptr;
id=shmget(22,128,IPC_CREAT|0664);//shmget allocates the shared memory segment
and returns the id associated with key......
ptr=shmat(id,0,0);//shmat attaches the shared memory segment identified by the id
for shared memory operation and returns the address of shared memory segment.
if(ptr)
{
printf("attached successfully\n");
}
else
{
perror("");
return;
}
printf("received :%s",(char*)ptr);//receiving the data from the shared mem segmet
shmdt(ptr);
}
Save: smssnd.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
main()
{
int id;
void *ptr;
OUTPUT:
$ cc smssnd.c
$ ./a.out
Successfully attached
Enter the message:hai
$ cc smsrcv.c
$ ./a.out
Attached successfully
Received:hai
AIM :
C program that displays the real time of a day every 60 seconds
PROGRAM :
#include<fcntl.h>
#include<stdio.h>
#include<time.h>
int main(void)
{
struct timeval t;
int h,m,s,sl;
gettimeofday(&t);
s1=t.ti_sec;
while(!kbhit())
{
gettime(&t);
h=t.ti_sec;
if(s1==s)
{
printf("%d :%d :%d ,h,m,s);
delay(5000);
}
}
}
OUTPUT:
13:29:12
AIM:
Program that performs FIFO
DESCRIPTION:
PROGRAM:
Save: fif.c
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
main()
{
pid_t p;
char buf[128],rdbuf[128];
int i=mkfifo("textf1",S_IRWXU);//mkfifo to create a fifo
if(i==0)
{
printf("Fifo is created\n");
}
else
perror("");
p=fork();//to create a child
if(p==0)
{
//child
int fd=open("textf1",O_RDWR);//opening the special file fifo
read(fd,rdbuf,128);//reading the data into rdbuf through file
descriptor fd
printf("Msg from parent is :%s",rdbuf);
}
else
{
//parent
int fd2=open("govin",O_RDWR);
printf("Enter a msg::\n");
scanf("%[^\n]",buf);
write(fd2,buf,128);//writing the data in the buf to fifo
through file descriptor fd2
}
}
Save: f1.c
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
int fd;
char str[123]="this is process one1111111111111";
mkfifo("textf12",S_IRWXU);
fd=open("textf12",O_RDWR);
write(fd,str,123);
}
Save:f2.c
/****Program2****/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
int fd;
char str[128];
mkfifo("textf12",S_IRWXU);
fd=open("textf12",O_RDWR);
read(fd,str,123);
printf("str::::%s",str);
}
OUTPUT:
$ cc fif.c
$ ./a.out
Fifo is created
Enter a msg:: hai
$ cc f1.c
$ cc f2.c
$ ./a.out
str::::this is process one11111111111
AIM:
Program that takes the contents of a text file & copies them into another text file character by
character
PROGRAM:
#include<stdio.h>
int main()
{
FILE *fp;
FILE *fp1;
char c;
fp=fopen("abc.txt","r");
fp1=fopen("bc.txt","w");
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
else
fputc(c,fp1);
}
fclose(fp);
fclose(fp1);
}
OUTPUT:
$ vi abc.txt
hai
$ vi c.txt
$ cc copy.c
$ . /a.out
$ vi bc.txt