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

📄 server.c

📁 利用Linux开发环境
💻 C
字号:
/*****************************************************************
 ** File Name:server.c
 *  Author:Maxiao
 * Date:2003.5.23
 *****************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <dirent.h>

#define		MAX_INPUT_SIZE	254
#define		SERVICE_PORT	3482
#define		FILEBUF_SIZE	1000

void get(char * filename, int sck);
void put(char * filename, int sck);
void rcd(char * filename, int sck);
void dir(int sck);
void pwd(int sck);
int main(int argc, char *argv[]) {
	char buffer[FILEBUF_SIZE];
	int sck, clnt_sck;
	int clnt_len;
	int i = 0;
	int opt;
	struct sockaddr_in serv_adr, clnt_adr;

	printf("\nWelcome to FTP program server!\n");

	/* Get the socket */
	if ((sck = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("error on socket()");
		exit(1);
	}

	/* This is to avoid the 120-second timeout on the port when the server is killed */
	opt = 1;
	if (setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) {
		perror("setsockopt error");
		exit(2);
	}

	/* Configure the server address, port, etc. */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_adr.sin_port = htons(SERVICE_PORT);

	/* Bind this port to this process */
	if (bind(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("bind error");
		exit(3);
	}

	/* Listen on this port for incoming connections */
	if (listen(sck, 5) < 0) {
		perror("listen error");
		exit(4);
	}

	/* Infinite loop */
	while (1) {
		i=0;
		
		clnt_len = sizeof(clnt_adr);
		/* Accept an incoming request */
		if ((clnt_sck = accept(sck, (struct sockaddr *) &clnt_adr, &clnt_len)) < 0) {
			perror("accept error");
			close(sck);
			exit(5);
		}

		/* Get the first two bytes to see what it is */
		read(clnt_sck, buffer, 2);

		if ((buffer[0] == 'G') && (buffer[1] == 'E')) {
			/* It's a GET */
			read(clnt_sck, buffer, 2); /* Consume the "T " */
			read(clnt_sck, buffer, 2); /* Start reading the filename */
			/* Read until we get a newline or two */
			while ((buffer[i] != '\n') && (buffer[i+1] != '\n')) {
				i += 2;
				read(clnt_sck, &buffer[i], 2);
			}
			/* If we only got one newline, consume the second, then place the null in the proper place */
			if (buffer[i] == '\n') buffer[i] = '\0';
			else {
				read(clnt_sck, &buffer[i+1], 1);
				buffer[i+1] = '\0';
			}

			get(buffer, clnt_sck); /* Do the actual GET */
		} else if ((buffer[0] == 'P') && (buffer[1] == 'U')) {
			/* It's a PUT */
			read(clnt_sck, buffer, 2); /* Consume the "T " */
			read(clnt_sck, buffer, 2); /* Start reading the filename */
			/* Read until we get a newline or two */
			while ((buffer[i] != '\n') && (buffer[i+1] != '\n')) {
				i += 2;
				read(clnt_sck, &buffer[i], 2);
			}
			/* If we only got one newline, consume the second, then place the null in the proper place */
			if (buffer[i] == '\n') buffer[i] = '\0';
			else {
				read(clnt_sck, &buffer[i+1], 1);
				buffer[i+1] = '\0';
			}

			put(buffer, clnt_sck); /* Do the actual PUT */
		} else if ((buffer[0] == 'R') && (buffer[1] == 'C')) {
			/* It's a PUT */
			read(clnt_sck, buffer, 2); /* Consume the "T " */
			read(clnt_sck, buffer, 2); /* Start reading the filename */
			/* Read until we get a newline or two */
			while ((buffer[i] != '\n') && (buffer[i+1] != '\n')) {
				i += 2;
				read(clnt_sck, &buffer[i], 2);
			}
			/* If we only got one newline, consume the second, then place the null in the proper place */
			if (buffer[i] == '\n') buffer[i] = '\0';
			else {
				read(clnt_sck, &buffer[i+1], 1);
				buffer[i+1] = '\0';
			}

			rcd(buffer, clnt_sck); /* Do the actual PUT */
		} else if ((buffer[0] == 'D') && (buffer[1] == 'I')) {
			/* It's an LS */
			read(clnt_sck, buffer, 2); /* Consume the two newlines */
			dir(clnt_sck); /* Perform the LS */
		} else if ((buffer[0] == 'P') && (buffer[1] == 'W')) {
			/* It's an LS */
			read(clnt_sck, buffer, 2); /* Consume the two newlines */
			pwd(clnt_sck); /* Perform the LS */	
		}
		close(clnt_sck); /* Close the incoming client connection */
	}

    exit(0); /* This will never happen */
}

void put(char * filename, int sck) {
	FILE *outfile;
	unsigned char databuf[FILEBUF_SIZE];
	struct stat sbuf;
	int bytes = 0;
	char return_code[2];

	/* stat() the file to see if it's an update or create */
	if (stat(filename, &sbuf) == -1) {
		return_code[0] = 'N'; return_code[1] = 'W';
	} else {
		return_code[0] = 'U'; return_code[1] = 'P';
	}
	/* Return the appropriate status code */
	write(sck, return_code, 2);

	if ((outfile = fopen(filename, "w")) == 0) {
		perror("fopen failed to open file");
		/* Return the error status code */
		return_code[0] = 'E'; return_code[1] = 'R';
		write(sck, return_code, 2);
		return;
	}

	/* Read the incoming bytes and write to the file */
	while ((bytes = read(sck, databuf, FILEBUF_SIZE)) > 0)
		write(fileno(outfile), databuf, bytes);

	/* Close the file */
	fclose(outfile);
}

void get(char * filename, int sck) {
	FILE *infile;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0;

	/* Open up the file locally */
	if ((infile = fopen(filename, "r")) == 0) {
		perror("fopen failed to open file");
		return;
	}

	/* Read from the file and write the bytes to the socket */
	while ((bytes = read(fileno(infile), databuf, FILEBUF_SIZE)) > 0)
		write(sck, databuf, bytes);

	/* Close the file */
	fclose(infile);
}

/* Read from the file and write the bytes to the socket */
void dir(int sck) {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int		n;

	/* Open the pipe to the ls -l command */
	if ((fcmd = popen("ls -l", "r")) == 0) {
		perror("popen error");
		return;
	}

	/* Read from the pipe and write to the socket */
	while ((n = read(fileno(fcmd), buffer, PIPE_BUF)) > 0)
		write(sck, buffer, n);

	/* Close the pipe - we should never get a non-zero return from ls */
	if (pclose(fcmd) != 0) {
		printf("Non-zero return value from \"ls -l\"");
	}
}


void pwd(int sck) {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int		n;

	/* Open the pipe to the ls -l command */
	if ((fcmd = popen("pwd", "r")) == 0) {
		perror("popen error");
		return;
	}

	/* Read from the pipe and write to the socket */
	while ((n = read(fileno(fcmd), buffer, PIPE_BUF)) > 0)
		write(sck, buffer, n);

	/* Close the pipe - we should never get a non-zero return from ls */
	if (pclose(fcmd) != 0) {
		printf("Non-zero return value from \"ls -l\"");
	}
}
void rcd(char *filename, int sck) {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int	n;
	chdir(filename);	
}

⌨️ 快捷键说明

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