当前位置: 技术问答>linux和unix
socket 实现图片传输?
来源: 互联网 发布时间:2016-08-16
本文导语: 我写的socket程序实现两主机间图片的传输,但传输后要丢失很多数据,图片根本打不开,比如一个500K的图片,传过来后只有20k了。这是服务程序接口: #include"unp.h" extern char current_path[100]; int cmd_pic(int sock,int sockms...
我写的socket程序实现两主机间图片的传输,但传输后要丢失很多数据,图片根本打不开,比如一个500K的图片,传过来后只有20k了。这是服务程序接口:
#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,10,26,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);
}
#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,10,26,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);
}
|
代码