Windows网络编程之winsock实现文件传输示例
本文实例展示了Windows网络编程中winsock实现文件传输的方法,对于进行Windows下的winsock网络编程有一定的借鉴参考价值。
该程序代码主要是基于TCP流协议的winsock网络文件传输示例,采用Windows下的C语言编写。可以实现传递任何格式文件的文件传输功能。
具体实现代码如下:
Server端代码如下:
/************************************************************************* >FileName:Win_Server.c >Author:SongLee ************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<WinSock2.h> #definePORT8087 #defineSERVER_IP"127.0.0.1" #defineBUFFER_SIZE1024 #defineFILE_NAME_MAX_SIZE512 #pragmacomment(lib,"WS2_32") intmain() { //声明并初始化一个服务端(本地)的地址结构 sockaddr_inserver_addr; server_addr.sin_family=AF_INET; server_addr.sin_addr.S_un.S_addr=INADDR_ANY; server_addr.sin_port=htons(PORT); //初始化socketdll WSADATAwsaData; WORDsocketVersion=MAKEWORD(2,0); if(WSAStartup(socketVersion,&wsaData)!=0) { printf("Initsocketdllerror!"); exit(1); } //创建socket SOCKETm_Socket=socket(AF_INET,SOCK_STREAM,0); if(SOCKET_ERROR==m_Socket) { printf("CreateSocketError!"); exit(1); } //绑定socket和服务端(本地)地址 if(SOCKET_ERROR==bind(m_Socket,(LPSOCKADDR)&server_addr,sizeof(server_addr))) { printf("ServerBindFailed:%d",WSAGetLastError()); exit(1); } //监听 if(SOCKET_ERROR==listen(m_Socket,10)) { printf("ServerListenFailed:%d",WSAGetLastError()); exit(1); } while(1) { printf("ListeningToClient...\n"); sockaddr_inclient_addr; intclient_addr_len=sizeof(client_addr); SOCKETm_New_Socket=accept(m_Socket,(sockaddr*)&client_addr,&client_addr_len); if(SOCKET_ERROR==m_New_Socket) { printf("ServerAcceptFailed:%d",WSAGetLastError()); break; } charbuffer[BUFFER_SIZE]; memset(buffer,0,BUFFER_SIZE); if(recv(m_New_Socket,buffer,BUFFER_SIZE,0)<0) { printf("ServerReceiveDataFailed!"); break; } charfile_name[FILE_NAME_MAX_SIZE+1]; memset(file_name,0,FILE_NAME_MAX_SIZE+1); strncpy(file_name,buffer,strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer)); printf("%s\n",file_name); FILE*fp=fopen(file_name,"rb");//windows下是"rb",表示打开一个只读的二进制文件 if(NULL==fp) { printf("File:%sNotFound\n",file_name); } else { memset(buffer,0,BUFFER_SIZE); intlength=0; while((length=fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0) { if(send(m_New_Socket,buffer,length,0)<0) { printf("SendFile:%sFailed\n",file_name); break; } memset(buffer,0,BUFFER_SIZE); } fclose(fp); printf("File:%sTransferSuccessful!\n",file_name); } closesocket(m_New_Socket); } closesocket(m_Socket); //释放winsock库 WSACleanup(); return0; }
Client端代码如下:
/************************************************************************* >FileName:Win_Client.c >Author:SongLee ************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<WinSock2.h> #definePORT8087 #defineSERVER_IP"127.0.0.1" #defineBUFFER_SIZE1024 #defineFILE_NAME_MAX_SIZE512 #pragmacomment(lib,"WS2_32") intmain() { //初始化socketdll WSADATAwsaData; WORDsocketVersion=MAKEWORD(2,0); if(WSAStartup(socketVersion,&wsaData)!=0) { printf("Initsocketdllerror!"); exit(1); } //创建socket SOCKETc_Socket=socket(AF_INET,SOCK_STREAM,0); if(SOCKET_ERROR==c_Socket) { printf("CreateSocketError!"); system("pause"); exit(1); } //指定服务端的地址 sockaddr_inserver_addr; server_addr.sin_family=AF_INET; server_addr.sin_addr.S_un.S_addr=inet_addr(SERVER_IP); server_addr.sin_port=htons(PORT); if(SOCKET_ERROR==connect(c_Socket,(LPSOCKADDR)&server_addr,sizeof(server_addr))) { printf("CanNotConnectToServerIP!\n"); system("pause"); exit(1); } //输入文件名 charfile_name[FILE_NAME_MAX_SIZE+1]; memset(file_name,0,FILE_NAME_MAX_SIZE+1); printf("PleaseInputFileNameOnServer:"); scanf("%s",&file_name); charbuffer[BUFFER_SIZE]; memset(buffer,0,BUFFER_SIZE); strncpy(buffer,file_name,strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name)); //向服务器发送文件名 if(send(c_Socket,buffer,BUFFER_SIZE,0)<0) { printf("SendFileNameFailed\n"); system("pause"); exit(1); } //打开文件,准备写入 FILE*fp=fopen(file_name,"wb");//windows下是"wb",表示打开一个只写的二进制文件 if(NULL==fp) { printf("File:%sCanNotOpenToWrite\n",file_name); system("pause"); exit(1); } else { memset(buffer,0,BUFFER_SIZE); intlength=0; while((length=recv(c_Socket,buffer,BUFFER_SIZE,0))>0) { if(fwrite(buffer,sizeof(char),length,fp)<length) { printf("File:%sWriteFailed\n",file_name); break; } memset(buffer,0,BUFFER_SIZE); } printf("ReceiveFile:%sFromServerSuccessful!\n",file_name); } fclose(fp); closesocket(c_Socket); //释放winsock库 WSACleanup(); system("pause"); return0; }
程序中备有较为详尽的注释,相信大家不难理解。感兴趣的朋友可以根据自身需要对程序加以灵活改善,使其功能更加强大。