0

I have a unix Datagram sockets opened between two processes running on same machine. However, my client process is sending request to itself instead of server.

I have kept FilePath to which sockets bind different. However, if I keep Filepath same then though client is able to send request to server but server sends response to itself. Please check this link for a question which is related to this question. There is almost full code there.

Not able to exchange message between a pair of unix socket client server to exchange messages randonly

Below is the problematic code snippet at client:

int msimcli_ConnectToSocket(const char* socketFile,int isSockTypeStreaming, SOCKET_PARAMS* socketParam)
{
   int rc = ROK;
   int result = 0;                                                                                                      
   char      buffer[CLI_MAX_BUF_SIZE];                                                                                  
   int       buffer_size = CLI_MAX_BUF_SIZE;                                                                            
   int                option = 1;                                                                                            
   socklen_t len;
   MSIM_ZERO(*socketParam);                                                                                             
   pthread_t snd_tid;                                                                                                   
   pthread_t rx_tid;
   if (isSockTypeStreaming)                                                                                             
   {
      socketParam->type = SOCK_STREAM;                                                                                  
   }
   else
   {
      socketParam->type = SOCK_DGRAM;                                                                                   
   }
   socketParam->fd = socket(AF_UNIX, socketParam->type, 0);                                                             
   if (0 > socketParam->fd)                                                                                             
   {
      rc = RFAILED;                                                                                                     
      goto Exit;
   }
   else{
      printf("socket created successfully with socket descriptor %d\n",socketParam->fd); 
      }
   rc = setsockopt(socketParam->fd, SOL_SOCKET, (SO_REUSEADDR), &option, sizeof(option));                               
   if (-1 == rc)
   {
      printf("setsockopt failed\r\n");                                                                                  
      close(socketParam->fd);                                                                                           
      socketParam->fd = -1;                                                                                             
      goto Exit;
   }
   /* Bind Unix socket to a FilePath */
   socketParam->remote.sun_family = AF_UNIX;                                                                            
   unlink(socketParam->remote.sun_path);                                                                                
   strcpy(socketParam->remote.sun_path, socketFile);
   socketParam->len = strlen(socketParam->remote.sun_path) + sizeof(socketParam->remote.sun_family) + 1;                
   rc = bind(socketParam->fd, (struct sockaddr*)&socketParam->remote, socketParam->len);                                
   if (-1 == rc)                           
   {
      printf("setsockopt failed\r\n");                                                                                  
      close(socketParam->fd);                                                                                           
      socketParam->fd = -1;                                                                                             
      goto Exit;
   }
   /* Create Receiver thread */                                                                                         
   if(ROK != (rc = pthread_create(&rx_tid,NULL,msimcli_RecvFromSocket,NULL)))                                           
   {
       printf("Thread create for Receiver failed\n");                                                                   
       goto Exit;                                                                                                       
   }
Exit:
   if (ROK != rc)
   {                                                                                                                    
      printf("%s: errno=0x%x %s\r\n", __FUNCTION__,errno, strerror(errno));   
            if (-1 < socketParam->fd)                                                                                         
      {                                                                                                                 
         close(socketParam->fd);                                                                                        
         socketParam->fd = -1;                                                                                          
      }                                                                                                                 
   }
   printf("<< rc %d\r\n", rc);                                                                                          
   return rc;                                                                                                           
}

int msimcli_SendToSocket(void* buf)                                                                                     
{                                                                                                                       
   int rc = ROK;                                                                                                        
   /*Test buffer*/                                                                                                      
   cliCmd *buff = (cliCmd *)buf;                                                                                        
   printf("Buff %s\n", ((cliCmd *)buf)->buf);                                                                           
   for (int i = 0; i < buff->hdr.msglen; i++)                                                                           
   {                                                                                                                    
    printf("[%x]", buff[i]);                                                                                            
   }                                                                                                                    
   printf("\n");                                                                                                        
   printf("sending on socket [%d]\n",datagramSocket.fd);                                                                
   rc = sendto(datagramSocket.fd, buf, buff->hdr.msglen, 0, \                                                           
         (struct sockaddr *)&datagramSocket.remote, datagramSocket.len);                                                
   if (rc == -1) {                                                                                                      
      printf("%s: errno=0x%x %s\r\n", __FUNCTION__,errno, strerror(errno));                                             
      printf("SENDTO ERROR\n");                                                                                         
      close(datagramSocket.fd);                                                                                         
      return 0;                                                                                                         
    }                                                                                                                    
   else {                                                                                                               
      printf("Data sent!\n");                                                                                           
      return rc;                                                                                                        
   }                                                                                                                    
}   

I expect a smooth message exchange between two processes.

  • Also, if I do not bind client with any PathName at all, I can still send message from client to server but server sends response to itself instead of client. Also, I see only one DGRAM socket in my netstat output. But if I bind my client also with a PathName I see two DGRAM sockets in netstat output. – Adarsh Srivastava Jul 12 '19 at 17:35

1 Answers1

0

You did not include a minimal reproducable exmaple; also it is unclear where datagramSocket comes from in the sender function.

however, to me it seems both sender and receiver use the exact same socket.

what you need to do is use a pair of sockets; that is a socketpair:

Socketpair() in C/Unix

nivpeled
  • 1,810
  • 5
  • 17