Unverified Commit 069068aa authored by Rhett Ying's avatar Rhett Ying Committed by GitHub
Browse files

[Log] fix confusing error log in TCPSocket::Bind() (#4299)

* [Log] fix confusing error log in TCPSocket::Bind()

* fix lint
parent 4f797295
......@@ -64,14 +64,22 @@ bool TCPSocket::Bind(const char * ip, int port) {
SAI sa_server;
sa_server.sin_family = AF_INET;
sa_server.sin_port = htons(port);
int retval = 0;
int ret = 0;
ret = inet_pton(AF_INET, ip, &sa_server.sin_addr);
if (ret == 0) {
LOG(ERROR) << "Invalid IP: " << ip;
return false;
} else if (ret < 0) {
LOG(ERROR) << "Failed to convert [" << ip
<< "] to binary form, error: " << strerror(errno);
return false;
}
do { // retry if EINTR failure appears
if (0 < inet_pton(AF_INET, ip, &sa_server.sin_addr) &&
0 <= (retval = bind(socket_, reinterpret_cast<SA*>(&sa_server),
sizeof(sa_server)))) {
if (0 <= (ret = bind(socket_, reinterpret_cast<SA *>(&sa_server),
sizeof(sa_server)))) {
return true;
}
} while (retval == -1 && errno == EINTR);
} while (ret == -1 && errno == EINTR);
LOG(ERROR) << "Failed bind on " << ip << ":" << port << " , error: " << strerror(errno);
return false;
......
......@@ -154,6 +154,14 @@ void start_server(int id) {
receiver.Finalize();
}
TEST(SocketCommunicatorTest, TCPSocketBind) {
dgl::network::TCPSocket socket;
testing::internal::CaptureStderr();
EXPECT_EQ(socket.Bind("127.0.0", 50001), false);
const std::string stderr = testing::internal::GetCapturedStderr();
EXPECT_NE(stderr.find("Invalid IP: 127.0.0"), std::string::npos);
}
#else
#include <windows.h>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment