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

📄 client_send_receive.c

📁 一个网络传输文件程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include "client_send_receive.h"#define MAXBUF 1024int client_send_receive(int sockfd, int trans_type, char *clientpath, char *servpath){	char buf[MAXBUF]={0};	int fd, n;	if(trans_type==0){						/*** put(client to server) ***/			write(sockfd,"CTS",3);		n=read(sockfd,buf, MAXBUF);		buf[n]='\0';		if(strcmp(buf,"RTR")==0){			/*** Client Ready To Send ***/			write(sockfd, servpath, strlen(servpath));			bzero(buf,MAXBUF);			n=read(sockfd, buf, MAXBUF);			buf[n]='\0';			if((strcmp(buf, "DERR"))==0){				printf("Sorry, Server no such Directory, Please check it\n");				return 1;			}			else if(strcmp(buf, "OK")==0){				fd=open(clientpath, O_RDONLY);				if(fd<0){					perror("fail to open");					return 1;				}				printf("transmiting...\n");				while((n=read(fd, buf, MAXBUF))>0){					write(sockfd, buf, n);				}				close(fd);			}		}	}	else if(trans_type==1){					/*** get(server to client) ***/			write(sockfd,"STC",3);		n=read(sockfd,buf, MAXBUF);		buf[n]='\0';		if(strcmp(buf,"RTS")==0){			/*** client Ready To Read ***/			write(sockfd, servpath, strlen(servpath));			bzero(buf,MAXBUF);			n=read(sockfd, buf, MAXBUF);			buf[n]='\0';			if((strcmp(buf, "FERR"))==0){				printf("Sorry, Server no such File, Please check it\n");				return 1;			}			else if(strcmp(buf, "OK")==0){				write(sockfd,"hh",2);				fd=open(clientpath, O_WRONLY | O_CREAT | O_TRUNC, 0644);				if(fd<0){					perror("file to open");					return 1;				}				printf("transmiting...\n");				while((n=read(sockfd, buf, MAXBUF))>0){					write(fd, buf, n);				}				close(fd);			}		}	}	printf("finish\n");	return 0;}

⌨️ 快捷键说明

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