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

📄 client.c

📁 实现了linux下的客户端和服务器端
💻 C
📖 第 1 页 / 共 2 页
字号:
	/* Get the socket */
	if ((sck = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("error on socket()");
		return 0;
	}

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command followed by two newlines */
	write(sck, "RL\n\n", 4); 

	printf("Remote directory listing:\n");

	/* Read from the socket and write to stdout */
	while ((bytes = read(sck, databuf, FILEBUF_SIZE)) > 0) {
		write(fileno(stdout), databuf, bytes);
		bytesread += bytes;
	}

	close(sck); /* Close the socket */

	return bytesread;
}

/*ists the contents of the local directory*/   
void ldir() {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int		n;

	printf("Local directory listing:\n");

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

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

	/* Close the pipe */
	if (pclose(fcmd) != 0) {
		printf("Non-zero return value from \"ldir\"");
	}
}

/*Lists a local current directory*/
unsigned int pwd(char * server) {
	int sck;
	struct sockaddr_in serv_adr;
	struct hostent *host;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0, bytesread = 0;
	
	/* Attempt to get the IP for the server by hostname */
	host = gethostbyname(server);
	if (host == (struct hostent *) NULL) {
		perror("gethostbyname failed");
		return 0;
	}

	/* Setup the port for the connection */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
	serv_adr.sin_port = htons(SERVICE_PORT);

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

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command followed by two newlines */
	write(sck, "PW\n\n", 4);
	

	printf("Remote directory listing:\n");

	/* Read from the socket and write to stdout */
	while ((bytes = read(sck, databuf, FILEBUF_SIZE)) > 0) {
		
		write(fileno(stdout), databuf, bytes);
		bytesread += bytes;
	}

	close(sck); /* Close the socket */

	return bytesread;
}     
void lpwd() {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int		n;

	printf("Local directory listing:\n");

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

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

	/* Close the pipe */
	if (pclose(fcmd) != 0) {
		printf("Non-zero return value from \"lpwd\"");
	}
}

unsigned int rcd(char * filename, char * server) {
	int sck;
	struct sockaddr_in serv_adr;
	struct hostent *host;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0, bytesread = 0;
	
	/* Attempt to get the IP for the server by hostname */
	host = gethostbyname(server);
	if (host == (struct hostent *) NULL) {
		perror("gethostbyname failed");
		return 0;
	}

	/* Setup the port for the connection */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
	serv_adr.sin_port = htons(SERVICE_PORT);

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

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command followed by two newlines */
        write(sck, "RC ", 4);
	write(sck, filename, strlen(filename));
	write(sck, "\n\n", 2);

	printf("Current remote directory has changed, now is :\n");

	/* Read from the socket and write to stdout */
	while ((bytes = read(sck, databuf, FILEBUF_SIZE)) > 0) {
		write(fileno(stdout), databuf, bytes);
		bytesread += bytes;
	}

	close(sck); /* Close the socket */

	return bytesread;
}

void lcd(char * filename) {
	FILE	*fcmd;
	char	buffer[PIPE_BUF];
	int		n;
	chdir(filename);
	printf("Current client directory has changed ,now is:\n");
	printf(filename);
	printf("\n");
	
}
unsigned int  mkd(char * pathname, char * server) {

	int sck;
	struct sockaddr_in serv_adr;
	struct hostent *host;
	FILE *infile;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0, bytesread = 0;

	/* Attempt to get the IP for the server by hostname */
	host = gethostbyname(server);
	if (host == (struct hostent *) NULL) {
		perror("gethostbyname failed");
		return 0;
	}

	/* Setup the port for the connection */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
	serv_adr.sin_port = htons(SERVICE_PORT);

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

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command & filename, followed by two newlines */
	write(sck, "MKD ", 4);
	write(sck, pathname, strlen(pathname));
	write(sck, "\n\n", 2);
	}

unsigned int rmd(char * pathname, char * server) {
	int sck;
	struct sockaddr_in serv_adr;
	struct hostent *host;
	FILE *outfile;
	short file_open=0;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0, bytesread = 0;
	
	/* Attempt to get the IP for the server by hostname */
	host = gethostbyname(server);
	if (host == (struct hostent *) NULL) {
		perror("gethostbyname failed");
		return 0;
	}

	/* Setup the port for the connection */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
	serv_adr.sin_port = htons(SERVICE_PORT);

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

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command & filename, followed by two newlines */
	write(sck, "RMD ", 4);
	write(sck, pathname, strlen(pathname));
	write(sck, "\n\n", 2);

	read(databuf,bytes,FILEBUF_SIZE);
	close(sck);
	return bytes;

}


unsigned int dlf(char * filename, char * server) {
	int sck;
	struct sockaddr_in serv_adr;
	struct hostent *host;
	FILE *outfile;
	short file_open=0;
	unsigned char databuf[FILEBUF_SIZE];
	int bytes = 0, bytesread = 0;
	
	/* Attempt to get the IP for the server by hostname */
	host = gethostbyname(server);
	if (host == (struct hostent *) NULL) {
		perror("gethostbyname failed");
		return 0;
	}

	/* Setup the port for the connection */
	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family = AF_INET;
	memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
	serv_adr.sin_port = htons(SERVICE_PORT);

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

	/* Connect to the server */
	if (connect(sck, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
		perror("error on connect()");
		return 0;
	}

	/* Write the command & filename, followed by two newlines */
	write(sck, "DLF ", 4);
	write(sck, filename, strlen(filename));
	write(sck, "\n\n", 2);

	
	close(sck);
}




⌨️ 快捷键说明

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