log open error

This commit is contained in:
John
2022-04-10 17:28:09 +02:00
parent 11e2a8ce0b
commit f0bb329469
+4
View File
@@ -673,20 +673,24 @@ int openNet(std::string host, uint16_t port) {
struct hostent* he; struct hostent* he;
he = gethostbyname(host.c_str()); he = gethostbyname(host.c_str());
if (he == nullptr) { if (he == nullptr) {
std::cerr << "unable to resolve host " << host << std::endl;
return -1; return -1;
} }
memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length); memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length);
} else if (inet_aton(host.c_str(), &address.sin_addr) == 0) { } else if (inet_aton(host.c_str(), &address.sin_addr) == 0) {
std::cerr << "unable to resolve IP " << host << std::endl;
return -1; return -1;
} }
address.sin_family = AF_INET; address.sin_family = AF_INET;
address.sin_port = (in_port_t)htons(port); address.sin_port = (in_port_t)htons(port);
int fd = socket(AF_INET, SOCK_STREAM, 0); int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) { if (fd < 0) {
std::cerr << "unable to open " << host << std::endl;
return -1; return -1;
} }
if (connect(fd, (struct sockaddr *) &address, sizeof(address)) != 0) { if (connect(fd, (struct sockaddr *) &address, sizeof(address)) != 0) {
close(fd); close(fd);
std::cerr << "unable to connect to " << host << std::endl;
return -1; return -1;
} }
fcntl(fd, F_SETFL, O_NONBLOCK); // set non-blocking fcntl(fd, F_SETFL, O_NONBLOCK); // set non-blocking