[英]C++ Sockets recv() syscall returning -1
我目前在服务器和客户端之间传递消息时遇到问题。 据我所知,我正确地遵循了Beej的Socket Programming Tutorial中概述的套接字编程最佳实践。
当我运行两个进程时,recv()syscall返回-1(错误),而不是接收到的字节数。 另外,在尝试输出buf时,还会出现许多gobbledygook字符。 这是有道理的,因为有错误。
我想知道是否有人可以引导我正确方向,为什么我遇到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
。 你需要recv
上newfd
:
byte_count = recv(newfd, buf, sizeof buf, 0); /* newfd instead of sockfd. */
既然这样了,
据我所知,我正确地遵循了套接字编程的最佳实践
我完全不同意。
listen
, bind
, getaddrinfo
等的返回状态 strerror
或perror
在你的程序 您想使用accept()返回的套接字来接收recv()
byte_count = recv(newfd, buf, sizeof buf, 0);
sockfd
仅用于侦听客户端, newfd
用于数据传输。
也许我不应该将此作为答案,而应作为评论。 不过,恕我直言,您对getaddrinfo()
使用对我来说似乎是错误的。
在客户端,应该先调用它,然后遍历结果,直到可以建立连接为止。
所以
struct addrinfo * r2 sockfd = -1; for (r2=res; r2; r2=r2->ai_next) { // make a socket: if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){ continue; // next result } // attempt connection to port if(connect(sockfd, res->ai_addr, res->ai_addrlen) == -1){ close(sockfd); sockfd = -1; continue; } } if (sockfd == -1) { // do error handling }
这样,您可以检查所有可能的连接。
在服务器端,使用getaddrinfo()
非常不寻常。 通常,您可以通过使用setsockopt()
取消设置IPV6_V6ONLY
标志来创建一个IPv6套接字,并使它也可以侦听IPv4。 这样,套接字可以侦听IPv6和IPv4。 (A,不是在Windows XP XP AFAIK上。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.