Input-dependent error: sendto() error code 22 (Invalid argument) depending on input size
Input-dependent error: sendto() error code 22 (Invalid argument) depending on input size
我在实现 C UDP 套接字程序时遇到问题。下面的代码可以完美地处理任何短于 56 个字符的输入,但如果我输入 56 个或更多字符,sendto
会抱怨我给它的参数无效(错误代码 22)。
例如,这将正确发送:
./talkerDemo localhost qqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwww
但这不会:
./talkerDemo localhost qqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwwH
什么给了?
/*
** talker.c
** Adapted from http://beej.us/guide/bgnet/html/single/bgnet.html#datagram
*/
#include <stdio.h>
#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 "4242" // the port users will be connecting to
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
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;
}
//============================================================
// !!!!!!! Eror occurs here:
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto");
exit(1);
}
//============================================================
freeaddrinfo(servinfo);
printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
close(sockfd);
return 0;
}
编辑
这是我实际上 运行 的代码版本。在发布问题之前,我已经回到原来的(上图)以查看该实施是否也出现了问题 - 我似乎是这样。但事实证明我很厚,使用了错误的二进制文件... derp
/*
** UDPTalker.hpp -- a datagram sockets "server"
** Adapted from http://beej.us/guide/bgnet/html/single/bgnet.html#datagram
*/
#ifndef UDPTALKER_H
#define UDPTALKER_H
#include <stdio.h>
#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>
#include <iostream>
#include <string>
#define UDPT_DEFAULT_PORT "4243"
#define UDPT_DEFAULT_HOST "localhost"
#define UDPT_MAXBUFLEN 2048
class UDPTalker {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
std::string host;
std::string port;
public:
//! Takes target hostname/ip and port as arguments. Defaults: ("localhost", "4243")
UDPTalker(std::string host = UDPT_DEFAULT_HOST, std::string port = UDPT_DEFAULT_PORT);
~UDPTalker();
void send(std::string msg);
};
#endif // UDPTALKER_H
这里是对应的.cpp
:
// File UDPTalker.cpp
#include "UDPTalker.hpp"
UDPTalker::UDPTalker(std::string h, std::string port) : host(h), port(port) {
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(host.c_str(), port.c_str(), &hints, &servinfo)) != 0) {
throw std::runtime_error(std::string("getaddrinfo: ").append(gai_strerror(rv)));
}
// 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;
}
freeaddrinfo(servinfo);
if (p == NULL) {
throw std::runtime_error("talker: failed to create socket\n");
}
}
UDPTalker::~UDPTalker() {
close(sockfd);
}
void UDPTalker::send(std::string msg) {
if ((numbytes = sendto(sockfd, msg.c_str(), msg.size(), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto! ");
}
// printf("talker: sent %d bytes to %s\n", numbytes, host.c_str());
}
freeaddrinfo(servinfo);
释放 servinfo
使用的内存。这意味着指针 p
现在指向空内存,因此当它传递给 sendto
时它可能包含无效内容。我的猜测是,由于某种原因,输入字符串中的额外字节是 "rolling over" 调用 class' send
方法时的内存位置。修复是将 freeaddrinfo(servinfo);
从构造函数移动到析构函数:
UDPTalker::UDPTalker(std::string h, std::string port) : host(h), port(port) {
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(host.c_str(), port.c_str(), &hints, &servinfo)) != 0) {
throw std::runtime_error(std::string("getaddrinfo: ").append(gai_strerror(rv)));
}
// 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) {
throw std::runtime_error("talker: failed to create socket\n");
}
}
UDPTalker::~UDPTalker() {
freeaddrinfo(servinfo);
close(sockfd);
}
我在实现 C UDP 套接字程序时遇到问题。下面的代码可以完美地处理任何短于 56 个字符的输入,但如果我输入 56 个或更多字符,sendto
会抱怨我给它的参数无效(错误代码 22)。
例如,这将正确发送:
./talkerDemo localhost qqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwww
但这不会:
./talkerDemo localhost qqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwweeeeeqqqqqwwwwwH
什么给了?
/*
** talker.c
** Adapted from http://beej.us/guide/bgnet/html/single/bgnet.html#datagram
*/
#include <stdio.h>
#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 "4242" // the port users will be connecting to
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
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;
}
//============================================================
// !!!!!!! Eror occurs here:
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto");
exit(1);
}
//============================================================
freeaddrinfo(servinfo);
printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
close(sockfd);
return 0;
}
编辑
这是我实际上 运行 的代码版本。在发布问题之前,我已经回到原来的(上图)以查看该实施是否也出现了问题 - 我似乎是这样。但事实证明我很厚,使用了错误的二进制文件... derp
/*
** UDPTalker.hpp -- a datagram sockets "server"
** Adapted from http://beej.us/guide/bgnet/html/single/bgnet.html#datagram
*/
#ifndef UDPTALKER_H
#define UDPTALKER_H
#include <stdio.h>
#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>
#include <iostream>
#include <string>
#define UDPT_DEFAULT_PORT "4243"
#define UDPT_DEFAULT_HOST "localhost"
#define UDPT_MAXBUFLEN 2048
class UDPTalker {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
std::string host;
std::string port;
public:
//! Takes target hostname/ip and port as arguments. Defaults: ("localhost", "4243")
UDPTalker(std::string host = UDPT_DEFAULT_HOST, std::string port = UDPT_DEFAULT_PORT);
~UDPTalker();
void send(std::string msg);
};
#endif // UDPTALKER_H
这里是对应的.cpp
:
// File UDPTalker.cpp
#include "UDPTalker.hpp"
UDPTalker::UDPTalker(std::string h, std::string port) : host(h), port(port) {
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(host.c_str(), port.c_str(), &hints, &servinfo)) != 0) {
throw std::runtime_error(std::string("getaddrinfo: ").append(gai_strerror(rv)));
}
// 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;
}
freeaddrinfo(servinfo);
if (p == NULL) {
throw std::runtime_error("talker: failed to create socket\n");
}
}
UDPTalker::~UDPTalker() {
close(sockfd);
}
void UDPTalker::send(std::string msg) {
if ((numbytes = sendto(sockfd, msg.c_str(), msg.size(), 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto! ");
}
// printf("talker: sent %d bytes to %s\n", numbytes, host.c_str());
}
freeaddrinfo(servinfo);
释放 servinfo
使用的内存。这意味着指针 p
现在指向空内存,因此当它传递给 sendto
时它可能包含无效内容。我的猜测是,由于某种原因,输入字符串中的额外字节是 "rolling over" 调用 class' send
方法时的内存位置。修复是将 freeaddrinfo(servinfo);
从构造函数移动到析构函数:
UDPTalker::UDPTalker(std::string h, std::string port) : host(h), port(port) {
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(host.c_str(), port.c_str(), &hints, &servinfo)) != 0) {
throw std::runtime_error(std::string("getaddrinfo: ").append(gai_strerror(rv)));
}
// 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) {
throw std::runtime_error("talker: failed to create socket\n");
}
}
UDPTalker::~UDPTalker() {
freeaddrinfo(servinfo);
close(sockfd);
}