#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