首页 > 代码库 > ftp服务器的简单实现(二)——客户端

ftp服务器的简单实现(二)——客户端

客户端:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define TRUE 1
#define dataLen 1024
char user_cmd[10], cmd_arg[20];
char buf[dataLen];

void cmd_pwd(int sock, int sockmsg);
void cmd_dir(int sock, int sockmsg);
void cmd_cd(int sock, int sockmsg, char *dirName);
void cmd_cdback(int sock, int sockmsg);
void cmd_help(int sock, int sockmsg);
void cmd_get(int sock, int sockmsg, char *fileName);
void cmd_put(int sock, int sockmsg, char *fileName);
void cmd_quit(int sock, int sockmsg);

int main(int argc, char *argv[])
{
	int cmd_len, arg_len;
	int sock, sockmsg;
	struct sockaddr_in server, servermsg;
	struct hostent *hp;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	sockmsg = socket(AF_INET, SOCK_STREAM, 0);
	
	if(sock < 0 || sockmsg < 0)
	{
		perror("opening stream socket");
		exit(1);
	}

	printf("argc is %d, argv[1] is %s\n",argc, argv[1]);

	hp = gethostbyname(argv[1]);
	//hp = gethostbyname("localhost");
	if(hp == 0)
	{
		fprintf(stderr, "%s:unknown host\n", argv[1]);
		exit(2);
	}

	server.sin_family = AF_INET;
	server.sin_port = htons(atoi(argv[2]));
	memcpy((char*)&server.sin_addr, (char*)hp->h_addr, hp->h_length);
	
	servermsg.sin_family = AF_INET;
	servermsg.sin_port = htons(atoi(argv[2]) +1);
	memcpy((char*)&servermsg.sin_addr, (char*)hp->h_addr, hp->h_length);

	if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0 ||
		connect(sockmsg, (struct sockaddr *)&servermsg, sizeof(servermsg)) < 0)
	{
		perror("connecting stream socket");
		exit(1);
	}

	while(TRUE)
	{
		memset(user_cmd, 0, 10);
		memset(cmd_arg, 0, 20);

		//First get the input character by user
		printf("command: ");
		scanf("%s", user_cmd);

		//Then get the command charater and argument
		if(strcmp(user_cmd, "quit") == 0)
		{
			cmd_quit(sock, sockmsg);
			close(sockmsg);
			close(sock);
			printf("connection closed\n\n");
			exit(0);
		}
		else if(strcmp(user_cmd, "?") == 0)
		{
			cmd_help(sock, sockmsg);
		}
		else if(strcmp(user_cmd, "pwd") == 0)
		{
			printf("stcmp pwd --------------->>>\n");
			cmd_pwd(sock, sockmsg);
		}
		else if(strcmp(user_cmd, "dir") == 0)
		{
			cmd_dir(sock, sockmsg);
		}
		else if(strcmp(user_cmd, "cd") == 0)
		{
			scanf("%s", cmd_arg);
			cmd_cd(sock, sockmsg, cmd_arg);
		}
		else if(strcmp(user_cmd, "cd..") == 0)
		{
			cmd_cdback(sock, sockmsg);
		}
		else if(strcmp(user_cmd, "get") == 0 )
		{
			scanf("%s", cmd_arg);
			cmd_get(sock, sockmsg, cmd_arg);
		}
		else if(strcmp(user_cmd, "put") == 0)
		{
			scanf("%s", cmd_arg);
			cmd_put(sock, sockmsg, cmd_arg);
		}
		else
		{
			printf("bad command!\n");
		}
	}
}


void cmd_pwd(int sock, int sockmsg)
{
	//int numRead
	char dirName[30];
	write(sockmsg, user_cmd, sizeof(user_cmd));
	read(sock, dirName, 30);
	printf("dirName is ------->>  %s\n", dirName);
}

//command dir
void cmd_dir(int sock, int sockmsg)
{
	int i, fileNum = 0;
	char fileInfo[50];

	write(sockmsg, user_cmd, sizeof(user_cmd));
	read(sock, &fileNum, sizeof(int));

	printf("---------------------------------------\n");
	printf("file number : %d\n", fileNum);
	if(fileNum > 0)
	{
		for(i=0; i<fileNum; i++)
		{
			memset(fileInfo, 0, sizeof(fileInfo));
			read(sock, fileInfo, sizeof(fileInfo));
			printf("%s\n", fileInfo);
		}
		printf("-----------------------------------\n");
	}
	else if(fileNum == 0)
	{
		printf("directory of server point is empty.\n");
		return;
	}
	else
	{
		printf("error in command ‘dir‘\n");
		return;
	}
} 

void cmd_cd(int sock, int sockmsg, char *dirName)
{
	char currentDirPath[200];
	write(sockmsg, user_cmd, sizeof(user_cmd));
	write(sockmsg, cmd_arg, sizeof(cmd_arg));
	printf("172 ----------->>\n");
	read(sock, currentDirPath, sizeof(currentDirPath));
	printf("174 ------------>>\n");
	printf("now is directory : %s\n", currentDirPath);
}

//command cd..
void cmd_cdback(int sock, int sockmsg)
{
	write(sockmsg, user_cmd, sizeof(user_cmd));
}

//command quit
void cmd_quit(int sock, int sockmsg)
{
	write(sockmsg, user_cmd, sizeof(user_cmd));
}

//command help
void cmd_help(int sock, int sockmsg)
{
	char help[300];
	write(sockmsg, user_cmd, sizeof(user_cmd));
	read(sock, help, 300);

	printf("%s\n", help);
}

//command get
void cmd_get(int sock, int sockmsg, char *fileName)
{
	int fd;
	long fileSize;
	char localFilePath[200];

	write(sockmsg, user_cmd, sizeof(user_cmd));
	write(sockmsg, cmd_arg, sizeof(cmd_arg));
	printf("%s\n%s\n", user_cmd, cmd_arg);

	memset(localFilePath, 0, sizeof(localFilePath));
	getcwd(localFilePath, sizeof(localFilePath));
	strcat(localFilePath, "/");
	strcat(localFilePath, fileName);

	fd = open(localFilePath, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
	if(fd!=-1)
	{
		memset(buf, 0, dataLen);
		read(sock, &fileSize, sizeof(long));
		while(fileSize > dataLen)
		{
			read(sock, buf, dataLen);
			write(fd, buf, dataLen);
			fileSize = fileSize - dataLen;
		}

		read(sock, buf, fileSize);
		write(fd, buf, fileSize);
		close(fd);
		printf("download completed\n");
	}
	else
	{
		printf("open file %s failed\n", localFilePath);
	}
}

//command put
void cmd_put(int sock, int sockmsg, char *fileName)
{
	write(sockmsg, user_cmd, sizeof(user_cmd));
	write(sockmsg, cmd_arg, sizeof(cmd_arg));

	int fd;
	long fileSize;
	int numRead;
	char filePath[200];

	struct stat fileSta;
	
	memset(filePath, 0, sizeof(filePath));
	getcwd(filePath, sizeof(filePath));

	strcat(filePath, "/");
	strcat(filePath, fileName);

	fd = open(filePath, O_RDONLY, S_IREAD);
	if(fd != -1)
	{
		fstat(fd, &fileSta);
		fileSize = (long)fileSta.st_size;

		write(sock, &fileSize, sizeof(long));
		memset(buf, 0, dataLen);
		while(fileSize > dataLen)
		{
			read(fd, buf, dataLen);
			write(sock, buf, dataLen);
			fileSize = fileSize -dataLen;
		}

		read(fd, buf, fileSize);
		write(sock, buf, fileSize);
		close(fd);
		printf("upload completed\n");
	}
	else
	{
		printf("open file %s failed\n", filePath);
	}
}

 编译的话,用gcc -g -o client client.c

运行的话,需要注意,需要两个参数:

./client localhost 3499

一个是本地主机,一个是端口号

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define TRUE 1
#define dataLen 1024
char user_cmd[10], cmd_arg[20];
char buf[dataLen];

void cmd_pwd(int sock, int sockmsg);
void cmd_dir(int sock, int sockmsg);
void cmd_cd(int sock, int sockmsg, char *dirName);
void cmd_cdback(int sock, int sockmsg);
void cmd_help(int sock, int sockmsg);
void cmd_get(int sock, int sockmsg, char *fileName);
void cmd_put(int sock, int sockmsg, char *fileName);
void cmd_quit(int sock, int sockmsg);

int main(int argc, char *argv[])
{
    int cmd_len, arg_len;
    int sock, sockmsg;
    struct sockaddr_in server, servermsg;
    struct hostent *hp;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    sockmsg = socket(AF_INET, SOCK_STREAM, 0);
    
    if(sock < 0 || sockmsg < 0)
    {
        perror("opening stream socket");
        exit(1);
    }

    printf("argc is %d, argv[1] is %s\n",argc, argv[1]);

    hp = gethostbyname(argv[1]);
    //hp = gethostbyname("localhost");
    if(hp == 0)
    {
        fprintf(stderr, "%s:unknown host\n", argv[1]);
        exit(2);
    }

    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    memcpy((char*)&server.sin_addr, (char*)hp->h_addr, hp->h_length);
    
    servermsg.sin_family = AF_INET;
    servermsg.sin_port = htons(atoi(argv[2]) +1);
    memcpy((char*)&servermsg.sin_addr, (char*)hp->h_addr, hp->h_length);

    if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0 ||
        connect(sockmsg, (struct sockaddr *)&servermsg, sizeof(servermsg)) < 0)
    {
        perror("connecting stream socket");
        exit(1);
    }

    while(TRUE)
    {
        memset(user_cmd, 0, 10);
        memset(cmd_arg, 0, 20);

        //First get the input character by user
        printf("command: ");
        scanf("%s", user_cmd);

        //Then get the command charater and argument
        if(strcmp(user_cmd, "quit") == 0)
        {
            cmd_quit(sock, sockmsg);
            close(sockmsg);
            close(sock);
            printf("connection closed\n\n");
            exit(0);
        }
        else if(strcmp(user_cmd, "?") == 0)
        {
            cmd_help(sock, sockmsg);
        }
        else if(strcmp(user_cmd, "pwd") == 0)
        {
            printf("stcmp pwd --------------->>>\n");
            cmd_pwd(sock, sockmsg);
        }
        else if(strcmp(user_cmd, "dir") == 0)
        {
            cmd_dir(sock, sockmsg);
        }
        else if(strcmp(user_cmd, "cd") == 0)
        {
            scanf("%s", cmd_arg);
            cmd_cd(sock, sockmsg, cmd_arg);
        }
        else if(strcmp(user_cmd, "cd..") == 0)
        {
            cmd_cdback(sock, sockmsg);
        }
        else if(strcmp(user_cmd, "get") == 0 )
        {
            scanf("%s", cmd_arg);
            cmd_get(sock, sockmsg, cmd_arg);
        }
        else if(strcmp(user_cmd, "put") == 0)
        {
            scanf("%s", cmd_arg);
            cmd_put(sock, sockmsg, cmd_arg);
        }
        else
        {
            printf("bad command!\n");
        }
    }
}


void cmd_pwd(int sock, int sockmsg)
{
    //int numRead
    char dirName[30];
    write(sockmsg, user_cmd, sizeof(user_cmd));
    read(sock, dirName, 30);
    printf("dirName is ------->>  %s\n", dirName);
}

//command dir
void cmd_dir(int sock, int sockmsg)
{
    int i, fileNum = 0;
    char fileInfo[50];

    write(sockmsg, user_cmd, sizeof(user_cmd));
    read(sock, &fileNum, sizeof(int));

    printf("---------------------------------------\n");
    printf("file number : %d\n", fileNum);
    if(fileNum > 0)
    {
        for(i=0; i<fileNum; i++)
        {
            memset(fileInfo, 0, sizeof(fileInfo));
            read(sock, fileInfo, sizeof(fileInfo));
            printf("%s\n", fileInfo);
        }
        printf("-----------------------------------\n");
    }
    else if(fileNum == 0)
    {
        printf("directory of server point is empty.\n");
        return;
    }
    else
    {
        printf("error in command ‘dir‘\n");
        return;
    }
}

void cmd_cd(int sock, int sockmsg, char *dirName)
{
    char currentDirPath[200];
    write(sockmsg, user_cmd, sizeof(user_cmd));
    write(sockmsg, cmd_arg, sizeof(cmd_arg));
    printf("172 ----------->>\n");
    read(sock, currentDirPath, sizeof(currentDirPath));
    printf("174 ------------>>\n");
    printf("now is directory : %s\n", currentDirPath);
}

//command cd..
void cmd_cdback(int sock, int sockmsg)
{
    write(sockmsg, user_cmd, sizeof(user_cmd));
}

//command quit
void cmd_quit(int sock, int sockmsg)
{
    write(sockmsg, user_cmd, sizeof(user_cmd));
}

//command help
void cmd_help(int sock, int sockmsg)
{
    char help[300];
    write(sockmsg, user_cmd, sizeof(user_cmd));
    read(sock, help, 300);

    printf("%s\n", help);
}

//command get
void cmd_get(int sock, int sockmsg, char *fileName)
{
    int fd;
    long fileSize;
    char localFilePath[200];

    write(sockmsg, user_cmd, sizeof(user_cmd));
    write(sockmsg, cmd_arg, sizeof(cmd_arg));
    printf("%s\n%s\n", user_cmd, cmd_arg);

    memset(localFilePath, 0, sizeof(localFilePath));
    getcwd(localFilePath, sizeof(localFilePath));
    strcat(localFilePath, "/");
    strcat(localFilePath, fileName);

    fd = open(localFilePath, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
    if(fd!=-1)
    {
        memset(buf, 0, dataLen);
        read(sock, &fileSize, sizeof(long));
        while(fileSize > dataLen)
        {
            read(sock, buf, dataLen);
            write(fd, buf, dataLen);
            fileSize = fileSize - dataLen;
        }

        read(sock, buf, fileSize);
        write(fd, buf, fileSize);
        close(fd);
        printf("download completed\n");
    }
    else
    {
        printf("open file %s failed\n", localFilePath);
    }
}

//command put
void cmd_put(int sock, int sockmsg, char *fileName)
{
    write(sockmsg, user_cmd, sizeof(user_cmd));
    write(sockmsg, cmd_arg, sizeof(cmd_arg));

    int fd;
    long fileSize;
    int numRead;
    char filePath[200];

    struct stat fileSta;
    
    memset(filePath, 0, sizeof(filePath));
    getcwd(filePath, sizeof(filePath));

    strcat(filePath, "/");
    strcat(filePath, fileName);

    fd = open(filePath, O_RDONLY, S_IREAD);
    if(fd != -1)
    {
        fstat(fd, &fileSta);
        fileSize = (long)fileSta.st_size;

        write(sock, &fileSize, sizeof(long));
        memset(buf, 0, dataLen);
        while(fileSize > dataLen)
        {
            read(fd, buf, dataLen);
            write(sock, buf, dataLen);
            fileSize = fileSize -dataLen;
        }

        read(fd, buf, fileSize);
        write(sock, buf, fileSize);
        close(fd);
        printf("upload completed\n");
    }
    else
    {
        printf("open file %s failed\n", filePath);
    }
}

ftp服务器的简单实现(二)——客户端