udp客户端服务器程序c

时间:2012-11-09 22:10:35

标签: c sockets udp

我在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);
}

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

UdpServer:

$ ./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...

UdpClient:

$ ./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程序停滞不前。有谁能解释一下问题是什么?

2 个答案:

答案 0 :(得分:0)

如果服务器和客户端在同一台计算机上运行,​​请提供

$ ./UdpClient localhost 49932

而不是

$ ./UdpClient MyPC 49932

否则
$ ./UdpClient <server-IP-address> 49932

同样在服务器代码中,

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

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

答案 1 :(得分: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");

  ...
}
...