我无法连接到数据端口。我能够连接到 21 命令端口,发送用户名和密码。然后我发送PASV\r\n
。我得到响应并计算数据端口A * 256 + B = new_port
。问题是我无法连接到这个端口。为了创建套接字,我正在使用createRemoteConnection()
函数。这被使用了两次,第一次用于命令端口,我通过那里new_port
,然后我再次调用它。请问你能帮帮我吗?谢谢你。
PASV
227 Entering Passive Mode (80,251,0,59,158,36).
A: 158
B: 36
Port: 40484
Creating socket...
Socket created
connect failed. Error: Can't assign requested address
-
int createRemoteConnection(Arguments &arguments,int cmd) {
int sock;
struct sockaddr_in server;
if (DEBUG) {
puts("Creating socket...");
}
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
fputs("Could not create socket.\n",stderr);
exit(1);
}
if (DEBUG) {
puts("Socket created");
}
char ip[100];
struct hostent *he;
struct in_addr **addr_list;
if ((he = gethostbyname(arguments.hostname)) == NULL) {
herror("gethostbyname");
exit(1);
}
addr_list = (struct in_addr **) he->h_addr_list;
for (int i = 0; addr_list[i] != NULL; i++) {
strcpy(ip, inet_ntoa(*addr_list[i]));
}
server.sin_addr.s_addr = inet_addr(ip);
server.sin_family = AF_INET;
server.sin_port = htons(cmd);
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
exit(1);
}
if (DEBUG) {
puts("Connected\n");
}
return sock;
}