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