Você está na página 1de 15

GURU NANAK Engineering College

Department of Information Technology NETWORK PROGRAMING LAB (07A4EC12) I M Tech I Semester [Branch: IT] (2008-2009)

J.REENA MATHI GOLDA


Assistant professor

GuruNanak Engineering College Ibrahimpatnam, R R District 501 506 (A. P)

WRITE A PROGRAM THAT DESCRIBES TCP/IP CLIENT SERVER COMMUNICATION IN DETAIL AIM: Program that illustrates tcp/ip client server communication in detail PROGRAM: TCP ECHO SERVER:main() function: #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); } }

TCP ECHO SERVER:str_echo function: #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);

SUB: Software Engineering

Faculty: J.REENA

if(n<0 && errno==EINTR) goto again; else if (n>0) err_sys(str_echo:readerror); } TCP ECHO CLIENT:main() function: #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); } TCP ECHO CLIENT:str_cli function: #include unp.h void str_cli(FILE *fp,int sockfd) { char sendline[MAXLINE],recvline[MAXLINE]; while(fgets(sendline,MAXLINE,fp)! = NULL) { if(readline(sockfd,recvline,MAXLINE)==0) err_quit(str_cli:server terminated prematurely); fputs(recvline,stdout); }} WRITE A PROGRAM THAT DESCRIBES UDP CLIENT SERVER COMMUNICATION IN DETAIL AIM: Program that illustrates udp client server communication in detail PROGRAM: UDP ECHO SERVER:main() function:

SUB: Software Engineering

Faculty: J.REENA

#include unp.h main(int argc,char **argv) { int sockfd; pid_t childfd; socklen_t clilen; struct sock_addr_in cliaddr,servaddr; sockfd=Socket(AF_INET,SOCK_DGRAM,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)); dg_echo(sockfd,(SA *) &cliaddre,sizeof(cliaddr)); }

UDP ECHO SERVER:dg_echo function: #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); } }

UDP ECHO CLIENT:main() function: #include unp.h int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; if(argc!=2) err_quit(usauge:UDPcli<IPaddress>);

SUB: Software Engineering

Faculty: J.REENA

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)); exit(0); } UDP ECHO CLIENT:dg_cli function: #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); } } WRITE A PROGRAM TO PERFORM MESSAGE QUEUES 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;

SUB: Software Engineering

Faculty: J.REENA

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); //msgget gets the id of the msgQ associated with the key....if msgQ doesn't exist with that key,then it creates the msgQ with that key and returns the id. if(id<0) { perror(""); return; } printf("msgid==%d",id); //msgrcv is the funtion to receive a msg from msgQueue if(msgrcv(id,&rcv,128,rcv.type,0)) { printf("message is : %s",rcv.data); } } //Program for Msgsnd #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 { save: msgsnd.c

SUB: Software Engineering

Faculty: J.REENA

perror(""); return; } } OUTPUT: $ cc msgsnd.c $ ./a.out Enter msg: hai Successfully sent $ cc msgq.c $ ./a.out Msgid==0 Message is:hai
WRITE A PROGRAM THAT ILLUSTRATES FILE LOCKING USING SEMAPHORES

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);
SUB: Software Engineering Faculty: J.REENA

} return 0; } OUTPUT: semget smctl PROGRAM THAT ILLUSTRATES PIPE AIM: Program for Pipe DESCRIPTION: Pipe is a 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

SUB: Software Engineering

Faculty: J.REENA

printf("In parent process\npid=====%d\n",getpid()); printf("Enter msg:"); scanf(" %[^\n]",wrbuff); write(filedes[1],wrbuff,128);//writing to the write-end of pipe } } OUTPUT: $ cc pip.c $ ./a.out In child process==3564 In parent process== 3564 Enter msg: hello Message from parent is hello

WRITE A PROGRAM ON ILLUSTRATING SHARED MEMORY AIM:

SUB: Software Engineering

Faculty: J.REENA

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: // PROGRAM FOR SMS RCV save: smsrcv.c #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; id=shmget(22,128,IPC_CREAT|0664); if(ptr=shmat(id,0,0)) { printf("Successfully attached\n");

SUB: Software Engineering

Faculty: J.REENA

} else { perror(""); return; } printf("Enter the msg :"); scanf("%[^\n]",ptr); } OUTPUT: $ cc smssnd.c $ ./a.out Successfully attached Enter the message:hai $ cc smsrcv.c $ ./a.out Attached successfully Received:hai

WRITE A C PROGRAM THAT DISPLAYS THE REAL TIME OF A DAY EVERY 60 SECONDS

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;

SUB: Software Engineering

Faculty: J.REENA

while(!kbhit()) { gettime(&t); h=t.ti_sec; if(s1==s) { printf("%d :%d :%d ,h,m,s); delay(5000); } } } OUTPUT: 13:29:12

WRITE A PROGRAM TO PERFORMS FIFO AIM: Program that performs FIFO DESCRIPTION: Fifo is also called as named pipe Fifo is used for IPC with synchronization Fifo can also be used for IPC between non related processes 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

SUB: Software Engineering

Faculty: J.REENA

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() {

SUB: Software Engineering

Faculty: J.REENA

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
WRITE A PROGRAM THAT TAKES THE CONTENTS OF A TEXT FILE & COPIES THEM INTO ANOTHER TEXT FILE CHARACTER BY CHARACTER

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); }

SUB: Software Engineering

Faculty: J.REENA

OUTPUT: $ vi abc.txt hai $ vi c.txt $ cc copy.c $ . /a.out $ vi bc.txt

SUB: Software Engineering

Faculty: J.REENA

Você também pode gostar