3

我正在用 C 编写一个简单的客户端和服务器程序。我能够将日期从客户端发送到服务器。但是,我无法从服务器向客户端发送确认。

/*******************udpserver.c*****************/
int main(int argc, char *argv[])
{
    /* Variable and structure definitions. */
    int sd, rc;
    struct sockaddr_in serveraddr, clientaddr;
    clientaddrlen = sizeof(clientaddr);
    int serveraddrlen = sizeof(serveraddr);
    char buffer[100];
    char *bufptr = buffer;
    int buflen = sizeof(buffer);

    if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("UDP server - socket() error");
        exit(-1);
    }

    printf("UDP server - socket() is OK\n");

    memset(&serveraddr, 0x00, serveraddrlen);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(0);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if((rc = bind(sd, (struct sockaddr *)&serveraddr, serveraddrlen)) < 0) {
        perror("UDP server - bind() error");
        close(sd);
        exit(-1);
    }

    int addr_len = sizeof(serveraddr);
    if (getsockname(sd, (struct sockaddr *) &serveraddr, &addr_len)<0) {
        perror("Error getting socket name.\n");
        return -1;
    }
    printf("Using IP %s and port %d\n", inet_ntoa(serveraddr.sin_addr), ntohs(serveraddr.sin_port));
    printf("UDP server - Listening...\n");

    rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, &clientaddrlen);
    if(rc < 0) {
        perror("UDP Server - recvfrom() error");
        close(sd);
        exit(-1);
    }

   printf("UDP Server received the following:\n \"%s\" message\n", bufptr);

   printf("UDP Server replying to the UDP client...\n");
   rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, clientaddrlen);
   if(rc < 0) {
       perror("UDP server - sendto() error");
       close(sd);
       exit(-1);
   }
   printf("UDP Server - sendto() is OK...\n");

   close(sd);
   exit(0);
}

我的 UDPClient 程序:

/****************udpclient.c********************/
int main(int argc, char *argv[])
{
    /* Variable and structure definitions. */
    int sd, rc;
    struct sockaddr_in serveraddr, clientaddr;
    int serveraddrlen = sizeof(serveraddr);
    char server[255];
    char buffer[100];
    char *bufptr = buffer;
    int buflen = sizeof(buffer);
    struct hostent *hostp;
    memset(buffer, 0x00, sizeof(buffer));
    /* 36 characters + terminating NULL */
    memcpy(buffer, "Hello! A client request message lol!", 37);

    if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("UDP Client - socket() error");
        exit(-1);
    }
    else
        printf("UDP Client - socket() is OK!\n");

    if(argc != 3) {
        /*Use default hostname or IP*/
        printf("UDP Client - Usage <Server hostname or IP>\n");
        exit(0);
    }

    memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));

    hostp = gethostbyname(argv[1]);
    if(hostp == (struct hostent *)NULL) {
        printf("HOST NOT FOUND --> ");
        printf("h_errno = %d\n", h_errno);
        exit(-1);
    }
    else {
        printf("UDP Client - gethostname() of the server is OK... \n");
        printf("Connected to UDP server\n");
    }
    memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));

    rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    if(rc < 0) {
        perror("UDP Client - sendto() error");
        close(sd);
        exit(-1);
    }
    else
        printf("UDP Client - sendto() is OK!\n");

    printf("Waiting a reply from UDP server...\n");

    rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&serveraddr, &serveraddrlen);

    if(rc < 0) {
        perror("UDP Client - recvfrom() error");
        close(sd);
        exit(-1);
    } else {
        printf("UDP client received the following: \"%s\" message\n", bufptr);
    }

    close(sd);
    exit(0);
}

运行这两个程序时,我得到以下输出:

UDP服务器:

$ ./UdpServer
UDP server - socket() is OK
Using IP 0.0.0.0 and port 49932
UDP server - Listening...
UDP Server received the following:
"Hello! A client request message lol!" message
UDP Server replying to the UDP client...
UDP Server - sendto() is OK...

UDP客户端:

$ ./UdpClient MyPC 49932 
UDP Client - socket() is OK!
UDP Client - gethostname() of the server is OK...
Connected to UDP server
UDP Client - sendto() is OK!
Waiting a reply from UDP server...

UdpClient 程序卡在这一点上。谁能解释一下问题是什么?

4

2 回答 2

0

如果服务器和客户端在同一台机器上运行,请给出

$ ./UdpClient localhost 49932

代替

$ ./UdpClient MyPC 49932

别的
$ ./UdpClient <server-IP-address> 49932

同样在服务器代码中,

clientaddrlen = sizeof(clientaddr);
应该
int clientaddrlen = sizeof(clientaddr);

但我想这只是一个复制粘贴错误。

于 2012-11-09T23:18:27.610 回答
0

您可能希望使用select()使该过程等到数据可供读取:

...
{
  fd_set rfds;
  int retval;

  FD_ZERO(&rfds);
  FD_SET(sd, &rfds);

  retval = select(sd+1, &rfds, NULL, NULL, NULL);
  if (retval == -1)
    perror("select()");
  else
    printf("Data is available for reading now.\n");

  ...
}
...
于 2012-11-10T08:54:46.680 回答