C++ 套接字 recv() 系统调用返回 -1

标签 c++ linux sockets

我目前在服务器和客户端之间传递消息时遇到问题。 据我所知,我正确地遵循了 Beej's Socket Programming Tutorial 概述的套接字编程最佳实践。 .

当我运行这两个进程时,recv() 系统调用返回 -1(错误),而不是接收到的字节数。此外,在尝试输出 buf 时,会出现一堆官话字符。这是有道理的,因为有错误。

我想知道是否有人可以引导我朝着正确的方向前进,为什么我在使用 recv() 时遇到问题?以下是相关的代码片段。

服务器:

struct sockaddr_storage their_addr;
socklen_t addr_size;
int sockfd, newfd, byte_count, status;
char buf[512];
struct addrinfo hints,  *res;

//  first,  load  up  address  structs  with  getaddrinfo():
memset(&hints,  0,  sizeof  hints);
hints.ai_family  =  PF_INET;
hints.ai_socktype  =  SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

// get address info, print stuff if error
if((status = getaddrinfo("nunki.usc.edu",  "21957",  &hints,  &res)) !=0){
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}

//  make  a  socket:
if((sockfd  =  socket(res->ai_family,  res->ai_socktype,  res->ai_protocol)) == -1){
    cout << "socket fail" << endl;
}

// bind the socket to the port
bind(sockfd, res->ai_addr, res->ai_addrlen);

// required output
cout << "Phase1: Login server has TCP port number " << "21957 " 
     << "and IP address " << getIPfromHost("nunki.usc.edu") << endl;

// listen for incoming connections
listen(sockfd, 10);
cout << "after listen" << endl;

// halt until receipt 
addr_size = sizeof(their_addr);
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
cout << "after accept" << endl;

// Now  that  we're  connected,  we  can  receive  some data
byte_count  =  recv(sockfd,  buf,  sizeof  buf,  0); 
printf("recv()'d  %d  bytes  of  data  in  buf\n",  byte_count);
printf("Msg is %s\n", buf);

客户:

struct addrinfo hints,  *res;
int  sockfd;

//  first,  load  up  address  structs  with  getaddrinfo():
memset(&hints,  0,  sizeof  hints);
hints.ai_family  =  AF_INET;
hints.ai_socktype  =  SOCK_STREAM;
getaddrinfo("nunki.usc.edu",  "21957",  &hints,  &res);

//  make  a  socket:
if((sockfd  =  socket(res->ai_family,  res->ai_socktype,  res->ai_protocol)) == -1){
    cout << "socket fail" << endl;
}

// attempt connection to port
if(connect(sockfd,  res->ai_addr,  res->ai_addrlen) == -1){
    cout << "connect fail" << endl;
}

// send message to server
cout << "sockfd " << sockfd << endl;
int byte_count = send(sockfd, "Hello", 5, 0); 
cout << byte_count << endl;

以下是服务器的输出:

Phase1: Login server has TCP port number 21957 and IP address 68.181.201.3
after listen
after accept
recv()'d  -1  bytes  of  data  in  buf
Msg is ÿhÿ?sÈ
Glæ

以下是客户端的输出:

sockfd 4
5

最佳答案

您在错误的套接字上调用了 recv。您需要在 newfdrecv:

byte_count = recv(newfd, buf, sizeof buf, 0); /* newfd instead of sockfd. */

既然这样,

As far as I know, I am properly following best practices for socket programming

我完全不同意。

  • 您没有检查 listenbindgetaddrinfo 等的返回状态
  • 您的程序中没有strerrorperror

关于C++ 套接字 recv() 系统调用返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111044/

相关文章:

java - 套接字编程服务器套接字超时

c++ - 为什么设计器生成的嵌入为 "Aggregation"的 UI 类无法实现自定义插槽?

c++ - 如何在 C++ 中使用 libmosquitto 和 SSL/TLS 连接到 mqtt 代理

python - pip install py2EXE/cs_Freeze 报错 Ubuntu

linux - 不能打开超过 1023 个套接字

c# - TcpClient 和 UdpClient 抛出错误

c++ - 从 boost asio 套接字读取并打印到终端

c++ - 在 CMake 中,如何基于每个用户更改构建类型的默认编译器标志?

linux - 在 Linux/Apache 上在后台运行 Cron 作业

linux - allegro 是否使用 X11 创建显示窗口?