/*SERVER PROGRAM*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct sockaddr_in add2,add1;
socklen_t len,len1;
int i,j,k,l,z;
int fm2[10];
int fd[2];
int ff1,ff2;
int status;
int pid,n;
int sock2;
void sigchld_handler()
{
read(fd[0],&j,sizeof(int));
read(fd[0],&k,sizeof(int));
i--;
fm2[j]=0; //SETTING FIFO STATUS 0 FOR IDLE 1 FOR BUSY
fm2[k]=0; //SETTING FIFO STATUS 0 FOR IDLE 1 FOR BUSY
printf("\nStatus Array at exit and i is %d\n",i);
for(l=0;l<10;l++)
printf("%d\n",fm2[l]);
}
void sigint_handler()
{
printf("\nSERVER is being terminated........\n");
if(pid!=getpid())
{
printf("\nClient Process with pid is %u Terminating\n",getpid())
;
n=1;
write(ff2,&n,sizeof(int));
sleep(1);
n=0;
write(sock2,&n,sizeof(int));
shutdown(sock2,SHUT_RDWR);
}
sleep(1);
exit(0);
}
void sigpipe_handler()
{
write(fd[1],&j,sizeof(int));
write(fd[1],&k,sizeof(int));
exit(0);
}
main()
{
int bin,sock,tmp;
char ch='t';
char fm1[10][3]={"f0","f1","f2","f3","f4","f5","f6","f7","f8","f9"};
//HANDLING CHILD PROCESS TERMINATION WHICH IS KILLED OR STOPPED
signal(SIGCHLD,sigchld_handler);
//HANDLING CTRL+Ci
signal(SIGINT,sigint_handler);
signal(SIGPIPE,sigpipe_handler);
signal(SIGQUIT,sigint_handler);
//PIPE CREATION FOR SENDING THE FIFO ARRAY STATUS
pipe(fd);
//SERVER SOCKET CREATION
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
perror("Server Socket Creation Error\n");
exit(1);
}
add1.sin_port=htons(4500);
add1.sin_addr.s_addr=INADDR_ANY;
add1.sin_family=AF_INET;
//BINDING SERVER SOCKET.
if(bind(sock,(struct sockaddr *)&add1,sizeof(add1))<0)
{
perror("Binding Error\n");
exit(1);
}
//LISTENING FOR CONNECTIONS.
i=listen(sock,5);
if(i<0)
{
perror("Listen error\n");
exit(1);
}
pid=getpid();
printf("\nServer Process id is %u\n",getpid());
//SETTING FIFO ARRAY STATUS AS IDLE BEFORE ACCEPTING CLIENT CONNECTIONS
for(i=0;i<10;i++)
fm2[i]=0;
//i VALUE MAINTAINS NUMBER OF CONNECTIONS
i=0;
while(1)
{
if(i<5)
{
//ACCEPT SIGNAL
sock2=accept(sock,(struct sockaddr *)&add2,&len1);
if(sock2<0)
{
perror("Accept Error\n");
exit(1);
}
// printf("\nI value is %d\n",i);
//i VALUE IS INCREMENTED AS A NEW CLIENT CONNECTION ACCEPTED.
i++;
//FIFO MANAGEMENTSTARTING
j=0;
while(fm2[j]!=0)
{
j++;
}
fm2[j]=1;
k=0;
while(fm2[k]!=0)
{
k++;
}
fm2[k]=1;
printf("\nStatus Array before and i is %d\n",i);
for(l=0;l<10;l++)
printf("%d\n",fm2[l]);
//FIFO MANAGEMENT CLOSED
//SPAWNING A NEW PROCESS FOR EACH AND EVERY NEW CLIENT CONNECTION
pid=fork();
if(pid==-1)
{
perror("FORK error\n");
}
//EXECUTING THE CHILD PROCESS
if(pid==0)
{
//SENDING THE FIFO NAMES TO CLIENT THROUGH WHICH COMMUNICATION HAS TO BE DONE.
write(sock2,&fm1[j],2);
write(sock2,&fm1[k],2);
ff1=open(fm1[j],O_RDONLY);
if(ff1==-1)
{
perror("OPENING FIRST FIFO\n");
}
ff2=open(fm1[k],O_WRONLY);
if(ff2==-1)
{
perror("OPENING SECOND FIFO\n ");
}
//SERVER DATA INTERFACE MODULE STARTS.
printf("pid is %u\n",getpid());
while(1)
{
read(ff1,&ch,sizeof(char));
//ENCRYPTION PART STARTS
// ch=ch ^ 'a' ;
//ENCRYPTION PART ENDS
n=1;
write(sock2,&n,sizeof(int));
if(ch==0)
{
break;
}
sleep(1);
write(ff2,&ch,sizeof(char));
}
//SERVER DATA INTERFACE MODULE ENDS.
close(ff1);
close(ff2);
write(fd[1],&j,sizeof(int));
write(fd[1],&k,sizeof(int));
// write(fd[1],&z,sizeof(int));
shutdown(sock2,SHUT_RDWR);
exit(0);
}
}
}
shutdown(sock,SHUT_RDWR);
}
/*CLIENT PROGRAM*/
#include
#include
#include
#include
#include
#include
#include
#include
struct sockaddr_in add;
void onerror(char *);
int sd,i,n;
int fd1, fd2;
int ff1,ff2,z;
char *j;
char ch='a';
void sigint_handler()
{
i=0;
write(ff1,&i,sizeof(int));
close(fd1);
close(fd2);
close(ff1);
close(ff2);
unlink(j);
shutdown(sd,SHUT_RDWR);
printf("\nChild process terminated because of SIGINT\n");
exit(0);
}
void sigpipe_handler()
{
printf("\nPIPE BROKEN\n");
exit(0);
}
main(int argc, char *argv[])
{
int bin,tmp,i=0,jk;
char f1n[3],f2n[3];
char s[10], *k=".";
//Signal Mechanisms for ctrl+c i.e SIGINT
signal(SIGINT,sigint_handler);
//Signal Mechanisms for broken pipe i.e SIGPIPE
signal(SIGPIPE,sigpipe_handler);
//Signal Mechanisms for ctrl+\ i.e SIGQUIT
signal(SIGQUIT,sigint_handler);
//Checking for number of input arguments provided during runtime.
if(argc<3)
{
printf("No Sufficient arguments supplied \n");
printf("Please provide full path for the file\n");
exit(0);
}
//CREATING CLIENT SOCKET
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("Client Socket Creation Error\n");
exit(1);
}
add.sin_port=htons(4500);
add.sin_addr.s_addr=inet_addr("127.0.0.1");
add.sin_family=AF_INET;
//CONNECTING CLIENT SOCKET
if(connect(sd,(struct sockaddr *)&add,sizeof(add))<0)
{
perror("Connection Error\n");
exit(1);
}
for(i=0;i<3;i++)
{
f1n[i]='\0';
f2n[i]='\0';
}
//READING FIFO NAMES FROM SERVER THROUGH WHICH COMMUNICATION PROCEEDS IN FUTURE.
if(read(sd,f1n,2)<0)
{
onerror("FIFO1 Error\n");
}
if(read(sd,f2n,2)<0)
{
onerror("FIFO2 Error\n");
}
printf("FIFO1 is %s\n",f1n);
printf("FIFO2 is %s\n",f2n);
//OPENING SENDER FIFO THROUGH WHICH DATA IS SENT TO SERVER
ff1=open(f1n,O_RDWR);
if(ff1==-1)
{
onerror("FIFO1 opening ERROR");
}
//OPENING RECEIVER FIFO THROUGH WHICH CLIENT RECEIVES ENCRYPTED DATA
ff2=open(f2n,O_RDWR);
if(ff2==-1)
{
onerror("FIFO2 opening ERROR\n");
}
//OPENING THE SOURCE FILE WHICH CONTAINS DATA TO BE ENCRYPTED
fd1=open(argv[1],O_RDONLY);
if(fd1<0)
{
onerror("Input File doesn't Exist\n");
}
//Mechanism to produce output filename as .
i=getpid();
z=strlen(argv[2]);
j=(char *)malloc(z);
strcpy(j,argv[2]);
sprintf(s,"%d",i);
strcat(j,k);
strcat(j,s);
//j contains output file name
//OPENING OUTPUT FILE INTO WHICH ENCRYPTED DATA IS STORED.
fd2=open(j,O_WRONLY|O_CREAT,0666);
if(fd2<0)
{
onerror("Opening Encryption File Error\n");
}
while(1)
{
// printf("\nSTART\n");
z=read(fd1,&ch,sizeof(char));
if(z==-1)
{
onerror("Read from Input file Error\n");
}
if(z==0)
{
write(ff1,&z,sizeof(int));
break;
}
else if(write(ff1,&ch,sizeof(char))<0)
{
onerror("Write into FIFO Error\n");
}
// printf("\nMIDDLE\n");
if(read(sd,&n,sizeof(int))<0)
{
onerror("\nRead from Socket Error\n");
}
if(n!=0)
{
// printf("\nInside If\n");
if(read(ff2,&ch,sizeof(char))<0)
{
onerror("Read from FIFO Error\n");
}
if(write(fd2,&ch,sizeof(char))<0)
{
onerror("Write into Encrypted File Error\n");
}
}
else
{
printf("\nInside else\n");
close(fd1);
close(fd2);
close(ff1);
close(ff2);
unlink(j);
exit(0);
}
// printf("\nEND\n");
}
close(fd1);
close(fd2);
close(ff1);
close(ff2);
shutdown(sd,SHUT_RDWR);
printf("\n\nSocket connection with Server closed Successfully\n");
}
void onerror(char *msg)
{
printf("\nERROR!! %s\n",msg);
shutdown(sd,SHUT_RDWR);
printf("\nSocket Connection Closed Abruptly\n");
exit(0);
}
Sunday, August 30, 2009
Subscribe to:
Posts (Atom)