我寫了一個應用程序,它使用來自GNU C套接字庫的TCP套接字。它只是基本上監聽傳入請求的套接字。我可以用本地主機上的telnet連接到套接字,但是當我嘗試從另一臺機器連接時,沒有響應。我正在運行Fedora 13並禁用了我的防火牆,但它仍然不起作用。無法連接到從遠程主機的Linux tcp套接字應用程序
插座代碼封裝在寫一些其他組織應該已經工作,但這裏的圖書是它的肉:
...
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;
謝謝, 亞歷克斯
你確定你正在監聽正確的網絡地址嗎? –
另一個經典的htons()問題? –
以及我使用的IP爲127.0.0.1。套接字代碼屬於另一個組織編寫的庫,它應該可以工作。 – Alex