#include "client.h"
int sock;
struct hostent *hp;
struct sockaddr_in addr;
int result;
char CMD[CMD_LEN];
char data[BUFFER_SIZE];
char safeguard = '\0';
FILE* file;
void userName(char* user)
{
memset(CMD, 0, sizeof(CMD));
strcpy(CMD, "user ");
strcat(CMD, user);
strcat(CMD, "\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Sending USER failed!");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void passwd(char* passwd)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pass ");
strcat(CMD, passwd);
strcat(CMD, "\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Recv PASSWORD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void put(char* filename)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0)
{
printf("[err] Sending data failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp));
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "type i\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "stor ");
strcat(CMD, filename);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
result = 1;
FILE* file = fopen(filename, "rb");
memset(data, 0, sizeof(data));
while (result = fread(data, sizeof(char), BUFFER_SIZE, file))
{
write(tempSock, data, result);
memset(data, 0, sizeof(data));
}
fclose(file);
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
}
void get(char* filename)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp));
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "type i\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0)
{
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "retr ");
strcat(CMD, filename);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
FILE* file = fopen(filename, "w");
while (result > 0){
memset(data, 0 , sizeof(data));
result = read(tempSock, data, BUFFER_SIZE);
fwrite(data, sizeof(char), result, file);
}
fclose(file);
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
}
void pwd()
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pwd");
strcat(CMD, "\r\n");
if ((result= write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Sending CMD failed!");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void cd(char* cwd)
{
memset(CMD, 0, sizeof(CMD));
char end[CMD_LEN];
strcpy(end, cwd);
strcpy(CMD, "cwd ");
strcat(CMD, end);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void dir(char* path)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp)); //init
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
result = 1;
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "list ");
strcat(CMD, path);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
while (result > 0){
memset(data, 0 , sizeof(data));
result = read(tempSock, data, BUFFER_SIZE);
printf("%s", data);
}
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN)
Kinonoyomeo
- 粉丝: 91
- 资源: 1万+
最新资源
- 基于plc的污水处理,组态王动画仿真,带PLC源代码,组态王源代码,图纸,IO地址分配
- MATLAB代码:考虑P2G和碳捕集设备的热电联供综合能源系统优化调度模型 关键词:碳捕集 综合能源系统 电转气P2G 热电联产 低碳调度 参考文档:Modeling and Optimiza
- 永磁同步直线电机仿真实例,仿真教学 maxwell16.0版本 12槽11极 包括图中模型以及一个仿真设置要点word文档教程
- 基于mpx+vue+node.js的双端网盘系统的设计与实现源代码全套技术资料.zip
- welearn刷时长版本v3.0.bat
- 前端分析-2023071100789-y5
- 前端分析-2023071100789
- 调查问卷系统源代码全套技术资料.zip
- C#实用教程郑阿奇梁敬东程序源代码及电子课件
- 环境监测系统源代码全套技术资料.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0