1、我們先搭建乙個乙個伺服器與客戶機並使他們進行通訊
2、在客戶機中對輸入的指令進行判斷,將指令轉化成對應的列舉型別傳送給服務機
3、服務機對傳送過來的ftp_cmd的值進行判斷,並執行相對應的功能
4、ls功能的實現:通過popen實現,獲取內容傳送客戶機並顯示
get 對指令進行分割,讀取分割後的檔名所對應的檔案,之後將讀取後的資料傳送給服務機
put 對指令進行分割,將檔案讀取到data中,傳送給客戶機,並在伺服器中建立檔案,將內容寫入
quit 直接退出
cd 採用chdir來實現
使用者名稱和密碼的實現:從檔案讀取密碼以及賬號與使用者輸入的進行對比,一致通過反之停止
伺服器**
#include #include #include #include #include #include /* see notes */
#include #include "msg.h"
#include "log.h"
#include "utils.h"
int g_running;
/** *
* @param in_cmd
* @param out_cmd
*/void handle_cmd(struct msg *in_cmd, struct msg *out_cmd)
break;
default:
break;
}}/**
* @brief 澶勭悊瀹㈡埛絝殑鍛戒護in_cmd錛屾妸緇撴灉鍐欏靉out_cmd
* * @param in_cmd
* @param out_cmd
*/void handle_cmd2(struct msg *in_cmd, struct msg *out_cmd)
} else if (ftp_cmd_hist == in_cmd->cmd) else if (ftp_cmd_cd == in_cmd->cmd)
int ret = chdir(dirname);
log_write("chdir ret %d", ret);
} else if (ftp_cmd_quit == in_cmd->cmd) else if (ftp_cmd_get == in_cmd->cmd)
long length = get_length(filename);
if (length < 0 || length > sizeof(out_cmd->data))
get_md5(filename, out_cmd->md5);
file *fp = fopen(filename, "r");
if (fp != null) else
} else if (ftp_cmd_put == in_cmd->cmd)
// 瀵規瘮瀹㈡埛絝殑md5
char md5[64];
get_md5(filename, md5);
if (memcmp(md5, in_cmd->md5, 32) != 0)
}}int main(int argc, char **ar**)
// bind絝彛錛孲erver_port
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = af_inet;
serveraddr.sin_port = server_port;
serveraddr.sin_addr.s_addr = htonl(inaddr_any);
int on = 1;
setsockopt(listenfd, sol_socket, so_reuseaddr, &on, sizeof(on));
ret = bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (ret < 0)
ret = listen(listenfd, 0);
if (ret < 0)
sock = accept(listenfd, null, 0);
if (ret < 0)
log_write("client connect.\n");
struct auth auth;
ret = recv(sock, &auth, sizeof(struct auth), 0);
log_write("%s %s\n", auth.username, auth.password);
struct auth server;
file *fp = fopen("passwd", "r");
if (fp != null)
if (0 != memcmp(auth.username, server.username, strlen(server.username)) ||
0 != memcmp(auth.password, server.password, strlen(server.password)))
ret = send(sock, &auth, sizeof(struct auth), 0);
log_write("send %d\n", ret);
if (ftp_cmd_error == auth.cmd)
g_running = 1;
while (g_running)
log_destroy();
return 0;
}
客戶機
#include #include #include #include #include #include /* see notes */
#include "log.h"
#include "msg.h"
#include "utils.h"
enum ftp_cmd get_cmd(char *buf, struct msg *msg)
return ftp_cmd_error;
}int handle_user_input(struct msg *msg_send)
msg_send->cmd = cmd;
strcpy(msg_send->args, buf);
return 0;
}int handle_user_input2(struct msg *msg_send)
else if (0 == memcmp(buf, "hist", 4)) else if (0 == memcmp(buf, "cd", 2)) else if (0 == memcmp(buf, "get", 3)) else if (0 == memcmp(buf, "quit", 4)) else if (0 == memcmp(buf, "put", 3))
long length = get_length(filename);
if (length < 0 || length > sizeof(msg_send->data))
get_md5(filename, msg_send->md5);
// #define null 0
file *fp = fopen(filename, "r");
if (null != fp) else
} else
if (cmd == ftp_cmd_error)
msg_send->cmd = cmd;
strcpy(msg_send->args, buf);
return 0;
}int main(int argc, char **ar**)
log_write("connect server sucess\n");
struct auth auth;
printf("username:");
scanf("%s", auth.username);
printf("password:");
scanf("%s", auth.password);
auth.cmd = ftp_cmd_auth;
ret = send(sock, &auth, sizeof(struct auth), 0);
log_write("send ret %d", ret);
ret = recv(sock, &auth, sizeof(struct auth), 0);
if (ftp_cmd_error == auth.cmd)
printf("well done\n");
while(1)
ret = send(sock, msg_send, sizeof(struct msg), 0);
log_write("send ret %d\n", ret);
ret = recv(sock, msg_recv, sizeof(struct msg), 0);
log_write("recv ret %d\n", ret);
log_write("cmd %d\n", msg_recv->cmd);
log_write("data %s\n", msg_recv->data);
if (ftp_cmd_hist == msg_recv->cmd || ftp_cmd_ls == msg_recv->cmd) else if (ftp_cmd_get == msg_recv->cmd)
char md5[64];
get_md5(filename, md5);
if (memcmp(md5, msg_recv->md5, 32) != 0)
} else if (ftp_cmd_quit == msg_recv->cmd)
}log_destroy();
return 0;
}
socket檔案傳輸
伺服器 本檔案是伺服器的 include for sockaddr in include for socket include for socket include for printf include for exit include for bzero include for time t an...
socket檔案傳輸
最近入職培訓中需要寫乙個linux下的c s網路檔案傳輸工具,在實現的過程中,遇到了一些坑,在這裡 做個總結 由於udp伺服器不需要accept 因此也沒有link id 連線的客戶端id 在伺服器檔案接收或下發檔案時需要知道當下與之通訊的客戶端的識別符號,由於udp是無連線的,並且sockfd s...
Socket檔案傳輸
服務端 輸入檔案完整路徑傳送給客戶端 import struct import json import os tcp server socket ip port 127.0.0.1 8080 buffsize 1024 埠的重複利用 tcp server.bind ip port tcp serve...