IPC mechanism for telephone conversation using Unix domain sockets

2014-03-19T05:09:10

I am implementing the IPC for a telephonic conversation.Created two files :receiver.c and caller.c.. The rc.h contains all the include files.I am testing out the use of Unix domain sockets according to FIFO's .. The caller.c file is :

#include "rc.h"
int main()
{
    int sock_fd;
    struct sockaddr_un unix_addr;
    char buf[2048];
    int n;
    if ((sock_fd=socket(AF_UNIX,SOCK_STREAM,0))<0)
    {
        perror("cli:socket()");
        exit(1);
    }
    unix_addr.sun_family=AF_UNIX;
    strcpy(unix_addr.sun_path,SERVER);
    if (connect(sock_fd,(struct sockaddr *) &unix_addr,
        sizeof(unix_addr.sun_family)+
        sizeof(unix_addr.sun_path))< 0)
    {
        perror("cli:connect()");
        exit(1);
    }
    sprintf(buf,"Reciever called by receiver");
    n=strlen(buf)+1;
    if (write (sock_fd,buf,n)!=n)
    {
        perror("cli:write()");
        exit(1);
    }
    printf("Caller sent-->%s",buf);
    if ((n=read(sock_fd,buf,2047))<0)
    {
        perror("cli:read()");
        exit(1);
    }
    buf[n]=0;
    while (buf[n]==0){n--;}
    if (buf[n]=='\n')
        buf[n]='\0';
    printf("Reciever received<--%s \n",buf);
    exit(0);
}

The receiver.c is:

#include <signal.h>
#include "rc.h"

static void stop(int n)
{
    unlink(SERVER);
    exit(0);
}

static void receiver()
{
    int sock_fd,cli_sock_fd;
    struct sockaddr_un unix_addr;
    char buf[2048];
    int n,addr_len;
    pid_t pid;
    char *pc;

    signal(SIGINT,stop);
    signal(SIGQUIT,stop);
    signal(SIGTERM,stop);
    unix_addr.sun_family=AF_UNIX;
    strcpy(unix_addr.sun_path,SERVER);
    addr_len=sizeof(unix_addr.sun_family)+strlen(unix_addr.sun_path);
    unlink(SERVER);
    if (bind(sock_fd,(struct sockaddr *)&unix_addr,addr_len)<0)
    {
        perror("Receiver:bind()");
        exit(1);
    }
    if (listen(sock_fd,5)<0)
    {
        perror("Receiver:caller()");
        unlink(SERVER);
        exit(1);
    }
    while ((cli_sock_fd=accept(sock_fd,(struct sockaddr*)&unix_addr,&addr_len))>=0)
    {
        if ((n=read(cli_sock_fd,buf,2047))<0)
        {
            perror("Receiver:read()");
            close(cli_sock_fd);
            continue;
        }
        buf[n]='\0';
        for(pc=buf;*pc!='\0' && (*pc<'0' || *pc>'9');pc++);
        pid=atol(pc);
        if (pid!=0)
        {
            sprintf(buf,"Receiver called by caller\n",pid);
            n=strlen(buf)+1;
            if (write(cli_sock_fd,buf,n)!=n)
                perror("Receiver:write()");
        }
        close(cli_sock_fd);
    }
    perror("Receiver:accept()");
    unlink(SERVER);
    exit(1);
}

int main()
{
    int r;
    if ((r=fork())==0)
        receiver();
    if(r<0)
    {
        perror("Receiver:fork()");
        exit(1);
    }
    exit(0);
}

Now the problem occurs while compiling where i am getting:

sourajyoti@ubuntu:~/os2$ gcc receiver.c -o test1
receiver.c: In function ‘stop’:
receiver.c:6:9: warning: unknown escape sequence: '\s' [enabled by default]
receiver.c: In function ‘receiver’:
receiver.c:23:28: warning: unknown escape sequence: '\s' [enabled by default]
receiver.c:25:9: warning: unknown escape sequence: '\s' [enabled by default]
receiver.c:34:10: warning: unknown escape sequence: '\s' [enabled by default]    
receiver.c:58:9: warning: unknown escape sequence: '\s' [enabled by default]

Then after this:

sourajyoti@ubuntu:~/os2$ ./test1
Receiver:bind(): Bad file descriptor

Same for client:

sourajyoti@ubuntu:~/os2$ gcc caller.c -o test2    
caller.c: In function ‘main’:    
caller.c:15:28: warning: unknown escape sequence: '\s' [enabled by default]    
sourajyoti@ubuntu:~/os2$ ./test2    
cli:connect(): No such file or directory

The header file rc.h is:

     #include <sys/types.h>
     #include <sys/socket.h>
     #include <sys/un.h>
     #include <sys/ipc.h>
     #include <sys/msg.h>
     #include <stdio.h>
     #include <errno.h>
     #include <stdlib.h>
     #include <unistd.h>
     #define SERVER "\tmp\server"

Copyright License:
Author:「Paku」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/22491110/ipc-mechanism-for-telephone-conversation-using-unix-domain-sockets

About “IPC mechanism for telephone conversation using Unix domain sockets” questions

I am implementing the IPC for a telephonic conversation.Created two files :receiver.c and caller.c.. The rc.h contains all the include files.I am testing out the use of Unix domain sockets accordin...
I need to set up some relatively simple IPC for Mac and it needs to be done using UNIX domain sockets. I'm brand new to Swift so while I know this is possible I'm struggling even create my client and
I am planning to use unix domain sockets for my IPC (inter process communication) between two processes running on same host machine. But I have to look into data security also before choosing unix
I am using AF_UNIX,SOCK_STREAM socket for IPC between 2 different processes. The client is sending data over the socket which the server picks up and processes. The size of each block of data that ...
Is it better to use POSIX message queues or Unix domain sockets for local IPC communication? I have worked with Unix sockets between machines (not domain) and I remember that making and breaking the
I want to communicate between NodeJS and a C program using node-ipc, over a Unix socket, which according to that homepage is the fastest option. (They will be on the same machine). That package cla...
I have two different applications where one of them has to feed data into the other. I am using Unix domain sockets for communicating between them. The client hooks onto the socket, checks for a se...
I want to implement IPC in a Cocoa application using UNIX domain sockets, with which I have no experience. I found Apple's CFLocalServer example project, but it's written in C and looks, well, fai...
Android SDK provides LocalSocket objects for IPC over Unix Domain Sockets, but does so using Linux abstract namespaces. As such, any process can bind to the server socket, where standard Unix sockets
I'm running a client/server application on Red Hat Enterprise using ZMQ for message passing. The IPC socket used to associate a client with the server is implemented using a Unix domain socket. If...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.