本文介绍了Unix数据报套接字正在向自身而不是远程发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一台机器上运行的两个进程之间打开了一个UNIX数据报套接字.但是,我的客户端进程正在向自身而不是服务器发送请求.

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.

我使套接字绑定到的FilePath保持不同.但是,如果我保持Filepath不变,则尽管客户端能够向服务器发送请求,但是服务器向自身发送响应.请检查此链接以查找与此问题相关的问题.那里几乎有完整的代码.

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.

无法在一对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.

推荐答案

您没有包括最小的可复制示例.同样不清楚sendgram函数中datagramSocket的来源.

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:

C/Unix中的Socketpair()

这篇关于Unix数据报套接字正在向自身而不是远程发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 10:58
查看更多