IPC using Unix Domain Sockets

前端 未结 1 588
刺人心
刺人心 2021-01-25 00:33

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 soc

相关标签:
1条回答
  • 2021-01-25 00:53

    Well, I figured out the problem here. The issue was that I needed to create the socket connection anew everytime I wanted to transfer a packet over and close it after that. So, main for the server remains the same, but the client now becomes :

    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 packetTransferCount;
    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){
    /****************************************************************/ 
    /*This part was outside the loop, moving it inside creates a new 
    socket connector everytime I was to transfera packet and I close it before 
    the loops ends */       
        int s, len;
    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);    
    /****************************************************************/  
        printf("Checking for display...\n");
        int dispConStatus = connect(s, (struct sockaddr *)&remote, len);
        if ( dispConStatus == -1) {
            printf("No display process found\n");
            processStatus = PROCESS;
        }
        else{
            printf("Display process found.. Transferring packet\n");
            send(s, txPackDisp, sizeof(txPackDisp), 0);
            processStatus = PROCESS;
            printf("PACKETS TRANSFERRED : %d\n",++packetTransferCount);
        }
        close(s);
    }
    else{
        while(processStatus == PROCESS){
            //sleep(1);
        }
    }
    }
    
    pthread_join(threadBF,NULL);
    
    return 0;
    }
    
    0 讨论(0)
提交回复
热议问题