28
MIC KANCHIKACHERLA Devineni Venkata Ramana & Dr. Hima Sekhar MIC College of Technology 1 Devineni Ventaka Ramana & Dr. Hima Sekhar MIC College of Technology Kanchikacharla, Krishna District, Pin: 521180, A.P, India. Phone No.: 08678 – 273535 Lab Instruction Manual Network Programming Prepared By A. Rama Satish M.Tech(CSE) Associate Professor Department of Computer Science and Engineering

Jntuk 4 1 Cse Network Programming Lab Manual

Embed Size (px)

Citation preview

Page 1: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

1

Devineni Ventaka Ramana & Dr. Hima Sekhar

MIC College of Technology Kanchikacharla, Krishna District, Pin: 521180, A.P, India.

Phone No.: 08678 – 273535

Lab Instruction Manual Network Programming

Prepared By

A. Rama Satish M.Tech(CSE) Associate Professor

Department of Computer Science and Engineering

Page 2: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

2

Week 1: Aim: C Program for implementing IPC of the forms: a. Pipes b. FIFO Program a: #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char *argv[]) { int fifo[2]; pid_t pid1,pid2; if(pipe(fifo)) { perror("pipe"); exit(2); } switch(pid1=fork()) { case -1:perror("fork"); exit(3); case 0:if(dup2(fifo[1],fileno(stdout))==-1) { perror("dup2"); exit(4); } close(fifo[0]); close(fifo[1]); if(execlp("/bin/sh","sh","-c",argv[1],NULL)==-1) { perror("execlp"); exit(5); } } switch(pid2=fork()) { case -1:perror("fork"); exit(6); case 0:if(dup2(fifo[0],fileno(stdin))==-1) { perror("dup2"); exit(7); } close(fifo[0]); close(fifo[1]); if(execlp("/bin/sh","sh","-c",argv[2],NULL)==-1) { perror("execlp"); exit(8); } } close(fifo[0]); close(fifo[1]); if(waitpid(pid1,&fifo[0],0)!=pid1 || waitpid(pid2,&fifo[1],0)!=pid2)

Page 3: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

3

perror("waitpid"); return (fifo[0]+fifo[1]); } Sample Output: $ cc –o pipeprg pipeprg.c $./pipeprg “cat pipeprg.c” more #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main(int argc,char *argv[]) { int fifo[2]; pid_t pid1,pid2; if(pipe(fifo)) { perror("pipe"); exit(2); } switch(pid1=fork()) { case -1:perror("fork"); exit(3); case 0:if(dup2(fifo[1],fileno(stdout))==-1) { perror("dup2"); ------The “cat pipeprg.c” command displayed page by page--------- Program b: /** Fifo Server*/ # include <unistd.h> # include<string.h> # include <stdio.h> # include <sys/stat.h> # include <sys/types.h> # include <errno.h> # include <stdlib.h> #include<fcntl.h> # define FIFO1 "/tmp/FIFO1" # define FIFO2 "/tmp/FIFO2" # define perms 0666 void server(int rfd,int wfd); int main(void) { int rfd,wfd; if((mknod(FIFO1,S_IFIFO|perms,0)<0)&&(errno!=EEXIST)) { perror("mknod"); exit(2); } if((mknod(FIFO2,S_IFIFO|perms,0)<0)&&(errno!=EEXIST)) { unlink(FIFO1);

Page 4: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

4

perror("mknod"); exit(3); } if((rfd=open(FIFO1,0))<0) { perror("open"); exit(4); } if((wfd=open(FIFO2,1))<0) { perror("open"); exit(5); } server(rfd,wfd); close(rfd); close(wfd); exit(0); } void server(int rfd,int wfd) { char buf[100];int n,fd; if((n=read(rfd,buf,100))<=0) { perror("read");exit(0); } buf[n]='\0'; fd=open(buf,0); while((n=read(fd,buf,100))>0) if(write(wfd,buf,n)!=n) perror("write error"); if(n<0) perror("write error"); } /** Fifo Client */ # include <unistd.h> # include<string.h> # include <stdio.h> # include <sys/stat.h> # include <sys/types.h> # include <stdlib.h> # include <errno.h> # define FIFO1 "/tmp/FIFO1" # define FIFO2 "/tmp/FIFO2" # define perms 0666 void client(int rfd,int wfd); int main(void) { int rfd,wfd; if((wfd=open(FIFO1,1))<0) { perror("open"); exit(2);

Page 5: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

5

} if((rfd=open(FIFO2,0))<0) { perror("open"); exit(3); } client(rfd,wfd); if(unlink(FIFO1)<0) { perror("unlink");exit(4); } if(unlink(FIFO2)<0) { perror("unlink");exit(5);} exit(0); } void client(int rfd,int wfd) { char buf[100];int n; if(fgets(buf,100,stdin)==NULL) { perror("client error"); exit(6); } n=strlen(buf); if(buf[n-1]=='\n') n--; if(write(wfd,buf,n)!=n) { perror("write"); exit(7); } while((n=read(rfd,buf,100))>0) if(write(1,buf,n)!=n) perror("data error"); if(n<0) perror("data read"); } Sample Output: $./fifoser /** Server reads the file name from fifo client and opens that file and read the contents of that file and send to fifo client. ***/

$./fifocli pipeprg.c /** Here at client side the content of pipeprg.c file ***/

Page 6: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

6

Week 2: Aim: C Program to implement file transfer Using IPC “Message Queue” Program: //***Message transferring by taking the message from file using message queue ***/ #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<string.h> #include<fcntl.h> #define MSGSZ 128 #define KEY 1234 typedef struct msgbuf { long mtype; char mtext[5555]; }mesg_buf; int main(void) { int msgid,msgflg=IPC_CREAT|0666,fptr,n; char buff[128]; mesg_buf sbuf; size_t buf_length; if((msgid=msgget(KEY,msgflg))<0) { perror("msgget"); exit(2); } else printf("message queue created:%d\n",msgid); sbuf.mtype=1; fptr=open("tcpcli.c",O_RDONLY,0); while((n=read(fptr,buff,sizeof(buff)))>0) { strncat(sbuf.mtext,buff,n); bzero(buff,sizeof(buff)); } buf_length=strlen(sbuf.mtext); if((msgsnd(msgid,&sbuf,buf_length,IPC_NOWAIT))<0) { perror("msgsnd"); exit(3); } printf("message sent \n"); exit(0);

Page 7: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

7

} //***Message transferring by taking the message from file using message queue ***/ #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<string.h> #include<fcntl.h> #include<sys/stat.h> #define MSGSZ 128 #define KEY 1234 typedef struct msgbuf { long mtype; char mtext[5555]; }mesg_buf; int main(void) { int msgid,fptr,n; mesg_buf rbuf; if((msgid=msgget(KEY,0))<0) { perror("mesgget"); exit(2); } fptr=creat("tmp",S_IRWXU); n=msgrcv(msgid,&rbuf,sizeof(rbuf.mtext),1,0); write(fptr,rbuf.mtext,n); //write(fptr,NULL,sizeof(NULL)); //bzero(rbuf.mtext,55555); printf("message recieved "); exit(0); } Sample Output: $./msgsnd Messge Queue is Created /** Then opens the file tcpcli.c file and send the file contents as a message in message queue to receiver ****/

$./msgrcv /*** Here it opens the existing message queue and reads the one message and stores it in “tmp” file *****/

Page 8: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

8

Week 3: Aim: C Program to create an integer variable using shared memory concept and increment the variable simultaneously by two processes using semaphore to avoid race condition. Program: /** Process 1 ****/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/sem.h> #include<error.h> void unlock(int semid); void lock(int semid); int main(void) { key_t key1=123,key2=456; int semid,shmid,size=27; int *s,*sptr; char ch='y'; union semun { int val; struct semid_ds *buff; ushort *array; }semun1; if((semid=semget(key1,1,IPC_CREAT|0666))<0) { perror("semget"); exit(2); } if((shmid=shmget(key2,size,IPC_CREAT|0666))<0) { perror("shmget"); exit(3); } s=shmat(shmid,(void *)0,0); semun1.val=1; if((semctl(semid,0,SETVAL,semun1))<0) { perror("semctl"); exit(4); } sptr=s; *sptr=0; while(ch!='n')

Page 9: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

9

{ lock(semid); *sptr=*sptr+1; printf("%d\n",*sptr); printf("enter any character to unblock\n"); ch=getchar(); unlock(semid); } shmdt((void *)0); return (0); } void lock(int semid) { struct sembuf sop; sop.sem_num=0; sop.sem_op=-1; sop.sem_flg=0; semop(semid,&sop,1); } void unlock(int semid) { struct sembuf sop; sop.sem_num=0; sop.sem_op=1; sop.sem_flg=0; semop(semid,&sop,1); } /** Process 2 ****/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/sem.h> #include<error.h> void unlock(int semid); void lock(int semid); int main(void) { key_t key1=123,key2=456; int semid,shmid,size=27; int * sptr,*s; char ch='y'; union semun { int val; struct semid_ds *buff; ushort *array;

Page 10: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

10

}semun1; if((semid=semget(key1,1,0))<0) { perror("semget"); exit(2); } if((shmid=shmget(key2,size,0))<0) { perror("shmget"); exit(3); } s=shmat(shmid,0,0); semun1.val=1; sptr=s; while(ch!='n') { lock(semid); *sptr=*sptr+1; printf("%d\n",*sptr); printf("enter any character to unblock\n"); ch=getchar(); unlock(semid); } shmdt(0); return 0; } void lock(int semid) { struct sembuf sop; sop.sem_num=0; sop.sem_op=-1; sop.sem_flg=0; semop(semid,&sop,1); } void unlock(int semid) { struct sembuf sop; sop.sem_num=0; sop.sem_op=1; sop.sem_flg=0; semop(semid,&sop,1); } Sample Output: $./shm1 1 3 …..

$./shm2 2 4 ….

Page 11: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

11

Week 4 Aim: C program to perform iterative client & server operation along with reversing given input sequence. Program: /*Server*/ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in servaddr,cliaddr; int j,k,lfd,cfd,clilen,bytes,n; char buf1[100],revmesg[100]; lfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(struct sockaddr_in)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ bind(lfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(lfd,5); for(;;) { clilen=sizeof(struct sockaddr_in); cfd=accept(lfd,(struct sockaddr *)&cliaddr,&clilen); bzero(buf1,100); while((n=recv(cfd,buf1,100,0))>0) { k=n; for(j=0;j<=n-1;j++) { revmesg[k-1]=buf1[j]; k--; } send(cfd,revmesg,n,0); bzero(buf1,100); bzero(revmesg,100); } } } /** Client*/ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h>

Page 12: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

12

#include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in scaddr; int clfd,n; char buf[100],buf2[100]; clfd=socket(AF_INET,SOCK_STREAM,0); bzero(&scaddr,sizeof(struct sockaddr_in)); scaddr.sin_family=AF_INET; scaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ inet_pton(AF_INET,"127.0.0.1",&scaddr.sin_addr); connect(clfd,(struct sockaddr *)&scaddr,sizeof(scaddr)); while((n=read(fileno(stdin),(void *)buf,100))>0) { send(clfd,(void *)buf,n,0); recv(clfd,buf2,n,0); write(fileno(stdout),buf2,n); bzero(buf,100); bzero(buf2,100); } close(clfd); exit(0); } Sample Output: $./itetcpser /*** Server Started ***/

$./itetcpcli Rama amaR ……..

Page 13: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

13

Week 5 Aim: C program to perform concurrent client & server operation along with reversing given input sequence. Program: /** Server */ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in servaddr,cliaddr; int j,k,lfd,cfd,clilen,bytes,n; char buf1[100],revmesg[100]; pid_t pid; lfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(struct sockaddr_in)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ bind(lfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(lfd,5); for(;;) { clilen=sizeof(struct sockaddr_in); cfd=accept(lfd,(struct sockaddr *)&cliaddr,&clilen); bzero(buf1,100); if((pid=fork())==0) { close(lfd); while((n=recv(cfd,buf1,100,0))>0) { k=n; for(j=0;j<=n-1;j++) { revmesg[k-1]=buf1[j]; k--; } send(cfd,revmesg,n,0); bzero(buf1,100); bzero(revmesg,100); } close(cfd); } } }

Page 14: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

14

/** Client*/ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in scaddr; int clfd,n; char buf[100],buf2[100]; clfd=socket(AF_INET,SOCK_STREAM,0); bzero(&scaddr,sizeof(struct sockaddr_in)); scaddr.sin_family=AF_INET; scaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ inet_pton(AF_INET,"127.0.0.1",&scaddr.sin_addr); connect(clfd,(struct sockaddr *)&scaddr,sizeof(scaddr)); while((n=read(fileno(stdin),(void *)buf,100))>0) { send(clfd,(void *)buf,n,0); recv(clfd,buf2,n,0); write(fileno(stdout),buf2,n); bzero(buf,100); bzero(buf2,100); } close(clfd); exit(0); } Sample Output: $./contcpser /*** Server Started ***/

$./contcpcli Rama amaR ……..

Page 15: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

15

Week 6: Aim: C program to transfer file using TCP client & server application Program: /** Server */ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in ssaddr,caddr; int lfd,cfd,bytes,len,fptr; char buf1[100]; lfd=socket(AF_INET,SOCK_STREAM,0); bzero(&ssaddr,sizeof(struct sockaddr_in)); ssaddr.sin_family=AF_INET; ssaddr.sin_addr.s_addr=htonl(INADDR_ANY); ssaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ bind(lfd,(struct sockaddr *)&ssaddr,sizeof(ssaddr)); listen(lfd,5); for(;;) { len=sizeof(caddr); cfd=accept(lfd,(struct sockaddr *)&caddr,&len); bzero(buf1,100); fptr=open("ashok",O_RDWR,0); while((bytes=read(fptr,(void *)buf1,sizeof(buf1)))) { write(cfd,(void *)buf1,bytes); bzero(buf1,sizeof(buf1)); } close(fptr); close(cfd); } exit(0); } /** Client */ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h>

Page 16: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

16

#include<stdlib.h> #include<strings.h> #include<string.h> #include<netinet/in.h> int main(void) { struct sockaddr_in scaddr; int clfd,n,fptr,len; char buf[100]; clfd=socket(AF_INET,SOCK_STREAM,0); bzero(&scaddr,sizeof(struct sockaddr_in)); scaddr.sin_family=AF_INET; scaddr.sin_port=htons(50002);/*example SERV_PORT=50002*/ inet_pton(AF_INET,"127.0.0.1",&scaddr.sin_addr); connect(clfd,(struct sockaddr *)&scaddr,sizeof(scaddr)); fptr=creat("c.txt",S_IRWXU); while(n=read(clfd,(void *)buf,sizeof(buf)-1)) { write(fptr,buf,n); bzero(buf,sizeof(buf)); } close(clfd); exit(0); } Sample Output: $./filtcpser /***Here ashok file contens are transmitted by server to the client ***/

$./filtcpcli /** Here c.txt file created with contents sent by the server ***/

Page 17: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

17

Week 7: Aim: C program to design a TCP Concurrent server to convert a given text into upper case using multiplexing system call “Select”. Program: /** Server*/ #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<fcntl.h> #include<poll.h> #include<sys/select.h> #include<ctype.h> void tcpser(int); int main() { int lfd,cfd,nbs; socklen_t clen; struct sockaddr_in caddr,saddr; lfd=socket(AF_INET,SOCK_STREAM,0); bzero(&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(55577); saddr.sin_addr.s_addr=htonl(INADDR_ANY); bind(lfd,(struct sockaddr*)&saddr,sizeof(saddr)); listen(lfd,5); tcpser(lfd); close(lfd); return(0); } void tcpser(int lfd) { int n,nready,i,cfd,clen,max; char buf[100],rbuf[100]; fd_set rset; int pid; int client[5]; struct sockaddr_in caddr; FD_ZERO(&rset); client[0]=lfd; FD_SET(lfd,&rset); max=lfd; for(;;) { nready=select(max+1,&rset,NULL,NULL,NULL); if(FD_ISSET(client[0],&rset)) { clen=sizeof(caddr); cfd=accept(lfd,(struct sockaddr *)&caddr,&clen);

Page 18: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

18

max=max>cfd?max:cfd; if((pid=fork())==0) { close(lfd); printf("%d\n",pid); } } FD_SET(cfd,&rset); client[1]=cfd; if(FD_ISSET(client[1],&rset)) { n=read(client[1],rbuf,100); for(i=0;i<strlen(rbuf);i++) rbuf[i]=(char)toupper((int)rbuf[i]); rbuf[i]='\0'; write(client[1],rbuf,strlen(rbuf)); bzero(rbuf,sizeof(rbuf)); } } } /** Client */ #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<fcntl.h> #include<poll.h> #include<sys/select.h> void tcpcli(int,FILE *); int main() { int sfd; struct sockaddr_in saddr; bzero(&saddr,sizeof(saddr)); sfd=socket(AF_INET,SOCK_STREAM,0); saddr.sin_family=AF_INET; saddr.sin_port=htons(55577); inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr); connect (sfd,(struct sockaddr *)&saddr,sizeof(saddr)); tcpcli(sfd,stdin); return(0); } void tcpcli(int fd,FILE *fp) { char sbuf[100],rbuf[100]; int n; printf("\n");

Page 19: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

19

bzero(&sbuf,sizeof(sbuf)); bzero(&rbuf,sizeof(rbuf)); while(fgets(sbuf,100,fp)!=NULL) { write(fd,sbuf,strlen(sbuf)); n=read(fd,rbuf,100); write(1,rbuf,strlen(rbuf)); bzero(&sbuf,sizeof(sbuf)); bzero(&rbuf,sizeof(rbuf)); } } Sample Output: $./seltcpser

$./seltcpcli rama RAMA krishna KRISHNA …….

Page 20: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

20

Week 8: Aim: C program to design a TCP concurrent server to echo given set of sentences using poll functions. Program: /** Server */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #include <limits.h> /* for OPEN_MAX */ #include <poll.h> #include <errno.h> #define MAXLINE 100 #define SERV_PORT 13154 #define POLLRDNORM 5 #define INFTIM 5 #define OPEN_MAX 5 int main(int argc, char **argv) { int k,i, maxi, listenfd, connfd, sockfd; int nready; ssize_t n; char line[MAXLINE]; socklen_t clilen; struct pollfd client[OPEN_MAX]; struct sockaddr_in cliaddr, servaddr; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); listen(listenfd, 5); client[0].fd = listenfd; client[0].events = POLLRDNORM; for (i = 1; i < OPEN_MAX; i++) client[i].fd = -1; /* -1 indicates available entry */ maxi = 0; /* max index into client[] array */ for ( ; ; ) { nready = poll(client, maxi+1, INFTIM); if (client[0].revents & POLLRDNORM) { /* new client connection */ clilen = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen); #ifdef NOTDEF printf("new client: %s\n", sock_ntop((struct sockaddr *) &cliaddr, clilen));

Page 21: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

21

#endif for (i = 1; i < OPEN_MAX; i++) if (client[i].fd < 0) { client[i].fd = connfd; /* save descriptor */ break; } if (i == OPEN_MAX) { printf("too many clients"); exit(0); } client[i].events = POLLRDNORM; if (i > maxi) maxi = i; /* max index in client[] array */ if (--nready <= 0) continue; /* no more readable descriptors */ } for (i = 1; i <= maxi; i++) { /* check all clients for data */ if ( (sockfd = client[i].fd) < 0) continue; if (client[i].revents & (POLLRDNORM | POLLERR)) { if ( (n = read(sockfd, line, MAXLINE)) < 0) { if (errno == ECONNRESET) { /*4connection reset by client */ #ifdef NOTDEF printf("client[%d] aborted connection\n", i); #endif close(sockfd); client[i].fd = -1; } else printf("readline error"); } else if (n == 0) { /*4connection closed by client */ #ifdef NOTDEF printf("client[%d] closed connection\n", i); #endif close(sockfd); client[i].fd = -1; } else{ printf("\n data from client is \n"); line[n]='\0'; k=strlen(line); printf(" length=%d data = %s\n", k,line); //write(sockfd, line, n); strcpy(line," "); } if (--nready <= 0) break; /* no more readable descriptors */ } } } }

Page 22: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

22

/** Client */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #define MAXBUFFER 1024 void sendstring(int , char *); int main( int C, char *V[] ) { int sd,fd; char c; struct sockaddr_in serveraddress; char text[100]; int i=0; sd = socket( AF_INET, SOCK_STREAM, 0 ); if( sd < 0 ) { perror( "socket" ); exit( 1 ); } if (V[1] == NULL ) { printf ("PL specfiy the server's IP Address \n"); exit(0); } if (V[2] == NULL ) { printf ("PL specify the server's Port No \n"); exit(0); } memset( &serveraddress, 0, sizeof(serveraddress) ); serveraddress.sin_family = AF_INET; serveraddress.sin_port = htons(atoi(V[2]));//PORT NO serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS if (connect(sd,(struct sockaddr*)&serveraddress, sizeof(serveraddress))<0) { printf("Cannot Connect to server"); exit(1); } printf("enter sentence to end enter #"); while(1) { c=getchar(); if(c=='#') break; text[i++]=c;

Page 23: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

23

} text[i]='\0'; sendstring(sd,text); close(sd); return 0; } void sendstring( int sd, /*Socket Descriptor*/ char *fname) /*Array Containing the string */ { int n , byteswritten=0 , written ; char buffer[MAXBUFFER]; strcpy(buffer , fname); n=strlen(buffer); while (byteswritten<n) { written=write(sd , buffer+byteswritten,(n-byteswritten)); byteswritten+=written; bzero(buffer+byteswritten,sizeof(buffer+byteswritten)); } printf("String : %s sent to server \n",buffer); } Sample Output: $./polltcpser

$./polltcpcli rama rama krishna krishna …….

Page 24: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

24

Week 9: Aim: C program to design a UDP Client & Server application to reverse a given input sentence. Program: /** Server */ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> #include<netinet/in.h> #include<sys/socket.h> int main(void) { int sockfd,n,i,j,k; socklen_t len; char mesg[100],revmesg[100]; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(51004); servaddr.sin_family=AF_INET; bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); len=sizeof(cliaddr); while((n=recvfrom(sockfd,mesg,100,0,(struct sockaddr *)&cliaddr,&len))>0) { k=n; for(j=0;j<=n-1;j++) { revmesg[k-1]=mesg[j]; k--; } sendto(sockfd,revmesg,n,0,(struct sockaddr *)&cliaddr,len); bzero(mesg,100); bzero(revmesg,100); } } /** Client */ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> #include<netinet/in.h>

Page 25: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

25

#include<sys/socket.h> int main(void) { int sockfd,n; socklen_t len; char mesg[100]; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_port=htons(51004); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr); len=sizeof(servaddr); while((n=read(fileno(stdin),(void *)mesg,100))>0) { sendto(sockfd,(void *)mesg,n,0,(struct sockaddr *)&servaddr,len); recvfrom(sockfd,mesg,n,0,(struct sockaddr *)&servaddr,&len); write(fileno(stdout),mesg,n); bzero(mesg,100); } bzero(mesg,100); close(sockfd); } Sample Output: $./udpser

$./udpcli rama amar krishna anhsirk …….

Page 26: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

26

Week 10: Aim: C program to design a UDP Client & Server to transfer a file Program: /** Server */ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> #include<netinet/in.h> #include<sys/socket.h> int main(void) { int sockfd,n,fptr; socklen_t len; char mesg[100],arg[100]; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(51004); servaddr.sin_family=AF_INET; bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); len=sizeof(cliaddr); fptr=creat("udperv.c",S_IRWXU); while((n=recvfrom(sockfd,(void *)mesg,100,0,(struct sockaddr *)&cliaddr,&len))>0) { write(fptr,(void *)mesg,n); bzero(mesg,100); } close(fptr); close(sockfd); exit(0); } /** Client */ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> #include<netinet/in.h> #include<sys/socket.h>

Page 27: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

27

int main(void) { int sockfd,n,fptr; socklen_t len; char mesg[100]; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_port=htons(51004); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr); len=sizeof(servaddr); fptr=open("ftcli.c",O_RDWR,0); while((n=read(fptr,(void *)mesg,100))>0) { sendto(sockfd,(void *)mesg,n,0,(struct sockaddr *)&servaddr,len); bzero(mesg,100); } bzero(mesg,100); close(sockfd); } Sample Output: $./udpfileser /*** Here server creates a new file and read the data from the client and save into the created file ****/

$./udpfilecli /*** Here client opens the file, and reads data of the file and sends that data to the client ****/

Page 28: Jntuk 4 1 Cse Network Programming Lab Manual

MIC

KANCHIK

ACHERLA

Devineni Venkata Ramana & Dr. Hima Sekhar

MIC College of Technology

28

CONTENTS S.No Name of the Experiment Page No.

Week 1 Implement the following forms of IPC. a)Pipes b)FIFO

1

Week 2 Implement file transfer using Message Queue form of IPC 5

Week 3 Write a programme to create an integer variable using shared memory concept and increment the variable simultaneously by two processes. Use senphores to avoid race conditions

7

Week 4 Design TCP iterative Client and server application to reverse the given input sentence

10

Week5 Design TCP concurrent Client and server application to reverse the given input sentence

12

Week6 Design TCP client and server application to transfer file 14

Week7 Design a TCP concurrent server to convert a given text into upper case using multiplexing system call “select”

16

Week8 Design a TCP concurrent server to echo given set of sentences using poll functions

19

Week9 Design UDP Client and server application to reverse the given input sentence

23

Week10 Design UDP Client server to transfer a file 25