【问题标题】:The UDP sendto() and recvfrom() functions are not working as expectedUDP sendto() 和 recvfrom() 函数未按预期工作
【发布时间】:2021-10-10 05:15:03
【问题描述】:

我想实现一个简单的UDP服务器和客户端,所以我用C语言写了一半的代码。 该代码的目的是从Client向Server发送消息,如果消息发送正确,Server向Client发送“ACK”消息。 但是sendto()recvfrom()的返回值为-1,消息没有发送出去。 在 udpserver.c 中,我知道“ACK”在 buf 中被 strcpy 牢牢覆盖。 在 udpserver.c 中,我可以发送消息,在 udpclient.c 中,我可以接收消息,但我不知道如何从这里编写代码。你能给我一些具体的代码吗? 另外,我希望服务器说 Hello,客户端说 ACK。

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

#define MYPORT "4567"                      // the port that client  will be connecting to
#define MAXBUFLEN 100
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
  if (sa->sa_family == AF_INET) {
    return &(((struct sockaddr_in*)sa)->sin_addr);
  }
  return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
  int sockfd;
  struct addrinfo hints, *servinfo, *p;
  int rv;
  int numbytes;
  struct sockaddr_storage their_addr;
  char buf[MAXBUFLEN];
  int32_t receivedNumber;
  socklen_t addr_len;

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;             // set to AF_INET to force IPv4
  hints.ai_socktype = SOCK_DGRAM;          // UDP 
  hints.ai_flags = AI_PASSIVE;             // use my IP


  if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  // loop through all the results and bind to the first we can
  for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("server: socket");
      continue;
    }
    if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("server: bind");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "listener: failed to bind socket\n");
    exit(1);
  }
  printf("server: waiting for client...\n");
  addr_len = sizeof their_addr;

  //Receive from client
  if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
    perror("recvfrom");
    exit(1);
  }
  printf("Received from client: %s\n", buf);
  strcpy(buf,"ACK");


  //Send client an ACK message
  if(sendto(sockfd,buf,strlen(buf),0,p->ai_addr,p->ai_addrlen)==-1){
    printf("Error\n");
    }

  /*
    unknown code
  */

  freeaddrinfo(servinfo);
  close(sockfd);
  return 0;
} 
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define SERVERPORT "4567" // the port that client  will be connecting to
#define MAXBUFLEN 100

int main(int argc, char *argv[])
{
  int sockfd;
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_storage their_addr;
  socklen_t addr_len;
  int rv;
  int numbytes;
  char buf[MAXBUFLEN];

  if (argc != 3) {
    fprintf(stderr,"usage: talker hostname message\n");
    exit(1);
  }

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;

  if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  // loop through all the results and make a socket
  for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("talker: socket");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "talker: failed to create socket\n");
    return 2;
  }

  // Send to server
  if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, p->ai_addr, p->ai_addrlen)) == -1) {
    perror("client: sendto");
    exit(1);
  }

  //Receive from server
   if(recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&p->ai_addrlen)==-1){
    printf("Error\n");
    }
  /* 
    unknown code
  */


  printf("Received from server: %s\n", buf);

  freeaddrinfo(servinfo);
  close(sockfd);

  return 0;
}
gcc udpserver.c
./a.out
server: waiting for client...
Received from client: Hello~

gcc udpclient.c 
 ./a.out 127.0.0.1 Hello
Received from server: �*��z     ```

【问题讨论】:

  • @G.M. OP使用UDP,发送一定长度的数据包。
  • 返回值 -1 表示失败。 errnoperror()strerror() 告诉你什么失败。您需要提供更多信息。

标签: c sockets udp


【解决方案1】:

如果您从通道读取到缓冲区并且结果是肯定的,那么您已经成功读取了一些 bytes,但不是以 null 结尾的 string

例如

numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, ...);

numbytes是读取的字节数,现在是时候制作一个字符串了:

buf[numbytes] = '\0';

客户端和服务器均不将空字节添加到buf 的末尾。

【讨论】:

  • @user207421 A) 他如何产生他的输出和 B) buf 仍然不是空终止的
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 2023-04-04
  • 2016-05-26
  • 2019-10-30
  • 2015-04-11
  • 2022-01-25
  • 2019-12-22
  • 1970-01-01
相关资源
最近更新 更多