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