Reputation: 732
I wrote an app that uses a TCP socket from the GNU C socket library. It just basically listens on a socket for incoming requests. I am able to connect to the socket with telnet on the localhost, but when I try connecting from another machine there is no response. I'm running Fedora 13 and disabled my firewall, but it still doesn't work.
The socket code is encapsulated in a library that was written by some other organization and is supposed to work already, but here's the meat of it:
...
fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
if (fd_ < 0)
{
perror("socket");
return -1;
}
int val = 1;
int rc = setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if (rc < 0)
{
perror("sesockopt");
close();
return -1;
}
rc = setsockopt(fd_, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
if (rc < 0)
{
perror("sesockopt");
close();
return -1;
}
const int flags = ::fcntl(fd_, F_GETFL, 0);
::fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
rc = ::bind(fd_, addr, addr.size_);
if (rc < 0)
{
perror("bind");
close();
return -1;
}
rc = ::listen(fd_, 10);
if (rc < 0)
{
perror("bind");
close();
return -1;
}
return 0;
Thanks, Alex
Upvotes: 0
Views: 937
Reputation: 84239
In order to accept connections you actually have to call accept(2)
on that TCP socket. As given, the code only prepares the socket for listening on the network.
Also, since you are marking that socket as non-blocking, you'd probably want to wrap it in some sort of select(2)
loop.
Upvotes: 1