什么可以导致`recvfrom()`不设置

时间:2015-04-04 11:20:33

标签: c sockets posix unix-socket

所以我使用Unix Domain Socket(数据报)在我的机器上的两个进程之间进行通信。 客户端服务器发送查询,该服务正在进行一些计算并将结果返回给客户端

我的服务器使用recvfrom()从客户端接收查询,我的问题是,在第一次调用recvfrom()时,它没有设置struct sockaddr *restrict address这是地址发件人。

第一次调用recvfrom()时发生 ,以下查询完全正常。

我在这里做错了什么?

client.c:

/* relevant code from client.c */

sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);

memset(&client, 0, sizeof(client));
client.sun_family = AF_UNIX;
strcpy(client.sun_path, CLIENT_PATH);

bind(sockfd, (struct sockaddr *)&client, sizeof(struct sockaddr_un);

memset(&server, 0, sizeof(server));
server.sun_family = AF_UNIX;
strcpy(server.sun_path, SERVER_PATH);

while(1) {

  FD_ZERO(&readfds);
  FD_SET(sockfd, &readfds);
  FD_SET(STDIN, &readfds);

      select(sockfd+1, &readfds, NULL, NULL, NULL);


      if (FD_ISSET(STDIN, &readfds)) {
          nbytes = read(STDIN, buf, sizeof buf);
          buf[nbytes] = '\0';
          if (sendto(sockfd, buf, strlen(buf), 0,
                  (struct sockaddr *)&server,
                  sizeof(struct sockaddr_un)) < strlen(buf)) {
              perror("send");
          }
      }

      if (FD_ISSET(sockfd, &readfds)) {
          if ((nbytes = recvfrom(sockfd, buf, MAXDATASIZE, 0, NULL, NULL)) < 0) {
              perror("recv");
          }
          buf[nbytes] = '\0';
          printf(">> %s", buf);
      }
}

server.c

/* relevant code from server.c */


FD_ZERO(&master);
FD_ZERO(&read_fds);


  if ((client = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
      perror("client socket");
      exit(3);
  }

  memset(&server, 0, sizeof(struct sockaddr_un));
  server.sun_family = AF_UNIX;
  strcpy(server.sun_path, SOCK_PATH);

  unlink(server.sun_path);
  if (bind(client, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
      perror("uds bind");
      exit(3);
  }

  FD_SET(client, &master);

  // keep track of the biggest file descriptor
  fdmax = client; //so far, it's this one

  // main loop
  for(;;) {
      read_fds = master; // copy it
      if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
          perror("select");
          exit(4);
      }

      // run through the existing connections looking for data to read
      for(i = 0; i <= fdmax; i++) {
          if (FD_ISSET(i, &read_fds)) { // we got one!!
                  if (i == client) {
                      // data is from client console
                      if ((nbytes = recvfrom(i, &buf, sizeof buf, 0, (struct sockaddr *)&client_addr, &remotelen)) <= 0) {
                          if (nbytes == 0) {
                              FD_CLR(i, &master);
                              close(i);
                          } else {
                              perror("client: recv");
                          }
                      }

                      buf[nbytes-1] = '\0';
                      if (strcmp(buf, "command-1") == 0) {
                          if ((nbytes = sendto(i, cmd1_str, strlen(cmd1_str), 0, (struct sockaddr *)&client_addr, remotelen)) < 0) {
                              perror("sendto");
                          }
                          buf[0] = '\0';
                      } else {
                          sendto(i, unknown_str, strlen(unknown_str), 0, (struct sockaddr *)&client_addr, remotelen);
                          buf[0] = '\0';
                      }
                  }     
              } 
          } // END got new incoming connection
      } // END looping through file descriptors
  } // END for(;;)--and you thought it would never end!

代码被修剪为仅包含应该重要且相关的内容。如果遗漏任何重要内容,请告诉我,我会添加它。

0 个答案:

没有答案