实现功能:
ls———查看服务端文件
lls———查看客户端自己的文件
cd———切换服务端目录
lcd———切换客户端自己的目录
切换成功
put———上传文件
get———下载文件
服务器端:
客户端:
源码:
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <limits.h>
#define RD_BUFSIZE 1024
#define WR_BUFSIZE 64
typedef struct c_msg{
int fd;
char cip_addr[16]; //ip鍦板潃淇℃伅
uint16_t c_port; //绔彛鍙?
}c_msg_t;
void choosecmd(char* cmd, c_msg_t* c_msg);
void * service_handle(void* arg){
char r_buf[RD_BUFSIZE];
c_msg_t c_msg = *(c_msg_t *)arg;
int ret = -1;
pthread_detach(pthread_self());
//5.鎺ュ彈鏁版嵁
for(;;){
memset(r_buf, 0, sizeof(r_buf));
ret = read(c_msg.fd, r_buf, sizeof(r_buf));
if(ret < -1){
pthread_exit(0);
}
printf("tid: %ld, fd: %d, receive data is: %s\n",pthread_self(), c_msg.fd, r_buf);
choosecmd(r_buf, &c_msg);
}
close(c_msg.fd);
pthread_exit(0);
}
//鏈嶅姟鍣ㄧ殑socket
int main(int argc, char* argv[]){
//閽堝TCP/IP鐨勭粨鏋勪綋(IP鍦板潃鍜岀鍙e彿鐨勯厤缃?
struct sockaddr_in sin;
bzero(&sin, sizeof(sin));
//1.鍒涘缓绔彛
int s_fd = socket(AF_INET, SOCK_STREAM, 0);
if(s_fd < 0){
perror("鍒涘缓绔彛澶辫触\n");
exit(-1);
}
//2.閰嶇疆IP鍜岀鍙e彿
//struct sockaddr_in {
// sa_family_t sin_family; /* address family: AF_INET */
// in_port_t sin_port; /* port in network byte order */
// struct in_addr sin_addr; /* internet address */
// };
/* Internet address. */
//struct in_addr {
// uint32_t s_addr; /* address in network byte order */
// };
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[1]));
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(s_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
perror("缁戝畾绔彛澶辫触\n");
close(s_fd);
exit(-1);
}
//3.閰嶇疆鐩戝惉
if(listen(s_fd, 5) < 0){
perror("璁剧疆鐩戝惉澶辫触\n");
exit(-1);
}
//4.鎺ユ敹璇锋眰骞跺垱寤哄瓙绾跨▼
pthread_t tid;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
c_msg_t c_msg;
for(;;){
c_msg.fd = accept(s_fd, (struct sockaddr *)&cin, &addrlen);
if(c_msg.fd < 0){
perror("鎺ュ彈璇锋眰澶辫触\n");
exit(-1);
}
if(!inet_ntop(AF_INET, &cin.sin_addr, c_msg.cip_addr, sizeof(c_msg.cip_addr))){
perror("鑾峰彇IP澶辫触\n");
exit(-1);
}
c_msg.c_port = ntohs(cin.sin_port);
printf("宸茬粡寤虹珛鍜孖P涓?s,绔彛涓?%d鐨勫鎴风鐨勮繛鎺n", c_msg.cip_addr, c_msg.c_port);
pthread_create(&tid, NULL ,service_handle , &c_msg);
}
close(s_fd);
exit(0);
}
void choosecmd(char* cmd, c_msg_t* c_msg){
int cmd_id = 0;
if(strcmp("lls\n", cmd) == 0){
cmd_id = 1;
}
else if(strcmp("quit\n", cmd) == 0){
cmd_id = 2;
}
else if(strstr(cmd, "lcd ")){
cmd_id = 4;
}
else if(strstr(cmd, "get ")){
cmd_id = 5;
}
else if(strstr( cmd, "put ")){
cmd_id = 6;
}
else if(strcmp("lpwd\n", cmd) == 0){
cmd_id = 7;
}
printf("cmd_id = %d\n", cmd_id);
char rd_tmp_buf[RD_BUFSIZE];
switch(cmd_id){
//lls
case 1:{
int rd_byte;
FILE* file = popen("ls", "r");
if(file == NULL){
printf("open failed!\n");
}
for(;;){
rd_byte = fread(rd_tmp_buf, sizeof(rd_tmp_buf) / 16, 1, file);
if(rd_byte < sizeof(rd_tmp_buf) / 16){
break;
}
}
write(c_msg->fd, rd_tmp_buf, strlen(rd_tmp_buf));
printf("鎺ユ敹鍒?lls鎸囦护, 鏈嶅姟鍣ㄦ枃浠跺凡鍙戦€?...\n");
break;
}
//quit
case 2:{
printf("鏂紑鍜孖P涓?s,绔彛涓?%d鐨勫鎴风鐨勮繛鎺ヤ腑...\n", c_msg->cip_addr, c_msg->c_port);
close(c_msg->fd);
pthread_exit(0);
break;
}
//lcd
case 4:{
char* p = strtok(cmd, " ");
p = strtok(NULL, " ");
printf("%s\n", p);
char cwd[PATH_MAX];
if(chdir(p)){
char e_msg[200] = "鍒囨崲浜戠洏鐩綍澶辫触锛岃妫€鏌ュ綋鍓嶄簯鐩樼洰褰曟槸鍚﹀瓨鍦? 褰撳墠浜戠洏鐩綍涓?\n";
getcwd(cwd, sizeof(cwd));
strcat(e_msg, cwd);
write(c_msg->fd, e_msg, strlen(e_msg));
printf("鍒囨崲浜戠洏鐩綍澶辫触锛岃妫€鏌ュ綋鍓嶇洰褰曟槸鍚﹀瓨鍦╘n");
printf("浜戠洏褰撳墠宸ヤ綔鐩綍涓? %s\n", cwd);
}else{
char s_msg[64] = "鍒囨崲浜戠洏鐩綍鎴愬姛, 褰撳墠鐩綍涓?";
getcwd(cwd, sizeof(cwd));
strcat(s_msg, cwd);
write(c_msg->fd, s_msg, strlen(s_msg));
printf("浜戠洏褰撳墠宸ヤ綔鐩綍涓? %s\n", cwd);
}
break;
}
//get
case 5:{
char c[8192];
char* c_p = c;
size_t rd_byte;
char* p = strtok(cmd, " ");
p = strtok(NULL, " ");
printf("%s\n", p);
if(access(p, F_OK) == -1){
write(c_msg->fd ,"0" ,1);
printf("褰撳墠璺緞涓猴紝涓嶅瓨鍦ㄨ鏂囦欢\n");
}else{
int des_fd = open(p, O_RDONLY);
while((rd_byte = read(des_fd, c_p, 128) > 0)){
c_p += rd_byte;
}
write(c_msg->fd ,c ,sizeof(c));
printf("鏂囦欢鍙戦€佹垚鍔?\n");
close(des_fd);
}
break;
}
//put
case 6:{
break;
}
//lpwd
case 7:{
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
write(c_msg->fd, cwd, strlen(cwd));
printf("浜戠洏褰撳墠鎵€鍦ㄦ枃浠剁郴缁熻矾寰勪负: %s\n", cwd);
break;
}
default:{
break;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <fcntl.h>
#define RD_BUFSIZE 1024
#define WR_BUFSIZE 64
char w_buf[WR_BUFSIZE];
char r_buf[RD_BUFSIZE];
void choosecmd(char *cmd, int sockfd);
int main(int argc, char* argv[]){
if(argc != 2){
printf("杈撳叆鍙傛暟鏈夎\n");
printf("瀹㈡埛绔垱寤哄叧闂?..\n");
exit(-1);
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
perror("瀹㈡埛绔垱寤虹鍙eけ璐n");
exit(-1);
}
struct sockaddr_in dest_sin;
socklen_t dest_s_addr = sizeof(dest_sin);
dest_sin.sin_family = AF_INET;
dest_sin.sin_port = htons(atoi(argv[1]));
dest_sin.sin_addr.s_addr = htonl(INADDR_ANY);
if(connect(sockfd,(struct sockaddr *)&dest_sin, dest_s_addr)){
perror("杩炴帴鐩爣鏈嶅姟鍣ㄥけ璐n");
exit(-1);
}
printf("杩炴帴鐩爣鏈嶅姟鍣ㄦ垚鍔?..\n");
for(;;){
memset(w_buf, 0, sizeof(w_buf));
while(fgets(w_buf, sizeof(w_buf), stdin) == NULL);
write(sockfd, w_buf, strlen(w_buf));
choosecmd(w_buf, sockfd);
}
}
void choosecmd(char *cmd, int sockfd){
int cmd_id = 0;
if(strcmp("ls\n", cmd) == 0){
cmd_id = 1;
}
else if(strcmp("lls\n", cmd) == 0){
cmd_id = 2;
}
else if(strcmp("quit\n", cmd) == 0){
cmd_id = 3;
}
else if(strstr(cmd, "lcd ")){
cmd_id = 4;
}
else if(strstr(cmd, "cd ")){
cmd_id = 5;
}
else if(strstr(cmd, "get ")){
cmd_id = 6;
}
else if(strstr(cmd, "put ")){
cmd_id = 7;
}
else if(strcmp(cmd, "pwd\n") == 0){
cmd_id = 8;
}
else if(strcmp(cmd, "lpwd\n") == 0){
cmd_id = 9;
}
printf("cmd_id = %d\n", cmd_id);
switch(cmd_id){
//ls
case 1:{
system("ls");
//char* arg = "ls";
//execl("/bin/ls", arg, NULL);
break;
}
//lls
case 2:{
memset(r_buf, 0, sizeof(r_buf));
read(sockfd, r_buf, sizeof(r_buf));
printf("鏈嶅姟鍣ㄦ枃浠? %s\n", r_buf);
break;
}
//quit
case 3:{
printf("閫€鍑?..\n");
close(sockfd);
exit(1);
break;
}
//lcd
case 4:{
char c[200];
read(sockfd, c, sizeof(c));
printf("%s\n", c);
break;
}
//cd
case 5:{
char* p = strtok(cmd, " ");
p = strtok(NULL, " ");
char cwd[PATH_MAX];
if(chdir(p)){
printf("鍒囨崲鐩綍澶辫触锛岃妫€鏌ュ綋鍓嶅鎴风鐩綍鏄惁瀛樺湪\n");
}
getcwd(cwd, sizeof(cwd));
printf("瀹㈡埛绔綋鍓嶅伐浣滅洰褰曚负: %s\n", cwd);
break;
}
//get
case 6:{
char c[8192];
char* c_p = c;
size_t rd_byte;
char* p = strtok(cmd, " ");
p = strtok(NULL, " ");
while((rd_byte = read(sockfd, c_p, 128)) > 0){
c_p += rd_byte;
}
int des_fd = open(p, O_WRONLY | O_CREAT, 0644);
//姣忔get閮芥槸閲嶆柊瑕嗗啓鍘熸枃浠?
lseek(des_fd, 0, SEEK_SET);
write(des_fd, c, sizeof(c));
close(des_fd);
break;
}
//put
case 7:{
break;
}
//pwd
case 8:{
printf("褰撳墠鏈湴璺緞涓?\n");
system("pwd");
break;
}
//lpwd
case 9:{
char cwd[PATH_MAX];
read(sockfd, cwd, PATH_MAX);
printf("褰撳墠浜戠洏璺緞涓?\n%s\n", cwd);
break;
}
default:{
printf("璇疯緭鍏ヤ互涓嬫寚浠や箣涓€:\n");
printf("-------------------cmd-------------------\n");
printf("1.ls : 鏌ョ湅鏈湴鏂囦欢绯荤粺\n");
printf("2.lls : 鏌ョ湅浜戠洏鏂囦欢绯荤粺\n");
printf("3.cd xx : 鍒囨崲鏈湴鏂囦欢璺緞\n");
printf("4.lcd xx : 鍒囨崲浜戠洏鏂囦欢璺緞\n");
printf("5.puts xx : 灏嗘枃浠跺瓨鍏ヤ簯鐩榎n");
printf("6.gets xx : 鑾峰彇浜戠洏鏂囦欢\n");
printf("7.pwd : 鏌ョ湅鏈湴鏂囦欢绯荤粺璺緞\n");
printf("8.lpwd : 鏌ョ湅浜戠洏鏂囦欢绯荤粺璺緞\n");
break;
}
}
}
标签:FTP,Linux,云盘,cmd,char,fd,printf,msg,buf
From: https://blog.csdn.net/2302_81386929/article/details/141072600