IPC using Unix Domain Sockets

2014-06-04T14:35:16

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 sever connection and if available sends the data. The server waits for a client connection and whenever some data is available, it reads the data and writes it into a file. The main functions for the client is :

CLIENT:
int main(int argc, char* argv[]) {

if (argc < 2) {
    printf("Usage: Operating_Mode Path_to_DAQ_dataset output_file cudaBF\n");
    printf("Operating_Mode = 0 : Real Time BF processing\nUsage: Operating_Mode output_file cudaBF\n");
    printf("Operating_Mode = 1 : Pre-recorded data BF processing\nUsage: Operating_Mode Path_to_DAQ_dataset output_file cudaBF\n");
    return 1;
}

/*******************************************/
int s, len , packetTransferCount;
struct sockaddr_un remote;

if ((s = socket(AF_UNIX,SOCK_STREAM, 0)) == -1) {
    perror("socket");
}

remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCKET_DISPLAY_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);    
/*******************************************/

pthread_t threadBF;

struct beamRecordedArgs processParams;
processParams.dataPath = argv[2];
processParams.bfOutputFile = argv[3];

pthread_create(&threadBF,NULL,beamProcessRecorded,(void *)&processParams);

packetTransferCount = 0;
while(processStatus != COMPLETE){
if(processStatus == TRANSFER){
    printf("Checking for display...\n");
    if (connect(s, (struct sockaddr *)&remote, len) == -1) {
        printf("No display process found\n");
        processStatus = PROCESS;
}
    else{
        printf("Display process found.. Transferring packet\n");
        send(s, txPackDisp, sizeof(txPackDisp), 0);
        close(s);
        processStatus = PROCESS;
        printf("PACKETS TRANSFERRED : %d\n",++packetTransferCount);
    }
}
else{
    while(processStatus == PROCESS){
        //sleep(1);
    }
}
}

pthread_join(threadBF,NULL);

return 0;
}

For server its :

SERVER:
int main(void)
{

int dispSock, lenSock, packetTransferCount;
struct sockaddr_un localDisp;

if ((dispSock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(-1);
}
localDisp.sun_family = AF_UNIX;
strcpy(localDisp.sun_path, SOCKET_DISPLAY_PATH);
unlink(localDisp.sun_path);
lenSock = strlen(localDisp.sun_path) + sizeof(localDisp.sun_family);
if (bind(dispSock, (struct sockaddr *)&localDisp, lenSock) == -1) {
    perror("bind");
    exit(-1);
}
if (listen(dispSock, 5) == -1) {
    perror("listen");
    exit(-1);
}

packetTransferCount = 0;

do{
    struct sockaddr_un remoteDisp;
    int dispSockCon,dispConn;
    dispConn = sizeof(remoteDisp);

    if ((dispSockCon = accept(dispSock, (struct sockaddr *)&remoteDisp, &dispConn)) == -1) {
        perror("accept");
        exit(-1);
    }
    printf("Connected.\n");

    int status = recv(dispSockCon, &rxPack, PACKET_SIZE*sizeof(float),0);

    if (status != -1){
        FILE *fp;
        fp = fopen("bfo_sock","w");
        fwrite(rxPack, PACKET_LENGTH, sizeof(float), fp);
        fclose(fp);
    }
    else{
        printf("Error in receiving packets\n");
    }
    close(dispSockCon);
}while(1);

close(dispSock);
unlink(SOCKET_DISPLAY_PATH);
return 0;
}

The problem that I am facing is, the client is only able to send one packet after it connects to the server (doesnt matter which is started first or is already running), after which its not detecting the server process at all, even when its running and other processes can detect it.Typically the size of data being transferred at once is roughly 13.3 kilobytes. The print outs that I see on the terminal is :

Number of packets to be processed in BF : 60
PACKETS PROCESSED : 1
Checking for display...
Display process found.. Transferring packet
PACKETS TRANSFERRED : 1
PACKETS PROCESSED : 2
Checking for display...
No display process found
PACKETS PROCESSED : 3
Checking for display...
No display process found

If someone can point me in the direction where I am going wrong here, it would really helpful for me as I am pretty new to IPC using sockets.

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

About “IPC using Unix Domain Sockets” questions

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...
I need to setup local IPC between client and server. It is a case of single server and multiple clients and data need to be exchanged in both directions. The client is a command which sends the com...

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