c - udp客户端服务器程序c

标签 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);
}

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

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程序就卡在了这里。谁能解释一下问题出在哪里?

最佳答案

您可能喜欢使用 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");

  ...
}
...

关于c - udp客户端服务器程序c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316775/

相关文章:

c++ - 为什么我的程序在打开 mkfifo-ed 管道时挂起?

c - Visual Studio 13 中的 NASM 设置

c - 在 MSG_DONTWAIT 模式下使用 recvfrom() 从原始套接字读取以太网数据包

java - ObjectInputStream/ObjectOutputStream 工作不正常

c++ - 使用 boost::asio 获取 UDP 套接字远程地址

c - 使用 C 和 Linux 环境变量运行两个命令

c - 初始化窗口代码 Mac OS X

android - 后台套接字连接

python - Python 中使用 socket.send() 的 UDP 调用非常慢

linux - 围绕疯狂的设备接口(interface)进行路由