我尝试使用简单的ftp客户端来获取bsd套接字上的文件列表。这是:
connectTo(int client_socket,
struct sockaddr_in* addr,
char* ipv4Address,
char* user,
char* password,
char* response) // buffer wchich store responses
{
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if(socket < 0)
{
printf("Can not create socket");
return 2;
}
addr->sin_family = AF_INET; // address family - internet socket
addr->sin_port = htons(PORT_NUMBER);
addr->sin_addr.s_addr = inet_addr(ipv4Address);
if(connect(client_socket,(struct sockaddr*)addr,sizeof(*addr)) < 0)
{
printf("Can not connect!\n");
return 3;
}
int size_read = recv(client_socket, response, BUF_SIZE, 0);
printf("size_read = %d\n",size_read);
response[size_read] = '\0'; // for not printing rubbish
printf("%s\n",response);
printf("sending username\n");
char *username = strdup("USER trenkinan\r\n");
send(client_socket,username,strlen(username)+1,0);
size_read = recv(client_socket, response, BUF_SIZE, 0);
// printf("size_read = %d\n",size_read);
response[size_read] = '\0'; // for not printing rubbish
printf("%s",response);
if(strstr(response,"331"))
{
//printf("sending password\n");
char *passwd = strdup("PASS test\r\n");
// printf("passwd string: %s",passwd);
int sent = send(client_socket,passwd,strlen(passwd)+1,0);
size_read = recv(client_socket, response, BUF_SIZE, 0);
response[size_read] = '\0'; // for not printing rubbish
printf("%s",response);
//printf("sent bytes: %d\n", sent);
}
return 0;
}
我在工作站上使用debian存储库(ftpd)中的ftp服务器。我尝试执行此代码,当我发送密码服务器回答500'':命令不明白。 我使用wireshark看看会发生什么,我的程序中的所有包看起来都很好,我使用ftp命令(linux上的轻量级ftp客户端)进行连接并且它可以工作但是来自该客户端的包看起来与我自己的客户端相同。有什么想法吗?
答案 0 :(得分:0)
所以,我发现了一个错误:strlen(用户名)而不是strlen(用户名)+1。我希望它会对某人有所帮助。