简体   繁体   English

Unix数据报套接字正在向自身而不是远程发送消息

[英]Unix datagram socket is sending message to itself instead of remote

I have a unix Datagram sockets opened between two processes running on same machine. 我在同一台机器上运行的两个进程之间打开了一个UNIX数据报套接字。 However, my client process is sending request to itself instead of server. 但是,我的客户进程正在向自身而不是服务器发送请求。

I have kept FilePath to which sockets bind different. 我保持套接字绑定的FilePath不同。 However, if I keep Filepath same then though client is able to send request to server but server sends response to itself. 但是,如果我保持Filepath不变,则尽管客户端能够向服务器发送请求,但是服务器向自身发送响应。 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 一对UNIX套接字客户端服务器之间无法交换消息,只能以rand交换消息

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. 我希望两个过程之间能够顺利进行消息交换。

You did not include a minimal reproducable exmaple; 您没有包括最小的可复制示例。 also it is unclear where datagramSocket comes from in the sender function. 同样不清楚sendgram函数中datagramSocket的来源。

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 C / Unix中的Socketpair()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM