1、socket图片文件传输示例从前在网上找过很多编程的资料,都很高深。对于入门级的我来说,简单又不简陋的小例子更好一些,所以总结一下刚写的小例子,用select模型实现的文件传输,并不完善,只作为今后的一个参考。代码经过调试,应该能用吧。服务器端:#pragma comment (lib,ws2_32.lib)#include #include #define BUFLEN sizeof(buf)int main() WSADATA wsaData; SOCKADDR_IN seraddr,cliaddr; SOCKET listensocket,newconnection,tempsocket
2、; ULONG sockmode; int sockok,totalsock=0; struct fd_set fdwrite,fdread; char buf10,buf010; union char a4; int b; a,flag; a.b=flag.b=0; int cliaddrlen=sizeof(cliaddr); FILE *fp; long code; int n,he=0,m=0,k=0,b=0; char *p; if ( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 ) printf(error1); return -1; if
3、(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); return -1; if
4、(SOCKET_ERROR=listen(listensocket,5) printf(error4); return -1; printf(wait ask); sockmode=1; if(SOCKET_ERROR=ioctlsocket(listensocket,FIONBIO,&sockmode) printf(error on ioctl); return -1; if(fp=fopen(output.jpg,rb)=NULL) /指定的文件是否存在 /指定文件不存在 printf(不能打开指定的文件!n); fseek(fp,0,SEEK_END); /读写指针移动到文件结尾 k=
5、a.b=ftell(fp); code=htonl(m); /返回当前读写指针的位置,即得到文件长度。 p=(char *)&k; while(1) FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_SET(listensocket,&fdread); for(int i=0;itotalsock;i+) FD_SET(tempsocket,&fdread); FD_SET(tempsocket,&fdwrite); if(SOCKET_ERROR=(sockok=select(NULL,&fdread,&fdwrite,NULL,NULL) printf(err
6、or on select); 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)
7、printf(error on ioctl); 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(error on recv long); return -1; /if(a.b=0) a.b=2; /else /if(recv(newconnection,buf
8、,sizeof(buf),0)=SOCKET_ERROR) / / printf(error on recv ack); / return -1; / /if(strcmp(buf,exit)=0) / / printf(conplete copy); / 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(error on send); return -1; /memset(a.
9、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=k) printf(copy complete); /send(clientSock,buf,n,0); /把文件内容发送给客户方 fclose(f
10、p); break; /if(strcmp(buf0,exit)=0) / break; WSACleanup( ); gets(buf); printf(exit to exit); if(strcmp(exit,buf) return 0;客户端:#pragma comment (lib,ws2_32.lib)#include #include #includeint main() WSADATA wsaData; SOCKADDR_IN cliaddr; SOCKET scon; char buf050; int n=0,m=0; int j=0,k=0,l=0,q=0; union c
11、har a4; int b; a;/存储文件长度,用字符型指针做接收发送的参数,然后用型运算 int sockok; struct fd_set fdwrite,fdread; int cliaddrlen=sizeof(cliaddr); ULONG sockmode=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)/创
12、建套接字 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); /地址 /* if(SOCKET_ERROR=bind(scon,(SOCKADDR*)&cliaddr,cliaddrlen)/显示绑定套接字与地址信息 printf(error3); return -1; */ printf(connect.n); if(SOCKET_ERROR=conne
13、ct(scon,(SOCKADDR *)&cliaddr,cliaddrlen)/连接远端主机 printf(error4 connect failed); return -1; printf(donen); if(SOCKET_ERROR=ioctlsocket(scon,FIONBIO,&sockmode) /修改套接字为非阻塞 printf(error on ioctl); return -1; if(!(fp=fopen(input.jpg,wb+) /打开已有或创建一个二进制文件 printf(error on fopen); for(int i=0;i+)/死循环 FD_ZERO(
14、&fdwrite); FD_ZERO(&fdread); FD_SET(scon,&fdwrite); FD_SET(scon,&fdread); if(sockok=select(0,&fdread,&fdwrite,NULL,NULL)0)/选择出可读写的套接字用形参接收 printf(error on select); return -1; if(m=-2) break;/完成传送文件标志位 /memset(buf,0,sizeof(buf); memset(buf0,0,sizeof(buf0);/清缓存 /gets(buf); /if(strcmp(exit,buf)=0) / /
15、printf(exit.); / return 0; / /*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)/文件长度接收标志为接收了 if(q=recv(scon,a.a,sizeof(a),0)0)/接收主文件 /continue; m= fwrite(buf0,n,1,fp); mem
16、set(buf0,0,sizeof(buf0); if(m=1)&(a.b-=n)=0)/文件写入本地且文件接收长度达到文件长度结束 couta.bendl; coutnendl; coutmendl; send(scon,exit,strlen(exit),0); printf(complete copy); m=-2; /printf(complate copy); break; if(FD_ISSET(scon,&fdwrite)&sockok-) if(l=1) if(send(scon,(char*)&l,sizeof(&l),0)0)/确认收到文件长度 printf(error on send); return -1; l=2; /*else printf(buf0); printf(n); */ if(fp) fclose(fp); closesocket(scon); WSACleanup( );
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1