- 论坛徽章:
- 0
|
回复 #1 fenglimei0907 的帖子
服务器端代码:
int sockfd,new_fd;
struct sockaddr_in addr,client_addr;
socklen_t sin_size;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
fprintf(stderr,"Socket Error:%s\n",strerror(errno));
exit(1);
}
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(SERVER_PORT);
if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in))<0)
{
fprintf(stderr,"Bind Error:%s\n",strerror(errno));
exit(1);
}
/*监听sockfd描述符*/
if(listen(sockfd, WAITBUF)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
fstream wfile;
char databuf[1024];
wfile.open ("wfile.jpg", ios: ut | ios::binary | ios::trunc);
long file_size=0;//保存图片大小
int nbytes=0;
while(1)
{
/*服务器阻塞,直到客户程序建立连接*/
sin_size=(socklen_t)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));
/*发送start信号,通知客户端开始发送图片*/
printf("server send -start\n" ;
if(send(new_fd,"I am ready",strlen("I am ready" ,0)==-1)
{
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
/*接收图片大小*/
memset(databuf,0,sizeof(databuf));
if((nbytes=recv(new_fd,databuf, 1024,0))==-1)
{
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}
databuf[nbytes]='\0';
file_size=atol(databuf);
printf("recv filesize:%ld\n",file_size);
/*接收图片数据*/
int getnum=0;
do
{
nbytes=0;
memset(databuf,0,sizeof(databuf));
if((nbytes=recv(new_fd,databuf, file_size-getnum,0))==-1)
{
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}
databuf[nbytes]='\0';
printf("receive: %d bytes\n",nbytes);
getnum+=nbytes;
wfile.write(databuf, nbytes);
}while(getnum<file_size);
printf("recv over.\n" ;
/*这个通信已经结束*/
close(new_fd);
/*循环下一个*/
}
close(sockfd);
wfile.close();
客户端代码:
int sockfd,port;
struct sockaddr_in addr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}
if((port=atoi(argv[2]))<0)
{
fprintf(stderr,"Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
fprintf(stderr,"Socket Error:%s\n",strerror(errno));
exit(1);
}
/*填充服务器端的资料*/
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
if(inet_aton(argv[1],&addr.sin_addr)<0)
{
fprintf(stderr,"Ip error:%s\n",strerror(errno));
exit(1);
}
/*客户程序发起连接请求*/
if(connect(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
fstream rfile;
char buffer[1024];
memset(buffer,0,sizeof(buffer));
rfile.open("2.jpg", ios: ut | ios::in | ios::binary);
int len=sizeof(struct sockaddr_in);
/*连接成功*/
int nbytes=0;
while(1)
{/*服务器端发出命令后,才开始发送图片*/
if((nbytes=recv(sockfd,buffer, 1024,0))==-1)
{
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}
buffer[nbytes]='\0';
printf("client recv -%s-\n",buffer);
if(strcmp(buffer,"I am ready" ==0)
{
printf("begin send\n" ;
break;
}
else
{
printf("waiting send\n" ;
}
}
/*获得文件大小*/
FILE* stream;
stream = fopen("2.jpg","r" ;
long file_size;
fseek(stream,0,SEEK_END);
file_size=ftell(stream);
fclose(stream);
sprintf(buffer,"%ld",file_size);
/*首先发送文件大小*/
if(send(sockfd,buffer,strlen(buffer),0)==-1)
{
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
printf("sended filesize:%s\n",buffer);
while(rfile.is_open())
{
memset(buffer,0,sizeof(buffer));
rfile.read(buffer,sizeof(buffer)-1);
int readLen = rfile.gcount();
printf("send: %d bytes\n",readLen);
printf("send: %d bytes\n",strlen(buffer));
// strcpy(buffer,"dfdsd" ;
if(send(sockfd,buffer,strlen(buffer),0)==-1)
{
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
if(rfile.eof())
break;
}
printf("send over\n" ;
/*发出传送完毕信号*/
// send(sockfd,"0",1,0);
rfile.close();
close(sockfd); |
|