当前位置: 技术问答>linux和unix
利用socket tcp传输文件出现小数目的字节丢失
来源: 互联网 发布时间:2017-03-15
本文导语: 最近在做socket练习,写了两个小程序实现文件的下载。服务器端提供文件下载,客户端连接服务器下载文件,采用tcp传输。只要文件在几十M以上,就会出现数据丢手。一般来说会有1KB以内的数据丢失,请问原因是什...
最近在做socket练习,写了两个小程序实现文件的下载。服务器端提供文件下载,客户端连接服务器下载文件,采用tcp传输。只要文件在几十M以上,就会出现数据丢手。一般来说会有1KB以内的数据丢失,请问原因是什么?下面是代码。
/* server.c - a socket-based file downloaded server
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 15000 /* our service number */
#define HOSTLEN 256
#define oops(msg) { perror(msg) ; exit(1) ; }
int main(int ac, char *av[])
{
struct sockaddr_in saddr; /* build our address here */
struct hostent *hp; /* this is part of our */
char hostname[HOSTLEN]; /* address */
int sock_id,sock_fd; /* line id, file desc */
char buf[BUFSIZ];
char filename[256];
int fd; /* downloaded file */
int nchar;
char *tty;
/*
* Step 1: ask kernel for a socket
*/
sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a socket */
if ( sock_id == -1 )
oops( "socket" );
/*
* Step 2: bind address to socket. Address is host,port
*/
bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct */
gethostname( hostname, HOSTLEN ); /* where am I ? */
hp = gethostbyname( hostname ); /* get info about host */
/* fill in host part */
bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
saddr.sin_port = htons(PORTNUM); /* fill in socket port */
saddr.sin_family = AF_INET ; /* fill in addr family */
if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )
oops( "bind" );
/*
* Step 3: allow incoming calls with Qsize=1 on socket
*/
if ( listen(sock_id, 1) != 0 )
oops( "listen" );
/*
* main loop: accept(), write(), close()
*/
while ( 1 ){
sock_fd = accept(sock_id, NULL, NULL); /* wait for call */
printf("Wow! got a call!n");
if ( sock_fd == -1 )
oops( "accept" ); /* error getting calls */
memset( filename, 0, 256);
read( sock_fd, filename, 256 );
printf( "filename:%sn", filename);
/* send data to clint */
if( (fd = open( filename, O_RDONLY )) == -1 ){
if( dup2( sock_fd, 2 ) == -1 ) /* redirect stderr to sock_fd */
oops("dup2");
perror("");
tty = ttyname(1);
close(2);
if( open( tty, O_RDONLY ) == -1 ){
printf("recover stderr errorn");
exit(1);
}
close(sock_fd); /*release the connection between target server and clint */
continue;
}
/* if no error happen */
write( sock_fd, "ok", 3 );
while( (nchar = read( fd, buf, BUFSIZ )) > 0 ){
if( write( sock_fd, buf, nchar ) != nchar )
oops("write");
}
printf("server finishedn");
close(sock_fd); /*release the connection between target server and clint */
close(fd);
}
}
/* clint.c - a client
* usage: clint servername port file path_to_save
* build: cc clint.c -o clint
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define oops(msg) { perror(msg); exit(1); }
main(int ac, char *av[])
{
struct sockaddr_in servadd; /* the number to call */
struct hostent *hp; /* used to get number */
int sock_id, sock_fd; /* the socket and fd */
char buf[BUFSIZ]; /* to receive message */
char info[BUFSIZ];
int messlen; /* for message length */
int fd;
char *filename;
int nchar;
if( ac != 5 )
oops("usage:clint servername port file path_to_save");
if( strlen(av[3]) >256 || strlen(av[4]) >256 ){
printf("error:file name is too longn");
exit(1);
}
/*
* Step 1: Get a socket
*/
sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a line */
if ( sock_id == -1 )
oops( "socket" ); /* or fail */
/*
* Step 2: connect to server
* need to build address (host,port) of server first
*/
bzero( &servadd, sizeof( servadd ) ); /* zero the address */
hp = gethostbyname( av[1] ); /* lookup host's ip # */
if (hp == NULL)
oops(av[1]); /* or die */
bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
servadd.sin_port = htons(atoi(av[2])); /* fill in port number */
servadd.sin_family = AF_INET ; /* fill in socket type */
/* now dial */
if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0)
oops( "connect" );
/*
* Step 3: transfer data
*/
filename = av[3];
if( write( sock_id, filename,strlen(filename) +1) != strlen( filename ) + 1) /* note:'' */
oops("send file request fail");
read( sock_id, info, BUFSIZ );
if( strcmp( info, "ok") != 0 ){
printf("%sn",info );
close(sock_id );
exit(1);
}
if( (fd = open( av[4],O_WRONLY|O_CREAT, 0755 )) == -1 )
oops("open filename" );
while( ( nchar = read(sock_id, buf, BUFSIZ)) > 0 ){ /* read stuff */
if( write( fd, buf, nchar )!= nchar )
oops("write");
}
printf("donen");
close( sock_id );
close( fd );
}
|
TCP字节流,自己反省去吧。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。