socket图片文件传输示例.docx
《socket图片文件传输示例.docx》由会员分享,可在线阅读,更多相关《socket图片文件传输示例.docx(14页珍藏版)》请在冰豆网上搜索。
socket图片文件传输示例
从前在网上找过很多sock编程的资料,都很高深。
。
对于入门级的我来说,简单又不简陋的小例子更好一些,所以总结一下刚写的小例子,
用select模型实现的jpg文件传输,并不完善,只作为今后的一个参考。
代码经过调试,应该能用吧。
服务器端:
#pragmacomment(lib,"ws2_32.lib")
#include
#defineBUFLENsizeof(buf)
intmain()
{
WSADATAwsaData;
SOCKADDR_INseraddr,cliaddr;
SOCKETlistensocket,newconnection,tempsocket;
ULONGsockmode;
intsockok,totalsock=0;
structfd_setfdwrite,fdread;
charbuf[10],buf0[10];
union{
chara[4];
intb;
}a,flag;
a.b=flag.b=0;
intcliaddrlen=sizeof(cliaddr);
FILE*fp;
longcode;
intn,he=0,m=0,k=0,b=0;
char*p;
if(WSAStartup(MAKEWORD(2,0),&wsaData)!
=0)
printf("error1");
return-1;}
if((listensocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
printf("error2");
return-1;
}
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons(8889);
seraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(SOCKET_ERROR==bind(listensocket,(SOCKADDR*)&seraddr,sizeof(seraddr)))
printf("error3");
if(SOCKET_ERROR==listen(listensocket,5))
printf("error4");
printf("waitask");
sockmode=1;
if(SOCKET_ERROR==ioctlsocket(listensocket,FIONBIO,&sockmode))
printf("erroronioctl");
if((fp=fopen("output.jpg","rb"))==NULL)//指定的文件是否存在
{//指定文件不存在
printf("不能打开指定的文件!
\n");
fseek(fp,0,SEEK_END);//读写指针移动到文件结尾
k=a.b=ftell(fp);
code=htonl(m);//返回当前读写指针的位置,即得到文件长度。
p=(char*)&k;
while
(1)
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_SET(listensocket,&fdread);
for(inti=0;i{FD_SET(tempsocket,&fdread);FD_SET(tempsocket,&fdwrite);}if(SOCKET_ERROR==(sockok=select(NULL,&fdread,&fdwrite,NULL,NULL))){printf("erroronselect");return-1;}if(FD_ISSET(listensocket,&fdread)){sockok--;if(INVALID_SOCKET==(newconnection=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrlen))){printf("error5");return-1;}else{printf("come");totalsock++;tempsocket=newconnection;}if(SOCKET_ERROR==ioctlsocket(newconnection,FIONBIO,&sockmode)){printf("erroronioctl");return-1;}}memset(buf,0,sizeof(buf));//memset(buf0,0,sizeof(buf0));if(FD_ISSET(newconnection,&fdread)){memset(buf,0,sizeof(buf));if(flag.b==2){if(recv(newconnection,flag.a,sizeof(flag),0)<0){printf("erroronrecvlong");return-1;}//if(a.b==0)a.b=2;}//else//if(recv(newconnection,buf,sizeof(buf),0)==SOCKET_ERROR)//{//printf("erroronrecvack");//return-1;//}//if(strcmp(buf,"exit")==0)//{//printf("conpletecopy");//break;//}}if(FD_ISSET(newconnection,&fdwrite)){//gets(buf0);sockok--;if(a.b&&flag.b==0){if((b=send(newconnection,a.a,sizeof(a),0))<0){printf("erroronsend");return-1;//memset(a.a,0,sizeof(a));}flag.b=2;}if(flag.b==1){//打开文件,并把文件内容发送到客户方,然后关闭文件和套接字rewind(fp);//文件读写指针移动到文件开头while(!feof(fp))//文件未结束{memset(buf,0,BUFLEN);//清除缓存中的内容n=fread(buf,1,BUFLEN,fp);//读取文件内容到缓存if(m=send(newconnection,buf,n,0)){if(m<0){printf("erroronsendmain");m=GetLastError();return-1;}if((he+=m)>=k)printf("copycomplete");}//send(clientSock,buf,n,0);//把文件内容发送给客户方}fclose(fp);break;}//if(strcmp(buf0,"exit")==0)//break;} }WSACleanup();gets(buf);printf("exittoexit");if(strcmp("exit",buf))return0; }客户端:#pragmacomment(lib,"ws2_32.lib")#include#include#includeintmain(){WSADATAwsaData;SOCKADDR_INcliaddr;SOCKETscon;charbuf0[50];intn=0,m=0;intj=0,k=0,l=0,q=0;union{chara[4];intb;}a;//存储文件长度,用字符型指针做接收发送api的参数,然后用int型运算intsockok;structfd_setfdwrite,fdread;intcliaddrlen=sizeof(cliaddr);ULONGsockmode=1;FILE*fp;if(WSAStartup(MAKEWORD(2,0),&wsaData)!=0)//初始化套接字空间{printf("error1");return-1;}if((scon=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)//创建套接字{printf("error2");return-1;}cliaddr.sin_family=AF_INET;//初始化为ipv4族cliaddr.sin_port=htons(8889);//端口号cliaddr.sin_addr.s_addr=inet_addr("127.0.0.1");//ip地址/*if(SOCKET_ERROR==bind(scon,(SOCKADDR*)&cliaddr,cliaddrlen))//显示绑定套接字与地址信息{printf("error3");return-1;}*/printf("connect...\n");if(SOCKET_ERROR==connect(scon,(SOCKADDR*)&cliaddr,cliaddrlen))//连接远端主机{printf("error4connectfailed");return-1;}printf("done\n"); if(SOCKET_ERROR==ioctlsocket(scon,FIONBIO,&sockmode))//修改套接字为非阻塞{printf("erroronioctl");return-1;}if(!(fp=fopen("input.jpg","wb+")))//打开已有或创建一个二进制文件{printf("erroronfopen");}for(inti=0;;i++)//死循环{FD_ZERO(&fdwrite);FD_ZERO(&fdread);FD_SET(scon,&fdwrite);FD_SET(scon,&fdread);if((sockok=select(0,&fdread,&fdwrite,NULL,NULL))<0)//选择出可读写的套接字用形参接收{printf("erroronselect");return-1;}if(m==-2)break;//完成传送文件标志位m==-2//memset(buf,0,sizeof(buf));memset(buf0,0,sizeof(buf0));//清缓存//gets(buf);//if(strcmp("exit",buf)==0)//{//printf("exit..");//return0;//}/*if(send(scon,buf,strlen(buf),0)==SOCKET_ERROR){continue;}else{printf(buf0);printf("\n");}*///gets(p);//n=(long)*p;while(FD_ISSET(scon,&fdread)&&(sockok--)&&(m!=-2))//判断是否有可读套接字{if(l==0)//文件长度接收标志l==1为接收了{if((q=recv(scon,a.a,sizeof(a),0))<0)//接收文件长度{printf("erroronrecvlong");return-1;}l=1;}if(l==2)//文件长度接收并返回确认信息了{memset(buf0,0,sizeof(buf0));while((n=recv(scon,buf0,sizeof(buf0),0))>0)//接收主文件{//continue;m=fwrite(buf0,n,1,fp);memset(buf0,0,sizeof(buf0));if((m==1)&&((a.b-=n)<=0))//文件写入本地且文件接收长度达到文件长度 结束{cout<cout<cout<send(scon,"exit",strlen("exit"),0);printf("completecopy");m=-2;//printf("complatecopy");break;}}}}if(FD_ISSET(scon,&fdwrite)&&sockok--){if(l==1){if(send(scon,(char*)&l,sizeof(&l),0)<0)//确认收到文件长度{printf("erroronsend");return-1;}l=2;}}/*else{printf(buf0);printf("\n");}*/} if(fp){fclose(fp);}closesocket(scon);WSACleanup(); }
FD_SET(tempsocket,&fdread);
FD_SET(tempsocket,&fdwrite);
if(SOCKET_ERROR==(sockok=select(NULL,&fdread,&fdwrite,NULL,NULL)))
printf("erroronselect");
if(FD_ISSET(listensocket,&fdread))
sockok--;
if(INVALID_SOCKET==(newconnection=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrlen)))
printf("error5");
else
printf("come");
totalsock++;
tempsocket=newconnection;
if(SOCKET_ERROR==ioctlsocket(newconnection,FIONBIO,&sockmode))
memset(buf,0,sizeof(buf));
//memset(buf0,0,sizeof(buf0));
if(FD_ISSET(newconnection,&fdread))
if(flag.b==2)
if(recv(newconnection,flag.a,sizeof(flag),0)<0)
printf("erroronrecvlong");
//if(a.b==0)a.b=2;
//else
//if(recv(newconnection,buf,sizeof(buf),0)==SOCKET_ERROR)
//{
//printf("erroronrecvack");
//return-1;
//}
//if(strcmp(buf,"exit")==0)
//printf("conpletecopy");
//break;
if(FD_ISSET(newconnection,&fdwrite))
//gets(buf0);
if(a.b&&flag.b==0)
if((b=send(newconnection,a.a,sizeof(a),0))<0)
printf("erroronsend");
//memset(a.a,0,sizeof(a));
flag.b=2;
if(flag.b==1)
//打开文件,并把文件内容发送到客户方,然后关闭文件和套接字
rewind(fp);//文件读写指针移动到文件开头
while(!
feof(fp))//文件未结束
memset(buf,0,BUFLEN);//清除缓存中的内容
n=fread(buf,1,BUFLEN,fp);//读取文件内容到缓存
if(m=send(newconnection,buf,n,0))
if(m<0)
printf("erroronsendmain");
m=GetLastError();
if((he+=m)>=k)
printf("copycomplete");
}//send(clientSock,buf,n,0);//把文件内容发送给客户方
fclose(fp);
break;
//if(strcmp(buf0,"exit")==0)
WSACleanup();
gets(buf);
printf("exittoexit");
if(strcmp("exit",buf))
return0;
客户端:
SOCKADDR_INcliaddr;
SOCKETscon;
charbuf0[50];
intn=0,m=0;
intj=0,k=0,l=0,q=0;
}a;//存储文件长度,用字符型指针做接收发送api的参数,然后用int型运算
intsockok;
ULONGsockmode=1;
=0)//初始化套接字空间
if((scon=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)//创建套接字
cliaddr.sin_family=AF_INET;//初始化为ipv4族
cliaddr.sin_port=htons(8889);//端口号
cliaddr.sin_addr.s_addr=inet_addr("127.0.0.1");//ip地址
/*if(SOCKET_ERROR==bind(scon,(SOCKADDR*)&cliaddr,cliaddrlen))//显示绑定套接字与地址信息
*/
printf("connect...\n");
if(SOCKET_ERROR==connect(scon,(SOCKADDR*)&cliaddr,cliaddrlen))//连接远端主机
printf("error4connectfailed");
printf("done\n");
if(SOCKET_ERROR==ioctlsocket(scon,FIONBIO,&sockmode))//修改套接字为非阻塞
if(!
(fp=fopen("input.jpg","wb+")))//打开已有或创建一个二进制文件
printf("erroronfopen");
for(inti=0;;i++)//死循环
FD_SET(scon,&fdwrite);
FD_SET(scon,&fdread);
if((sockok=select(0,&fdread,&fdwrite,NULL,NULL))<0)//选择出可读写的套接字用形参接收
if(m==-2)break;//完成传送文件标志位m==-2
//memset(buf,0,sizeof(buf));
memset(buf0,0,sizeof(buf0));//清缓存
//gets(buf);
//if(strcmp("exit",buf)==0)
//printf("exit..");
//return0;
/*if(send(scon,buf,strlen(buf),0)==SOCKET_ERROR)
continue;
printf(buf0);
printf("\n");
//gets(p);
//n=(long)*p;
while(FD_ISSET(scon,&fdread)&&(sockok--)&&(m!
=-2))//判断是否有可读套接字
if(l==0)//文件长度接收标志l==1为接收了
if((q=recv(scon,a.a,sizeof(a),0))<0)//接收文件长度
l=1;
if(l==2)//文件长度接收并返回确认信息了
memset(buf0,0,sizeof(buf0));
while((n=recv(scon,buf0,sizeof(buf0),0))>0)//接收主文件
//continue;
m=fwrite(buf0,n,1,fp);
if((m==1)&&((a.b-=n)<=0))//文件写入本地且文件接收长度达到文件长度 结束
cout<cout<cout<send(scon,"exit",strlen("exit"),0);printf("completecopy");m=-2;//printf("complatecopy");break;}}}}if(FD_ISSET(scon,&fdwrite)&&sockok--){if(l==1){if(send(scon,(char*)&l,sizeof(&l),0)<0)//确认收到文件长度{printf("erroronsend");return-1;}l=2;}}/*else{printf(buf0);printf("\n");}*/} if(fp){fclose(fp);}closesocket(scon);WSACleanup(); }
cout<cout<send(scon,"exit",strlen("exit"),0);printf("completecopy");m=-2;//printf("complatecopy");break;}}}}if(FD_ISSET(scon,&fdwrite)&&sockok--){if(l==1){if(send(scon,(char*)&l,sizeof(&l),0)<0)//确认收到文件长度{printf("erroronsend");return-1;}l=2;}}/*else{printf(buf0);printf("\n");}*/} if(fp){fclose(fp);}closesocket(scon);WSACleanup(); }
cout<send(scon,"exit",strlen("exit"),0);printf("completecopy");m=-2;//printf("complatecopy");break;}}}}if(FD_ISSET(scon,&fdwrite)&&sockok--){if(l==1){if(send(scon,(char*)&l,sizeof(&l),0)<0)//确认收到文件长度{printf("erroronsend");return-1;}l=2;}}/*else{printf(buf0);printf("\n");}*/} if(fp){fclose(fp);}closesocket(scon);WSACleanup(); }
send(scon,"exit",strlen("exit"),0);
printf("completecopy");
m=-2;
//printf("complatecopy");
if(FD_ISSET(scon,&fdwrite)&&sockok--)
if(l==1)
if(send(scon,(char*)&l,sizeof(&l),0)<0)//确认收到文件长度
l=2;
/*else{
}*/
if(fp)
closesocket(scon);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1