I’m using BSD sockets and I want to use ::connect to connect to example.com on port 80. The man page of ::socket tells me that it returns either a valid file descriptor, or -1 on error.
auto fd = ::socket(AF_INET, SOCK_STREAM, 0);
struct ::sockaddr_in addr;
::bzero(&addr, sizeof(addr));
addr.sin_family = family_;
struct ::hostent* hostent = ::gethostbyname(host.c_str());
::bcopy(hostent->h_addr, &addr.sin_addr.s_addr, hostent->h_length);
addr.sin_port = port;
auto err = ::connect(fd,
reinterpret_cast<struct ::sockaddr*>(&addr),
sizeof(addr));
fd == 0, so ::socket succeeded (otherwise it would’ve returned -1). However, err == -1 and errno is set to EBADF, indicating that fd is a bad file descriptor.
What could be going on here? Why does ::connect tell me that I gave it a bad file descriptor while I clearly did not?
You should confirm that
socketis actually returning 0? Unless you’ve closed the standard file descriptors, that would be very unusual. Make sure you check it immediately after the socket call in case it’s being corrupted by the other calls.