无法连接端口9150 c ++

时间:2013-08-08 23:51:51

标签: c port tor

我正在尝试使用以下代码连接到localhost(TOR)上的端口9150:

WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData) != 0)
{
    return 1;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0)
{
    printf("error 0");
}

struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if(fd > 0)
{
    printf("error 1");
}

并且失败并显示“错误1”。 TOR / Vidalia在这个端口上运行,一切正常,所以错误必须在我的代码中。请帮帮我。

1 个答案:

答案 0 :(得分:1)

这里的小事:

#include <stdio.h> /* for perror() */

WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData) != 0)
{
    return 1;
}

int fd = socket(AF_INET, SOCK_STREAM, 0);
int rv = 0;

if(fd < 0)
{
     perror("socket");
     exit(fd); /* or return */
}

struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

您的核心问题在以下代码中

rv = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if(rv < 0)
{
     perror("connect");
     exit(rv); /* or return */
}