0

I want to create single side chat application using c and when reader(client) presses Ctrl+c, writer(server) should be closed.But problem is still I cant figure out how to sent signal to writer when reader presses Ctrl+c.

Please give any suggestion for this...

This is reader side code :

 #include<fcntl.h>
 #include<stdio.h>
 #include<sys/stat.h>
 #include<sys/types.h>
 #include<unistd.h>
 #include<string.h>
 #include<stdlib.h>
 #include<signal.h>
 #define max 1990
 int fu;
 void sigHadeler();

    int main(){
       struct stat sb;


       int fd;
       char* fifo="/home/man/shellScript/fifo";
       char buf[max];
       struct sigaction new_action, old_action;
       new_action.sa_handler = sigHadeler;
       sigemptyset (&new_action.sa_mask);
       new_action.sa_flags = 0; 
           if( sigaction (SIGINT, NULL, &old_action) == -1) 
         perror("Failed to retrieve old handle"); /Ctrl + C */

     if( sigaction (SIGINT, &new_action, NULL) == -1)/* set the new action */
      ("Failed to set new Handle");

             printf("Start conversation....\n");
      while(1){

     while(1){
       stat(fifo,&sb);

       if((sb.st_mode & S_IFMT)==S_IFIFO){

        fd=open(fifo,O_RDONLY);
        int re =read(fd,buf,max);
        buf[re]='\0';
        printf("read: %s\n",buf);

        close(fd);

    break;
    }
     }

      }

    return 0;
   }

    void sigHadeler(){
    char* fifo="/home/man/shellScript/fifo";
    printf("What have you done \n");
    write(fu,"0",strlen("0"));
    close(fu);
    unlink(fifo);
    exit(1);
    }

This is writer side code :

#include<fcntl.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>
#define max 1990

int main(){
    int fd;
    char* input;
    input = malloc(sizeof(char)*1000);
    char* fifo="/home/man/shellScript/fifo";
    printf("Enter string :");
    struct stat sb;
    mkfifo(fifo,0666);

        while(1){

        printf("Writing..\n");
        fgets(input,max,stdin);
        input[strlen(input)-1]='\0';
        fd=open(fifo,O_WRONLY| O_NONBLOCK);
        if(fd<=0){printf("Reader is closed or no readers ...\n");
            exit(1);    
        }

        write(fd,input,strlen(input));
        close(fd);
        while(1){
            stat(fifo,&sb);
            if((sb.st_mode & S_IFMT)==S_IFIFO){
            close(fd);
        }
        break;
    }
    }
unlink(fifo);
return 0;
}
Deep Shah
  • 293
  • 5
  • 21
GPrathap
  • 7,336
  • 7
  • 65
  • 83
  • Instead of opening and closing every time, why not put the open and close outside the loop and check the result of the write. Does it need to be non-blocking? – cup Jun 02 '14 at 06:00
  • Yes it can be done. and O_NONBLOCK flag is not needed ,But problem is when press Ctrl+c how to send it to writer ? – GPrathap Jun 02 '14 at 06:04
  • still I can't figure out how to use SIGPIPE signal (; – GPrathap Jun 02 '14 at 06:05
  • 1
    You don't need to - the kernel will send the writer a sigpipe when the reader disconnects. Similarly, the reader will be sent a sigpipe when the writer disconnects. You should see this if you aren't opening and closing all the time. – cup Jun 02 '14 at 06:24
  • If you just need it for `ctrl-c` why not catch `SIGINT` and send a final special message through the pipe? – Fantastic Mr Fox Jun 02 '14 at 06:25
  • 1
    If the client and server are on different machines, you can't send a signal between the two machines. You could use out-of-band (OOB) to propagate an urgent message from client to server (see [`sendmsg()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html)), or you can rely on the server getting a message (SIGPIPE) if it tries to write to the child after the child has disconnected (it will get EOF — zero bytes available to read — if it tries to read from the child after the child has disconnected). It can take a couple of minutes to spot the disconnection. – Jonathan Leffler Jun 02 '14 at 06:28
  • Your reader code writes to file descriptor `fu` which is never explicitly initialized so it contains 0 which is equivalent to standard input. Note, too, that it is not officially safe to use `printf()` in a signal handler function. – Jonathan Leffler Jun 02 '14 at 06:31

0 Answers0