当前位置: 技术问答>linux和unix
socket传送文件,丢失数据???
来源: 互联网 发布时间:2016-08-15
本文导语: 我写的socket程序,实现两主机间文件的传输,但不论是传输文本文件还是二进制文件在文件的最后都要丢失一两k的数据 这是服务接口: [code=C/C++][/#include"unp.h" extern char current_path[100]; int cmd_pic(int sock,int sockmsg) { p...
我写的socket程序,实现两主机间文件的传输,但不论是传输文本文件还是二进制文件在文件的最后都要丢失一两k的数据
这是服务接口:
[code=C/C++][/#include"unp.h"
extern char current_path[100];
int cmd_pic(int sock,int sockmsg)
{
printf("1n");//debug
FILE *fd;
char buff[MAXSIZE];
size_t read_bytes;
char file_name[100];
bzero(buff,MAXSIZE);
bzero(file_name,100);
if( recv(sockmsg,file_name,100,0) == -1 )
err_sys("recv error");
strcat(current_path,"/");
strcat(current_path,file_name);
printf("debug:%sn",current_path);//debug
if(( fd = fopen(current_path,"rb")) == NULL )
err_sys("fopen error");
while(( read_bytes = fread(buff,sizeof(char),MAXSIZE/sizeof(char),fd)) != 0 )
{
if(send(sock,buff,read_bytes,0) == -1 )
err_sys("send error");
bzero(buff,MAXSIZE);
}
if(ferror(fd))
{
printf("fread errorn");
exit(1);
}
return(0);
}]
这是客服接口:
[code=C/C++][/#include"unp.h"
int client_cmd_pic(int sock,int sockmsg)
{
char file_name[100];
char buff[1024];
ssize_t n;
FILE *fd;
bzero(file_name,100);
bzero(buff,1024);
if( send(sockmsg,"get",3,0) == -1 )
err_sys("cmd send error");
printf("input the file namen");
scanf("%s",file_name);
if( send(sockmsg,file_name,strlen(file_name),0) == -1 )
err_sys("file_name send error");
if(chdir("/home/hexiaogang/Desktop") == -1 )
err_sys("chdir error");
if(( fd = fopen("kk","ab")) == NULL )
{
printf("fopen errorn");
exit(1);
}
while(( n = recv(sock,buff,1024,0)) != 0 )
{
if(n == -1 && errno == EINTR )
continue;
if( n == -1 )
err_sys("recv error");
fwrite(buff,sizeof(char),n/sizeof(char),fd);
}
fflush(fd);
fclose(fd);
return(0);
}
]
这是服务接口:
[code=C/C++][/#include"unp.h"
extern char current_path[100];
int cmd_pic(int sock,int sockmsg)
{
printf("1n");//debug
FILE *fd;
char buff[MAXSIZE];
size_t read_bytes;
char file_name[100];
bzero(buff,MAXSIZE);
bzero(file_name,100);
if( recv(sockmsg,file_name,100,0) == -1 )
err_sys("recv error");
strcat(current_path,"/");
strcat(current_path,file_name);
printf("debug:%sn",current_path);//debug
if(( fd = fopen(current_path,"rb")) == NULL )
err_sys("fopen error");
while(( read_bytes = fread(buff,sizeof(char),MAXSIZE/sizeof(char),fd)) != 0 )
{
if(send(sock,buff,read_bytes,0) == -1 )
err_sys("send error");
bzero(buff,MAXSIZE);
}
if(ferror(fd))
{
printf("fread errorn");
exit(1);
}
return(0);
}]
这是客服接口:
[code=C/C++][/#include"unp.h"
int client_cmd_pic(int sock,int sockmsg)
{
char file_name[100];
char buff[1024];
ssize_t n;
FILE *fd;
bzero(file_name,100);
bzero(buff,1024);
if( send(sockmsg,"get",3,0) == -1 )
err_sys("cmd send error");
printf("input the file namen");
scanf("%s",file_name);
if( send(sockmsg,file_name,strlen(file_name),0) == -1 )
err_sys("file_name send error");
if(chdir("/home/hexiaogang/Desktop") == -1 )
err_sys("chdir error");
if(( fd = fopen("kk","ab")) == NULL )
{
printf("fopen errorn");
exit(1);
}
while(( n = recv(sock,buff,1024,0)) != 0 )
{
if(n == -1 && errno == EINTR )
continue;
if( n == -1 )
err_sys("recv error");
fwrite(buff,sizeof(char),n/sizeof(char),fd);
}
fflush(fd);
fclose(fd);
return(0);
}
]
|
噢,那就用setsockopt设置SO_LINGER选项然后close掉套接口,具体可以参看UNP第七章