gethostbyaddr上的错误:成功

时间:2016-09-26 05:09:47

标签: c sockets network-programming udp

我正在c中构建一个多客户端UDP服务器,但当我尝试从系统连接到我的服务器时,我收到此错误 gethostbyaddr上的错误:成功 请找到下面的服务器代码。我已尝试过类似问题的解决方案(gethostbyaddr() returns NULL but errno result in SUCCESS),但我无法正常工作。非常感谢任何帮助

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSIZE 1024

/*
* error - wrapper for perror
*/
 void error(char *msg) {
  perror(msg);
  exit(1);
}

int main(int argc, char **argv) {
  int sockfd; /* socket */
  int portno; /* port to listen on */
  int clientlen; /* byte size of client's address */
  struct sockaddr_in serveraddr; /* server's addr */
  struct sockaddr_in clientaddr; /* client addr */
  struct hostent *hostp; /* client host info */
  char buf[BUFSIZE]; /* message buf */
  char *hostaddrp; /* dotted decimal host addr string */
  int optval; /* flag value for setsockopt */
  int n; /* message byte size */
  FILE *fp; /* file variable */
  char str[10];
  int i = 0;
  char userlist[10];
  int array_size;
  char line[256];
  int cred,flag;

  /*
  * check command line arguments
  */
  if (argc != 2) {
     fprintf(stderr, "usage: %s <port>\n", argv[0]);
     exit(1);
  }
  portno = atoi(argv[1]);

  /*
  * socket: create the parent socket
  */
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");

  /* setsockopt: Handy debugging trick that lets
  * us rerun the server immediately after we kill it;
  * otherwise we have to wait about 20 secs.
  * Eliminates "ERROR on binding: Address already in use" error.
  */
  optval = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
        (const void *)&optval , sizeof(int));

  /*
  * build the server's Internet address
  */
  bzero((char *) &serveraddr, sizeof(serveraddr));
  memset(&serveraddr,0,sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serveraddr.sin_port = htons((unsigned short)portno);

  /*
  * bind: associate the parent socket with a port
  */
  if (bind(sockfd, (struct sockaddr *) &serveraddr,
     sizeof(serveraddr)) < 0)
     error("ERROR on binding");

  /*
  * main loop: wait for a datagram, then echo it
  */
  clientlen = sizeof(clientaddr);
  while (1) {

   /*
   * recvfrom: receive a UDP datagram from a client
   */
   bzero(buf, BUFSIZE);
   n = recvfrom(sockfd, buf, BUFSIZE, 0,
     (struct sockaddr *) &clientaddr, &clientlen);
   if (n < 0)
      error("ERROR in recvfrom");

   /*
 * gethostbyaddr: determine who sent the datagram
 */
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
          sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
  error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
  error("ERROR on inet_ntoa\n");
printf("server received datagram from %s (%s)\n",
   hostp->h_name, hostaddrp);
printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);

fp = fopen("users.txt", "r");
while (fgets(line, sizeof(line), fp)) {
  //printf("%s\n",line);
  cred = strncmp(buf,line,strlen(line)-1);
  //printf("%d",strlen(line)-1);
  if(cred == 0){
    printf("Authenticated....");
    flag = 1;
    break;
  }
  else{
    printf("Invalid username/password");
  }
}
 fclose(fp);

1 个答案:

答案 0 :(得分:1)

gethostbyaddr()期望指向struct in_addr的指针作为第一个参数,对于您显示的代码,它将为&clientaddr.sin_addr

形成相关(Linux)文档(man gethostbyaddr):

  

[...]主机地址参数是一个指向类型结构的指针,取决于地址类型,例如结构in_addr *(可能是通过调用 inet_addr 获得的(3) ))地址类型 AF_INET

gethostbyaddr()将错误代码设置为h_errno而不是errno

形成相关(Linux)文档(man gethostbyaddr):

  

返回值

     

[... [ gethostbyname() gethostbyaddr()函数会在发生错误时返回hostent结构或空指针。出错时,h_errno变量包含错误编号。

可能的错误代码也由man-page给出:

  

<强>错误

     

变量h_errno可以具有以下值:

     
      
  • HOST_NOT_FOUND

         

    指定的主机未知。

  •   
  • NO_ADDRESS或NO_DATA

         

    请求的名称有效,但没有IP地址。

  •   
  • NO_RECOVERY

         

    发生了不可恢复的名称服务器错误。

  •   
  • TRY_AGAIN

         

    权威名称服务器上发生临时错误。稍后再试。

  •