网络通信开发(2)

本文介绍了网络通信开发的实例,包括客户端client.c、服务器端server.c的程序代码,以及使用shell脚本进行的程序操作,展示了完整的执行流程和结果。
摘要由CSDN通过智能技术生成

客户端程序 client.c

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

#define port 3333
 
int  sockclient;
struct sockaddr_in sockaddr1;
char ipaddr[15];


int linkS() 
{	
	if((sockclient=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
	    perror("socket");	
	    exit(0);
	}
		
	memset(&sockaddr1,0,sizeof(sockaddr1));
	sockaddr1.sin_family = AF_INET;
	sockaddr1.sin_addr.s_addr = inet_addr(ipaddr);
	sockaddr1.sin_port = htons(port);
	
	if(connect(sockclient,(struct sockaddr* )&sockaddr1,sizeof(sockaddr1))==-1)
	{
	    perror("connect");
	    exit(0);
	}

	return 1;
}

//~~~~~~~~~~~~~~~~~~~~~~~上传文件~~~~~~~~~~~~~~~~~~~~~~~~~
void upload_file(char *filename)
{	
	int fd;
	char buf[1024];
	int count=0;
	int size = strlen(filename);
	char cmd = 'U';

	struct stat fstat;
		
	if((fd=open(filename,O_RDONLY))==-1)
	{
		perror("open: ");
		return;
	}
	
	/*发送上传命令*/
	write(sockclient,&cmd,1);
	
	/*发送文件名*/
	write(sockclient,(void *)&size,4);
	write(sockclient,filename,size);
	
	/*发送文件长度*/
	if(stat(filename,&fstat)==-1)
		return;
	
	write(sockclient,(void *)&(fstat.st_size),4);
	
	/*发送文件内容*/
	while((count=read(fd,(void *)buf,1024))>0)
	{
		write(sockclient,&buf,count);	
	}		
	
	close(fd);

}
//~~~~~~~~~~~~~~~~~~~~~~~下载文件~~~~~~~~~~~~~~~~~~~~~~~~~

void download_file(char *filename)
{
	int fd;
	char buf[1024];
	int count=0;
	int filesize = 0;
	int tmpsize = 0;
	int namesize = 0;
	char cmd = 'D';
	
	int size = strlen(filename);
	
	/*发送下载命令*/
	write(sockclient,(void *)&cmd,1);
	
	/*发送文件名*/
	write(sockclient,&size,4);
	write(sockclient,filename,size);
	
	/*创建文件*/
	if((fd=open(filename,O_RDWR|O_CREAT,0777))<0)
	{
		perror("open error:\n");	
	}
	
	/*接收文件长度*/
	read(sockclient,&filesize,4);	

	while((count=read(sockclient,(void *)buf,1024))>0)
	{
		write(fd,&buf,count);
		tmpsize += count;
		if(tmpsize==filesize)
			break;	

	}
	
	close(fd);	
}


void quit()
{
	char cmd = 'Q';
	
	write(sockclient,(void *)&cmd,1);
	
	system("clear");
				
	exit(0);	
}

void menu()
{
	char command;
	char file_u[30];
	char file_d[30];
	char tmp;
	char c;
	
	while(1)
	{
		printf("\n------------------------------  1.Upload Files  ------------------------------\n");
		printf("------------------------------  2.Download Files  ------------------------------\n");
		printf("------------------------------      3.Exit   ------------------------------------\n");
		printf("Please input the Client command:");	

		command=getchar();
		
		switch(command)
		{
			case '1':
			{
					printf("Upload File:");
					
					while ((c=getchar()) != '\n' && c != EOF);
					
					fgets(file_u,30,stdin);
					
					file_u[strlen(file_u)-1]='\0';

					upload_file(file_u);
		  	}
			break;
				
			case '2':
				{
					printf("Download Files:");
					
					while ((c=getchar()) != '\n' && c != EOF);
					
					fgets(file_d,sizeof(file_d),stdin);
					
					file_d[strlen(file_d)-1]='\0';
					
					download_file(file_d);
			  	}
				break;
				
			case '3':
				quit();
				
				break;
			
			default:
				printf("Please input right command\n");
				break;
		}
	}
}


int main(int argc,char *args[])
{
    if(argc!=2)
    {
	    printf("format error: you mast enter ipaddr like this : client 192.168.0.6\n");
	    exit(0);
    }
    
    strcpy(ipaddr,args[1]); 
	
    linkS();
    
    menu();
    
    close(sockclient);
    
    return 0;
}

服务器端程序 server.c

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

char clientIP[15];									/*文件列表*/
int sockfd;                      
int new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber = 3333;


void handle(char cmd)
{
	char filename[100];

	int filesize = 0;
	int tmpsize = 0;
	int namesize = 0;
	int count=0;

	int fd;

	
	struct stat fstat;
	char buf[1024];

	switch(cmd)
	{	
		case 'U':
		{	
			
			/*接收文件名*/
			read(new_fd,&namesize,4);
			read(new_fd,(void *)filename,namesize);
			filename[namesize]='\0';
			
			/*创建文件*/
			if((fd=open(filename,O_RDWR|O_CREAT,0777))<0)
			{
				perror("open error:\n");	
			}
			
			/*接收文件大小*/
			read(new_fd,&filesize,4);		

			while((count=read(new_fd,(void *)buf,1024))>0)
			{
				write(fd,&buf,count);
				tmpsize += count;
				if(tmpsize==filesize)
				    break;	
			}
			
			close(fd);	
		}
		break;
		
		case 'D':
		{	
			/* 接收文件名 */
			read(new_fd,&namesize,4);
			read(new_fd,filename,namesize);
			filename[namesize]='\0';
			
			if((fd=open(filename,O_RDONLY))==-1)
			{
				perror("open: ");
				return;
			}
			
			/*发送文件长度*/
			if(stat(filename,&fstat)==-1)
				return;
			
			write(new_fd,&(fstat.st_size),4);
			
			/*发送文件内容*/
			while((count=read(fd,(void *)buf,1024))>0)
			{
				write(new_fd,&buf,count);	
			}
			
			close(fd);

		}
		break;
	}	
}


/*主函数*/
void main()
{
	int i=0;
	char cmd;
	
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
	{
		perror("socket error");	
		exit(-1);
	}

	bzero(&server_addr,sizeof(struct sockaddr_in));
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	server_addr.sin_port=htons(portnumber);	
	
	if(bind(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr))<0)
	{
	    perror("Bind error");
	    exit(1);		
	}
	
	if(listen(sockfd, 5)==-1)
	{
	    perror("listen error");
	    exit(1);
	}	
	
	
	while(1)
	{
		if((new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size)) == -1)
		{
			perror("accept error!");
			exit(-1);
		}
			
		//strcpy(clientIP,inet_ntoa(client_addr.sin_addr)); 				
		
		while(1)
		{
		        /*读取命令*/
			read(new_fd,&cmd,1);

			if(cmd == 'Q')
			{
                            printf("Having a Client exit!\n");
                        	close(new_fd);
				break;
			}
			else
			{
				handle(cmd);
			}
		}
		close(new_fd);		
	}
	close(sockfd);
}


shell脚本程序

rm -rf server
rm -rf client
rm -rf Server
rm -rf Client

gcc server.c -o server
gcc client.c -o client

mkdir Server Client

mv server ./Server
mv client ./Client

执行结果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值