红联Linux门户
Linux帮助

c语言socket文件传输中buffer缓存出现的问题

发布时间:2011-07-30 14:23:29来源:红联作者:w648795256
新手求助:在socket文件传输中为什么每次读取buffer都会在数据后面出现一个(filename)即文件名
我每次读取buffer都会将buffer里面的内容先清空~!
c/c++客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
#define portnumber 3333
#define FILE_NAME_MAX_SIZE 512
int main(int argc, char *argv[])
{
int sockfd;
char buffer[BUFFER_SIZE];
struct sockaddr_in server_addr;
struct hostent *host;
FILE *fd;

if(argc!=2)
{
fprintf(stderr,"Usage:%s hostname \a\n",argv[0]);
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Gethostname error\n");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}

/* 客户程序填充服务端的资料 */
bzero(&server_addr,sizeof(server_addr)); // 初始化,置0
server_addr.sin_family=AF_INET; // IPV4
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr); // IP地址

/* 客户程序发起连接请求 */
//while(1)
{
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
char filename[FILE_NAME_MAX_SIZE];
bzero(filename,FILE_NAME_MAX_SIZE);
printf("please input which file you want to downlop:\n");
scanf("%s",&filename);
printf("%s\n",filename);
bzero(buffer,BUFFER_SIZE);
strcat(buffer,filename);
printf("%s\n",buffer);
send(sockfd,buffer,BUFFER_SIZE,0);
fd=fopen(filename,"a");
int nbytes=0;
while(1)
{
bzero(buffer,BUFFER_SIZE);
nbytes=recv(sockfd,buffer,sizeof(buffer),0);
printf("nbytes:%d\n",nbytes);
printf("buffer:%s\n",buffer);
printf("strlen(buffer):%d\n",strlen(buffer));
if(strcmp(buffer,"1")==0)
{
break;
}
write(fd,buffer,sizeof(buffer));
}
printf("fjdjjsdfjafjdfj\n");
fclose(fd);
/* 结束通讯 */
close(sockfd);
}
}
这是服务器代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define portnumber 3333
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512

int main(int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
int nbytes;
int n;
FILE *fd;
char buffer[BUFFER_SIZE];
char filename[FILE_NAME_MAX_SIZE];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
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))==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
if(listen(sockfd,5)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}

while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr)); int length;
bzero(buffer,BUFFER_SIZE);
if((length=recv(new_fd,buffer,BUFFER_SIZE,0))<0)
{
printf("read data fail\n");
}
strcpy(filename, buffer);
printf("%s\n",filename);
printf("file do you want to send:\n");
scanf("%s",filename);
fd=fopen(filename,"r");
if(fd!=NULL)
{
fprintf(stderr,"fopen success\n");
}
while(1)
{ bzero(buffer,BUFFER_SIZE);
nbytes=fread(buffer,sizeof(buffer),1,fd);
//nbytes=read(fd,buffer,sizeof(buffer));
printf("%d\n",nbytes);
printf("%s\n",buffer);
if(strcmp(buffer,"")==0)
{
break;
}
send(new_fd,buffer,sizeof(buffer),0);
}
printf("jkkk");
bzero(buffer,BUFFER_SIZE);
strcpy(buffer,"1");
send(new_fd,buffer,sizeof(buffer),0);
fclose(fd);
}
close(sockfd);
/* 结束通讯 */

exit(0);
}
这是在Linux中运行代码时出现的情况:
服务器发送文件名为abc的文件:
root@liu-virtual-machine:/home/liu/code/server# ./ccc
Server get connection from 127.0.0.1
aa
file do you want to send:
abc
fopen success
1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccccccccccccc
dddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
fffffffffffffffffffffffffffffff fffffffffffffffffffffffffffffffffffffffff
ggggggggggggggggggggggggggggggg ggggggggggggggggggggggggggggggggggggggggg
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
lllllllllllllllllllllllllllllll lllllllllllllllllllllllllllllllllllllllll
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
ooabc
0
ooooooooooooooooooooooooooooooo ooooooooooooooooooooooooooooooooooooooo
ppppppppppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppp

客户端接受文件
root@liu-virtual-machine:/home/liu/code/client# ./ddd 127.0.0.1
please input which file you want to downlop:
aa
aa
aa
nbytes:1024
buffer:aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccccccccccc
dddddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffff
gggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
llllllllllllllllllllllllllllllllll llllllllllllllllllllllllllllllllllllll
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
ooaa
strlen(buffer):1026
nbytes:1024
buffer:oooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooo
pppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppp

strlen(buffer):144
nbytes:1024
buffer:1
strlen(buffer):1
fjdjjsdfjafjdfj
文章评论

共有 4 条评论

  1. xujinlinux 于 2011-12-30 09:51:11发表:

    在客户端程序中的这句代码strcat(buffer,filename);将输入的文件名添加到了buffer的后面,这样你每次清空buffer都是不其作用的。

  2. Edward1010 于 2011-12-26 20:16:07发表:

    {:2_91:}

  3. ai0909 于 2011-11-01 14:11:13发表:

    {:2_94:}

  4. D调漫步 于 2011-08-04 10:56:48发表:

    看着很晕