⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.c

📁 在Linux系统上使用Socket接口实现FTP客户端和服务器的程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/fcntl.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <errno.h>#include <string.h>#include <arpa/inet.h>#define BUFF 1024#define MINIBUF 512#define server_port 4009int getSocket(int* port){	int ret;			register int socketfd;		// server 描述符 	struct sockaddr_in local;	// 本地地址		if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){		fprintf(stderr, "fail to initial socket!\n");		exit(1);	}	local.sin_family = AF_INET;	local.sin_addr.s_addr = INADDR_ANY;	local.sin_port = htons(*port);	if ((ret = bind(socketfd, (struct sockaddr *)&local, sizeof(local))) == -1){		fprintf(stderr, "Bind unseccessfully!\n");		exit(2);	}	int len = sizeof(local);	getsockname(socketfd,(struct sockaddr*)&local, &len);	*port = ntohs(local.sin_port);	return socketfd;}void contact(int clientSocketfd, int number){	char buf[BUFF];	int dataSockfd;	char file[BUFF];	int rs,src,target,k;		char root[BUFF];	FILE* fp=popen("pwd","r");	fgets(root,BUFF,fp);	pclose(fp);		while(1){		if(read(clientSocketfd,buf,BUFF)>0){			printf("The command from the clientd%d is %s\n", number, buf);			//printf("!!%c!!%c!!%c!!\n", buf[0],buf[1],buf[2]);		}		if(buf[0] == 'Q' && buf[1] == 'U' && buf[2] == 'I' && buf[3] == 'T'){			printf("The client %d quits", number);			return;		}		else if (buf[0] == 'P' && buf[1] == 'W' && buf[2] == 'D'){			FILE* fp=popen("pwd","r");			if((fgets(file,BUFF,fp)) != NULL){				printf("Current directory is : %s", file);				write(clientSocketfd, (char *)(file), BUFF);			}			pclose(fp);		}		else if (buf[0] == 'D' && buf[1] == 'I' && buf[2] == 'R'){			FILE * pf=popen("ls","r");			while(fgets(file,BUFF,pf) != NULL){				write(clientSocketfd,file,BUFF);			}			pclose(pf);			write(clientSocketfd,"\n",BUFF);		}		else if (buf[0] == 'C' && buf[1] == 'D' && buf[2] == 'U' && buf[3] == 'P'){			FILE* fp=popen("pwd","r");			if((fgets(file,BUFF,fp)) != NULL){				if (strcmp(file, root) == 0){					printf("can't access father dir!\n");					write(clientSocketfd,"\n",BUFF);					continue;				}				int i;				for (i = 0; file[i] != '\0'; i++){}				for (; file[i] != '/'; i--){}				file[i] = '\0';			}			pclose(fp);			rs = chdir(file);			if(rs == 0){				FILE* fp=popen("pwd","r");				if((fgets(file,BUFF,fp)) != NULL){					printf("Current directory is : %s", file);					write(clientSocketfd,(char *)(file),BUFF);				}				pclose(fp);			}			else{				write(clientSocketfd,"\n",BUFF);			}		}				else if (buf[0] == 'C' && buf[1] == 'D'){			rs = chdir((char*)(buf+3));			if(rs== 0){				FILE* fp=popen("pwd","r");				if((fgets(file,BUFF,fp)) != NULL){					printf("Current directory is : %s", file);					write(clientSocketfd,(char *)(file),BUFF);				}				pclose(fp);			}			else{				write(clientSocketfd,"\n",BUFF);			}		}		else if(buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T'){			printf("Loading starts!\n");			strcpy(file, (char *)(buf+4));			if((src = open(file,O_RDONLY))<0){				printf("file %s is not exist!\n", file);				write(clientSocketfd,"ERROR",BUFF);			}			else{				int port = 0;				dataSockfd = getSocket(&port);				listen(dataSockfd, 20);				sprintf(buf, "%d", port);				write(clientSocketfd, buf, BUFF);	// send "<dataSocketPort>"				read(clientSocketfd, buf, BUFF);	// receive "READY" or "ERROR"				//printf("here %s\n",buf);				if (buf[0] == 'E'){					close(src);					close(dataSockfd);					continue;				}				struct sockaddr_in client_in;		// client address				// accept				int len = sizeof(struct sockaddr_in);				int csk;				if ((csk = accept(dataSockfd, 					(struct sockaddr *)&client_in, &len)) == -1){					printf("accept error\n");				}				lseek(src, 0, SEEK_SET);				memset(buf, 0x0 ,BUFF);				while((k = read(src,buf,BUFF)) > 0){								write(csk, buf, k);				}				write(csk, 0, 0);			}			close(src);			close(dataSockfd);			printf("Loading finishes\n");		}				else if(buf[0] == 'P' && buf[1] == 'U'&& buf[2] == 'T')		{			strcpy(file, (char *)(buf+4));			FILE * res=popen("ls","r");			int isExist = 0;			while(fgets(buf,BUFF,res) != NULL){				int i;				for (i = 0; buf[i] != '\n'; i++){}				buf[i] = '\0';				if (strcmp(buf, file) == 0){					printf("File %s has already existed!\n", file);					write(clientSocketfd, "ERROR", BUFF);	// send "ERROR"					isExist = 1;					break;				}			}			if (isExist == 1)				continue;			pclose(res);						if((target = open(file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0){				printf("error to write to file %s!\n", file);				write(clientSocketfd, "ERROR", BUFF);	// send "ERROR"				continue;			}			else{				int port = 0;				dataSockfd = getSocket(&port);				listen(dataSockfd, 20);				sprintf(buf, "%d", port);				write(clientSocketfd, buf, BUFF);	// send "<dataSocketPort>"								struct sockaddr_in client_in;		// client address				// accept				int len = sizeof(struct sockaddr_in);				int csk;				if ((csk = accept(dataSockfd, (struct sockaddr *)&client_in, &len)) == -1){					printf("accept error\n");				}				while ((k = read(csk, buf, BUFF)) > 0){					write(target, buf, k);					if (k != BUFF)						break;				}			}			close(target);			close(dataSockfd);			printf("Puting has finished\n");		}					}}int main(int argc, char *argv[]){	char command[MINIBUF];		// 接收的命令		int clientNum = 0;		// client数	int ret;				int socketfd;			// server socket描述符	int clientSocketfd;		// client socket描述符	char buf[BUFF];                  //	int port = server_port;	socketfd = getSocket(&port);		// listen	listen(socketfd, 20);	while (1){		struct sockaddr_in client_in;		// client address		int len = sizeof(struct sockaddr_in);		if ((clientSocketfd = accept(socketfd, (struct sockaddr *)&client_in, &len)) == -1){			printf("accept error\n");		}			clientNum++;		sprintf(buf, "%d", clientNum);		printf("Client %s is connected\n", buf);		printf("Client name: %s\n", inet_ntoa(client_in.sin_addr));				int pid;                   //多线程		switch (pid = fork()){		case 0:			close(socketfd);			write(clientSocketfd, buf, BUFF);  //tell the client which number it is 			contact(clientSocketfd, clientNum);			close(clientSocketfd);			exit(1);		case -1:			printf("fork error\n");			break;		default:			close(clientSocketfd);			break;		}	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -