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

📄 brt.c.svn-base

📁 Linux下写的 socket 文件传输送程序 服务端和客户端完美统一! 命令行参数分析的完美方式!
💻 SVN-BASE
字号:
/*************************************************************************** *   Copyright (C) 2009 by MicroCai   * *   microcai@sina.com   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <sys/types.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/mman.h>#include <sys/socket.h>#include <netinet/in.h>#include <ctype.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <time.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include "other.h"#define BUFF_SIZE	870/****************************************************//*				协议格式							*/struct pack_hdr{	/*****************魔力数字********/	/***/#define BRT_MHDR	0x1234567u_int32_t	magic_header:	28;   /**/	/*********************************/	/* 所谓魔力数字就是一个特殊的记号,		*	 * 有了这个数字,我们才能很快的发现是否为*	 * 我们的服务器。并且可以和老版本兼容。如	 * 果没有魔力数字,我们就按照老版本办事	 * 这样就做到兼容啦			   	  */	/************魔力数字*************/	/*数据头的大小*/u_int32_t	mode:	4;	u_int16_t	size_hdr;	u_int16_t	buf_size;	//	/*文件大小,63位的数据,对于眼下的文件大小够用了*/	u_int64_t filesize:63;	/*是否有下一个文件*/	u_int64_t	havenext:1;//用最高位表示	char filename[0];};/*****************************************************/int usestd;u_int32_t serverip = 0;char		dir[255]={0};struct cmd_prog	my_args[] ={	{'c', "stdin", BOOL_t, &usestd, sizeof( int ), "read(for server)/write(for client) form stdin/stdout"},	{'d', "dir", STRING_t, &dir, sizeof( dir ), "transfer a whole dir"},	{'s', "server", IP_ADDRESS_V4, &serverip, 4, "server IP"},	{0}};struct remain_args r;int main( long argc, char *argv[] ){	if ( GetConfigs( my_args, argc, argv, &r ) )return 0;	int sockfd =  socket( AF_INET, SOCK_STREAM , IPPROTO_TCP );	if ( sockfd <= 0 )return 1;	struct sockaddr_in addr = {0};	addr.sin_port = htons( 8888 );	addr.sin_family = AF_INET;	addr.sin_addr.s_addr = serverip ;	if ( serverip == 0 )	{		do		{			int fd=1;		//	addr.sin_addr.s_addr = htonl( INADDR_ANY );			if(setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR,&fd, sizeof(fd))<0)					break;			if(bind( sockfd, ( struct sockaddr* )&addr, sizeof( addr )) < 0 )				break;			if ( listen( sockfd, 2 ) )break;			fd = accept( sockfd, 0, 0 );						close(sockfd);						if ( fd < 0 )break;			if ( usestd )			{				char	buf[1];				int ch;				while ( read( 0, buf, 1 ) )					write( fd , buf, 1 );				close( fd );			}			else			{				int f;				int tfd;				struct pack_hdr phdr;				char *BUF = malloc( BUFF_SIZE );				for ( f = 0;f < r.argc;++f )				{					tfd = open( r.argv[f], O_RDONLY );					if ( tfd < 0 )					{						char cmd[512];						sprintf( cmd, "err:cannot open file: %s \n", r.argv[f] );						fputs( cmd, stderr );						continue;					}					struct stat fs = {0};					fstat64( tfd, &fs );					phdr.filesize = fs.st_size;					printf( "transferr : %s\nsize = %dByte \n", r.argv[f], phdr.filesize );					if ( f == r.argc - 1 )						phdr.havenext = 0;					else						phdr.havenext = 1;					int filenamelen = strlen( r.argv[f] ) + 1;					phdr.magic_header = BRT_MHDR;					phdr.buf_size = BUFF_SIZE;					phdr.size_hdr = sizeof( struct pack_hdr ) + filenamelen ;					write( fd, &phdr, sizeof( struct pack_hdr ) );					write( fd, r.argv[f], filenamelen );					u_int64_t t= 0;long  ch;					while (( ch = read( tfd, BUF, BUFF_SIZE ) ) > 0 )					{						t += ch;						write( fd, BUF, ch );						printf( "\r %d Bytes trasfered. (%%%d)", t,(int)((t *100)/ phdr.filesize) );					}					printf("\n");					close( tfd );					fsync(fd);				}				free( BUF );				close( fd );			}			return 0;		}		while ( 0 );		puts( strerror( errno ) );		return 1;	}	else   //这是客户端的事情	{		do		{			int fd;			char buf[BUFF_SIZE];			addr.sin_addr.s_addr = serverip;			if ( addr.sin_addr.s_addr == 0 )			{				perror( "Invalid IP\n" );				break;			}			if ( connect( sockfd, ( struct sockaddr * )&addr, sizeof( addr ) ) < 0 )break;			if ( !usestd )			{				struct pack_hdr phdr;				char *BUF;// = malloc( BUFF_SIZE );				do				{					if ( read( sockfd, &phdr, sizeof( phdr ) ) != sizeof( phdr ) )					{						printf( "server/client mismatch!\n" );						return 1;					}					BUF = malloc( phdr.buf_size );					memset( BUF, 0, phdr.buf_size );					if ( read( sockfd, BUF, phdr.size_hdr - sizeof( phdr ) ) < ( phdr.size_hdr - sizeof( phdr ) ) )					{						printf( "server/client mismatch!\n" );						return 1;					}					int fd = open( BUF, O_WRONLY | O_CREAT );					if ( fd < 0 )					{						fprintf(stderr, "can't create or open file %s \n", BUF );						return 1;					}					int ch;					long fs = 0;					printf( "Now recv %s\n", BUF );					while (( ch = read( sockfd, BUF, phdr.buf_size ) ) > 0 )					{						fs += ch;						if ( write( fd, BUF, ch ) == -1 )break;						if ( fs > ( phdr.filesize - phdr.buf_size ) )						{							ch = read( sockfd, BUF, phdr.filesize - fs );							write( fd, BUF, ch );							break;						}					}					printf( "Done!\n" );					free( BUF );					fchmod(fd,0644);					close(fd);				}				while ( phdr.havenext );				close( sockfd );			}			else			{				while ( read( sockfd, buf, 1 ) > 0 )					write( 1, buf, 1 );				close( sockfd );			}			return 0;		}		while ( 0 );		return 1;	}	return EXIT_SUCCESS;}

⌨️ 快捷键说明

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