gpt4 book ai didi

c - Ubuntu 中的 FTP 主动模式客户端错误 : I won't open a connection to 127. 0.1.1

转载 作者:行者123 更新时间:2023-11-30 17:23:33 25 4
gpt4 key购买 nike

我已经创建了 FTP 主动模式客户端。在 OS X 操作系统上它运行得很好。但是当我迁移到 Ubuntu 时,它开始失败。问题是,在 OS X 上,当我启用数据连接时,我的监听套接字会监听我的 wifi 路由器 IP。我不知道为什么,但在我的 Ubuntu 上它监听 127.0.1.1。因此,我得到了对 PORT 命令的响应:

I won't open a connection to 127.0.1.1

如何像在 OS X 中一样确定主机的优先级?

数据连接:

    else if (strcmp(command, "ls") == 0)
{
// Listening data socket initialization
data_socket = listen_to_server(&ip_address);

if (data_socket == SOCKET_ERROR)
{
goto EXIT;
}

// FTP commad PORT request
if(request_port(&command_socket, ip_address) == SOCKET_ERROR)
{
goto EXIT;
}

// Server response receiving
if(receive_packet(&command_socket, packet) == SOCKET_ERROR)
{
goto EXIT;
}

// Server response printing
printf ("%s\n", packet);

// FTP command LIST request
if(request_list(&command_socket) == SOCKET_ERROR)
{
goto EXIT;
}

// Server response receiving
if(receive_packet(&command_socket, packet) == SOCKET_ERROR)
{
goto EXIT;
}

// Server response printing
printf ("%s\n", packet);

// Server connection accepting (Active mode)
int address_size = sizeof(struct sockaddr_in);
struct sockaddr_in remote_address;

data_socket_server = accept(data_socket, (struct sockaddr *)&remote_address, (socklen_t *)&address_size);

// Server response receiving
if(receive_packet(&command_socket, packet) == SOCKET_ERROR)
{
goto EXIT;
}

// Server response printing
printf ("%s\n", packet);

// Server data receiving
if(receive_packet(&data_socket_server, packet) == SOCKET_ERROR)
{
goto EXIT;
}

// Server data printing
printf ("%s\n", packet);

// Sockets closing
closesocket(data_socket_server);
closesocket(data_socket);
}

监听套接字创建:

// Listening to server function
SOCKET listen_to_server(char **ip)
{
// SOCKET variable
SOCKET data_socket;
// SOCKET address structure
struct sockaddr_in socket_address;
// Listening host name
char host_name [256] = {0};
// Listening host entry
struct hostent *host_entry;

// Host name initialization
if (gethostname(host_name, sizeof(host_name)) == INVALID_SOCKET)
{
printf("Error: failed to get host name\n");

return INVALID_SOCKET;
}

// Host entry structure initialization
if ((host_entry = gethostbyname(host_name)) == NULL)
{
printf("Error: failed to get host by name \'%s\'\n", host_name);

return INVALID_SOCKET;
}

// Socket address structure initialization
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(DATA_PORT);
socket_address.sin_addr = *(struct in_addr *)host_entry -> h_addr;
memset (&(socket_address.sin_zero), 0, 8);

// Socket initialization
if ((data_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Error: failed to initialize socket\n");

return INVALID_SOCKET;
}

// Socket and address binding
if (SOCKET_ERROR == bind(data_socket, (struct sockaddr *)&socket_address, sizeof(socket_address)))
{
printf("Error: failed to bind socket\n");

closesocket(data_socket);

return INVALID_SOCKET;
}

// Socket seting as listening
if (SOCKET_ERROR == listen(data_socket, MAX_QUEUE_LENGTH))
{
printf("Error: failed to set socket as listening\n");

closesocket (data_socket);

return INVALID_SOCKET;
}

*ip = inet_ntoa(*(struct in_addr *)host_entry -> h_addr);

return data_socket;
}

另外,netstat 不显示监听数据 soxket 外部地址:

tcp        0      0 127.0.1.1:1024          0.0.0.0:*               LISTEN

最佳答案

if ((host_entry = gethostbyname(host_name)) == NULL)
...
socket_address.sin_addr = *(struct in_addr *)host_entry -> h_addr;
...
if (SOCKET_ERROR == bind(data_socket, (struct sockaddr *)&socket_address, sizeof(socket_address)))

看起来您只是假设系统本地配置的主机名将解析为外部地址。这个假设是错误的。

您应该使用 FTP 控制连接的本地地址 (getsockname) 作为数据连接的本地地址。

关于c - Ubuntu 中的 FTP 主动模式客户端错误 : I won't open a connection to 127. 0.1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com