0% found this document useful (0 votes)
32 views2 pages

Lab Practice

This document contains code for a client and server program that communicate over named pipes (FIFOs) to transfer files. The client program creates two FIFOs, connects to one to send a file path to the server, and the other to receive the file contents sent back from the server. The server program creates the same two FIFOs, listens on one for path requests, opens the requested file, reads its contents, and writes it back to the client over the second FIFO.

Uploaded by

gadha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Lab Practice

This document contains code for a client and server program that communicate over named pipes (FIFOs) to transfer files. The client program creates two FIFOs, connects to one to send a file path to the server, and the other to receive the file contents sent back from the server. The server program creates the same two FIFOs, listens on one for path requests, opens the requested file, reads its contents, and writes it back to the client over the second FIFO.

Uploaded by

gadha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File: /home/prateek/Documents/nw-lab/practice/client.

c Page 1 of 1

#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(int argc,char *argv[])
{
char p[100],c[3000];
int fd1,fd2;

mknod("fifo1",S_IFIFO|0666,0);
mknod("fifo2",S_IFIFO|0666,0);

printf("Client online\n");
printf("\n waiting for server...\n");

fd1=open("fifo1",O_RDWR);

printf("\n SERVER ONLINE !\n "


"CLIENT:Enter the path\n");
gets(p);
while(!feof(stdin))
{
write(fd1,p,strlen(p));
printf("Waiting for reply....\n");
fd2=open("fifo2",O_RDWR);
read(fd2,c,3000);
printf("File received! displaying the contents:\n");
fputs(c,stdout);
exit(1);
}
}
File: /home/prateek/Documents/nw-lab/practice/server.c Page 1 of 1

#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(int argc,char *argv[])


{
char p[100],c[300],ch;
int f1,fd1,fd2,i=0;

mknod("fifo1",S_IFIFO |0666,0);
mknod("fifo2",S_IFIFO |0666,0);

printf("\nSERVER ONLINE");
fd1=open("fifo1",O_RDONLY);
printf("client online\nwaiting for request\n\n");
while(1)
{
read(fd1,p,100);
f1=open(p,O_RDWR);
printf("\nserver:%s found\n"
"tranfering the contents",p);
stdin=fdopen(f1,"r");
while((ch=getc(stdin))!=EOF)
{
c[i++]=ch;
}
c[i]='\0';
printf("\nfile contents are:\n%s\n ",c);
fd2=open("fifo2",O_RDWR);
write(fd2,c,strlen(c));
printf("\nserver:transfer completed");
exit(1);
}
}

You might also like