使用 C 中的套接字向 FTP 服务器发送命令
Send command to FTP server using sockets in C
我是套接字编程的新手。我想登录到我的 ftp 服务器。我可以成功建立连接,但我无法发送其他用户和密码命令。
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
argv[0]="2";
argv[1]="00.00.00.0000"; //my ip
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
char message[100]="USER ftpuser@domain.com";
argc=2;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(21);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
if( send(sockfd , message , strlen(message) , 0) < 0)
{
perror("send failed");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
return 0;
}
}
if(n < 0)
{
printf("\n Read error \n");
return 0;
}
return 0;
}
我收到这个:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 22:05. Server port: 21.
220 You will be disconnected after 15 minutes of inactivity.
为什么连接建立后无法向服务器发送命令?
您需要使用 CRLF 终止每个 FTP 命令。这在您的 USER 命令中缺失。远程服务器在收到换行符之前不会处理该命令。
来源:RFC 959
我是套接字编程的新手。我想登录到我的 ftp 服务器。我可以成功建立连接,但我无法发送其他用户和密码命令。
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
argv[0]="2";
argv[1]="00.00.00.0000"; //my ip
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
char message[100]="USER ftpuser@domain.com";
argc=2;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(21);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
if( send(sockfd , message , strlen(message) , 0) < 0)
{
perror("send failed");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
return 0;
}
}
if(n < 0)
{
printf("\n Read error \n");
return 0;
}
return 0;
}
我收到这个:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 22:05. Server port: 21.
220 You will be disconnected after 15 minutes of inactivity.
为什么连接建立后无法向服务器发送命令?
您需要使用 CRLF 终止每个 FTP 命令。这在您的 USER 命令中缺失。远程服务器在收到换行符之前不会处理该命令。
来源:RFC 959