0% found this document useful (0 votes)
2 views4 pages

Watch 3 File

The document contains a C program that sets up a TCP server to monitor three file descriptors: standard input, a FIFO file, and a socket. It creates a socket, binds it to a specified port, and listens for incoming connections while using the select function to handle input from the monitored files. The program reads input from the FIFO and standard input, and accepts client connections to receive messages.

Uploaded by

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

Watch 3 File

The document contains a C program that sets up a TCP server to monitor three file descriptors: standard input, a FIFO file, and a socket. It creates a socket, binds it to a specified port, and listens for incoming connections while using the select function to handle input from the monitored files. The program reads input from the FIFO and standard input, and accepts client connections to receive messages.

Uploaded by

rajprithiv944
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// monitoring 3 file discreptors

1. take fifo file


[Link] client.c file
3. this file
#include"header.h"
int main()
{
int sfd,nsfd,len,b,fd,ret,a;
char s1[20];
char s[20];
struct timeval t;
fd_set v;
struct sockaddr_in s_addr,c_addr;
/********** socket() ***************/
fd=open("f1",O_RDWR);
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
perror("Socket:");
return 0;
}
/*********** bind() ****************/
s_addr.sin_family=AF_INET;
s_addr.sin_port= htons(2000);
s_addr.sin_addr.s_addr=inet_addr("[Link]");
len=sizeof(s_addr);
b= bind(sfd,(struct sockaddr*) &s_addr,len);
if(b<0)
{
perror("Bind:");
return 0;
}
/**************** listen() **************************/
if( listen(sfd,5) <0)
{
perror("Listen:");
return 0;
}
printf("MOnitoring file ......\n");
FD_ZERO(&v);
FD_SET(0,&v); //stdin
FD_SET(fd,&v); // fifo
FD_SET(sfd,&v); // socket file
t.tv_sec=20;
t.tv_usec=0;
ret=select(sfd+1,&v,0,0,&t);
if(ret<0)
perror("Select:");
else if (ret==0)
{
printf("Time out");
}
else if(FD_ISSET(0,&v))
{
scanf("%d",&a);
printf("STDIN: %d\n",a);
}
else if(FD_ISSET(fd,&v))
{
read(fd,s1,sizeof(s1));
printf("FIFO:%s\n",s1);
}
else if(FD_ISSET(sfd,&v))
{
/****************** accept() ****************/
printf("waiting for the client incoming request\n" );
nsfd=accept(sfd,(struct sockaddr*)&c_addr,&len);
if(nsfd<0)
{
perror("Accept:");
return 0;
}
printf("client request Accepted\n");
printf("waiting for message from client\n");
read(nsfd,s,sizeof(s));
printf("client sent message: %s\n",s);
}
close(sfd);
close(nsfd);
}

You might also like