Network Programming Lab Record
Network Programming Lab Record
a)pipes
b)FIFO
a)PIPES:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork()) {
exit(0);
} else {
1
read(pfds[0], buf, 5);
wait(NULL);
return 0;
2
OUTPUT:
3
b)FIFO:
PROGRAM:
client:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
char s[300];
fd = open(FIFO_NAME, O_WRONLY);
perror("write");
else
4
}
return 0;
Server :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
char s[300];
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writer\n");
do {
perror("read");
else {
s[num] = '\0';
5
printf("tick: read %d bytes: \"%s\"\n", num, s);
return 0;
6
OUTPUT:
7
WEEK-2
PROGRAM:
Client
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
int msqid;
key_t key;
message_buf sbuf;
size_t buf_length;
key = 1234;
perror("msgget");
8
exit(1);
else
sbuf.mtype = 1;
buf_length = strlen(sbuf.mtext) + 1 ;
perror("msgsnd");
exit(1);
else
exit(0);
9
Server:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
int msqid;
key_t key;
message_buf rbuf;
key = 1234;
{ perror("msgget");
exit(1); }
perror("msgrcv");
exit(1);
printf("%s\n", rbuf.mtext);
exit(0);
10
OUTPUT:
11
WEEK-3
Write a program to create an integer variable using shared memory concept and increment
the variable simultaneously by two processes. Use semaphores to avoid the race conditions
PROGRAM:
HEADER:
#include<stdlib.h>
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
SERVER:
#include "header.h"
int main()
int sid,mid, i;
char ch;
int *ptr;
struct sembuf s;
s.sem_num = 0;
s.sem_op = -1;
s.sem_flg = SEM_UNDO;
*ptr = 00;
12
semctl(sid, 0, SETVAL, 1));
*ptr = (*ptr) + 1;
sleep(5);
s.sem_op = 1;
CLIENT:
#include "header.h"
int main()
int sid,mid, i;
char ch;
int *ptr;
struct sembuf s;
s.sem_num = 0;
s.sem_op = -1;
s.sem_flg = SEM_UNDO;
semctl(sid, 0, GETVAL));
13
for(i=0; i<10; i++)
*ptr = (*ptr) + 1;
sleep(5);
s.sem_op = 1;
14
OUTPUT:
15
WEEK- 4 & 5
Design TCP iterative client and server application to reverse the given input sentence
PROGRAM:
HEADER:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
CLIENT:
#include "header.h"
int s,sal;
char buf[20];
if (argc != 2)
exit(1);
16
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(9734);
sal = sizeof(sa);
buf[n] ='\0';
close(s);
exit(0);
SERVER:
#include "header.h"
int main()
char buf[20];
sa.sin_family = AF_INET;
17
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sal = sizeof(sa);
listen(ss, 5);
cal = sizeof(ca);
while(1) {
buf[n] = '\0';
int i,j;
close (cs);
18
OUTPUT:
19
WEEK-6
PROGRAM:
HEADER:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <fcntl.h>
CLIENT:
#include "header.h"
main()
int s = socket(AF_INET,SOCK_STREAM,0);
sa.sin_port = htons(PORT);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
char buf[50];
int n;
while(( n =read(s,buf,50))>0 )
20
close(fd);
close(s);
SERVER:
#include "header.h"
main(){
int ss,cs;
ss = socket(AF_INET,SOCK_STREAM,0);
sa.sin_family = AF_INET;
sa.sin_port = htons(PORT);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
listen(ss,5);
int fd = open("server.txt",O_RDONLY,0);
char buf[50];
int n;
{write(cs,buf,n);}
close(fd);
return 0;
21
OUTPUT:
22
WEEK-9
HEADER:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
SERVER:
#include "week9h.h"
main()
int addr_len, n;
char buf[20];
sa.sin_family = AF_INET;
sa.sin_port = htons(MYPORT);
sa.sin_addr.s_addr = INADDR_ANY;
bzero(&(sa.sin_zero), 8);
23
bind(ss, (struct sockaddr *)&sa,sal );
while(1){
buf[n] = '\0';
int i,j;
close(ss); }
CLIENT:
#include "week9h.h"
int n;
if (argc != 2) {
exit(1);}
sa.sin_family = AF_INET;
sa.sin_port = htons(MYPORT);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
24
bzero(&(sa.sin_zero), 8);
char buf[20];
buf[n] = '\0';
close(s);
return 0;
25
OUTPUT:
26
WEEK-10
HEADER:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <fcntl.h>
SERVER:
#include "week10h.h"
int main()
int n;
sa.sin_family = AF_INET;
sa.sin_port = htons(MYPORT);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(sa.sin_zero), 8);
27
char fname[] = "aaaa";
char buf[20];
while(1){
if( n == 0 ) { break; }
close(fd); close(s);
return 0;
CLIENT:
#include "week10h.h"
main()
sa.sin_family = AF_INET;
sa.sin_port = htons(MYPORT);
sa.sin_addr.s_addr = INADDR_ANY;
bzero(&(sa.sin_zero), 8);
28
bind(ss, (struct sockaddr *)&sa, sizeof(struct sockaddr));
char buf[20];
int n;
while(1){
n = read(fd,buf,20);
if(n == 0) { break ; }}
close(fd); close(ss);
return 0;
29
OUTPUT:
30