博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
win32 tcp文件传输并发服务器
阅读量:5116 次
发布时间:2019-06-13

本文共 3855 字,大约阅读时间需要 12 分钟。

#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")#define PORT 9999#define IPADDR "127.0.0.1"#define BACKLOG 20#define FILENAME 200#define LENGTH 200#define BUFFERSIZE 1024struct FILEHEAD //FILE-head struct{ char filename[LENGTH];//file name unsigned int length;//the byte of the file};struct FILEDATA //FILE-data struct{ char filename[LENGTH];//file name char package[BUFFERSIZE];//package data unsigned int length;//the byte of the file unsigned int index;//index of the package};struct sockaddr_in clientaddr; //Definition of the external variable for thread function callvoid getFileInformation(FILEHEAD file){ printf( "file information :\n" ); printf( " Filename: %s\n", file.filename ); //printf( " Ext: %s\n", file.ext ); printf( " the file length is: %ld btye\n", file.length );}void showClientinfo(){ //获取当前系统时间 SYSTEMTIME st; GetLocalTime(&st); char SysDate[30]; //将systime中的时间转变为字符串存入SysDate[30]; sprintf(SysDate, "%4d-%2d-%2d %2d:%2d:%2d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); //Server显示客户端信息 printf("%s Recv from Client [%s:%d] : %s\n", SysDate, inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); //服务器向客户端回显信息}DWORD WINAPI requestThread(LPVOID lparam){ FILEHEAD filehead; FILEDATA filedata; SOCKET newsock=(SOCKET)(LPVOID)lparam; //char buf[BUFFERSIZE]={0}; memset(&filehead,0,sizeof(filehead)); memset(&filedata,0,sizeof(filedata)); showClientinfo(); //printf("等待文件头信息 ...\n"); int length_file_info=recv(newsock,(char *)&filehead,sizeof(filehead),0); if (SOCKET_ERROR==length_file_info) { printf("receive failed!\n"); closesocket(newsock); return -1; } if (length_file_info<=0) { exit(1);//异常退出 } getFileInformation(filehead);//打印文件信息 FILE *fp=NULL; fp=fopen(filehead.filename,"wb+"); if (NULL==fp) { perror("fail to build the file!!!\n"); exit(1); } //printf("要接收的文件名为:"); //printf(filehead.filename);//打印文件名 //printf ("\n catch file now....\n"); int recv_length=0;//接收到字节的长度 //Sleep(100); printf("开始接收...\n"); filedata.index=0; while (1) { recv_length=recv(newsock,(char *)&filedata,sizeof(filedata),0); if (recv_length == SOCKET_ERROR) { printf("recv failed !\n"); closesocket(newsock); //WSACleanup(); return -1; } fwrite(filedata.package,1,BUFFERSIZE,fp); if (0==recv_length) { break; } //printf("第%d块接收成功!\n",filedata.index); } printf("\n接收完成...\n\n"); fflush(fp); fclose(fp); fp=NULL; return 0;}int main(int argc,char *argv[]){ //初始化winsock版本信息,加载动态链接库(dll) WSADATA wsData; if (WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("WSAStartup failed !!!\n"); return -1; } //创建套接字 SOCKET socklisten; if((socklisten=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET) { printf("socket failed!!!\n"); WSACleanup(); return -1; } //设置服务器地址 struct sockaddr_in servaddr; memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(PORT); servaddr.sin_addr.S_un.S_addr=inet_addr(IPADDR); //绑定socket地址结构到监听套接字 if (bind(socklisten,(sockaddr *)&servaddr,sizeof(servaddr))!=0) { printf("binding failed!!!\n"); closesocket(socklisten); WSACleanup(); } //在server上运行监听 if (listen(socklisten,20)!=0) { printf("listen failed !!!\n"); closesocket(socklisten); WSACleanup(); return -1; } //接收客户端的连接请求 printf("TCP server is start!!!\n"); //clientaddrlength要有初值, int client_addr_length = sizeof(clientaddr); memset(&clientaddr,0,client_addr_length); SOCKET connect; //循环等待 while (1) { if ((connect=accept(socklisten,(sockaddr *)&clientaddr,&client_addr_length))==INVALID_SOCKET) { printf("accept failed!!!\n"); closesocket(connect); WSACleanup(); return -1; } //创建新线程 DWORD ThreadID; CreateThread(NULL,0,requestThread,(LPVOID)connect,0,&ThreadID); } }

 

转载于:https://www.cnblogs.com/wuyida/archive/2013/01/28/6301481.html

你可能感兴趣的文章
【模板】最小生成树
查看>>
设计模式之结构型模式
查看>>
poj2569
查看>>
使用pygal_maps_world.i18n中数据画各大洲地图
查看>>
sql server必知多种日期函数时间格式转换
查看>>
jQuery EasyUI 的下拉选择combobox后台动态赋值
查看>>
timeline时间轴进度“群英荟萃”
查看>>
python if else elif statement
查看>>
网络编程
查看>>
文本隐藏(图片代替文字)
查看>>
java面试题
查看>>
提高码力专题(未完待续)
查看>>
pair的例子
查看>>
前端框架性能对比
查看>>
@property中 retain 详解
查看>>
uva 387 A Puzzling Problem (回溯)
查看>>
12.2日常
查看>>
Delphi 取整函数round、trunc、ceil和floor
查看>>
C/C++二维数组的用法
查看>>
排序 冒泡排序法
查看>>