多线程Socket服务端处理程序

 


  1. #include <WinSock2.h>  
  2. #include <WS2tcpip.h>  
  3. #include <windows.h>  
  4. #include <stdio.h>  
  5.  
  6. #pragma comment(lib,"ws2_32.lib")  
  7. #define DEFAULT_PORT "100000"  
  8. #define BUF_SIZE 4096  
  9. #define MAX_REQUEST 1024  
  10.  
  11. DWORD WINAPI CommunicationThread(LPVOID lpParameter){  
  12.     DWORD dwTid = GetCurrentThreadId();  
  13.     SOCKET socket = (SOCKET)lpParameter;  
  14.     LPSTR szRequest = (LPSTR)HeapAlloc(GetProcessHeap(),0,MAX_REQUEST);  
  15.     int iResult;  
  16.     int bytesSent;  
  17.     iResult = recv(socket,szRequest,MAX_REQUEST,0);  
  18.     if(iResult == 0){  
  19.         printf("Connection closed.\n");  
  20.         HeapFree(GetProcessHeap(),0,szRequest);  
  21.         closesocket(socket);  
  22.         return 1;  
  23.     }else if(iResult == SOCKET_ERROR){  
  24.         printf("Connection error.(%d)\n",WSAGetLastError());  
  25.         HeapFree(GetProcessHeap(),0,szRequest);  
  26.         closesocket(socket);  
  27.         return 1;  
  28.     }else if(iResult > 0){  
  29.         printf("\tCommunicationThread(%d)\tBytes Received:(%d)\n",dwTid,iResult);  
  30.         printf("\tCommunicationThread(%d)\tReceived string:(%s)\n",dwTid,szRequest);  
  31.  
  32.         if(lstrcmpi(szRequest,"download file") == 0){  
  33.             HANDLE hFile;  
  34.             LPVOID lpReadBuf;  
  35.             DWORD dwBytesRead;  
  36.             DWORD dwFileSize;  
  37.             DWORD dwSendFile = 0;  
  38.             hFile = CreateFile("download.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);  
  39.             if(hFile == INVALID_HANDLE_VALUE){  
  40.                 printf("\tCommunicationThread\tCould not open file (error %d)\n",GetLastError());  
  41.                 send(socket,"error",6,0);  
  42.                 closesocket(socket);  
  43.                 return 1;  
  44.             }  
  45.             lpReadBuf = HeapAlloc(GetProcessHeap(),0,BUF_SIZE);  
  46.             dwFileSize = GetFileSize(hFile,NULL);  
  47.             while(1){  
  48.                 if(!ReadFile(hFile,lpReadBuf,BUF_SIZE,&dwBytesRead,NULL)){  
  49.                     printf("\tCommuncationThread could not read from file.(%d)\n",GetLastError());  
  50.                     closesocket(socket);  
  51.                     CloseHandle(hFile);  
  52.                     return 1;  
  53.                 }  
  54.                 bytesSent = send(socket,(LPSTR)lpReadBuf,dwBytesRead,0);  
  55.                 if(bytesSent == SOCKET_ERROR){  
  56.                     printf("\tCommuncationThread\tsend error %d\n",WSAGetLastError());  
  57.                     closesocket(socket);  
  58.                     CloseHandle(hFile);  
  59.                     return 1;  
  60.                 }  
  61.                 printf("\tCommuncationThread(%d)\tsend %d bytes.\n",dwTid,bytesSent);  
  62.                 dwSendFile +=dwBytesRead;     
  63.                 if(dwSendFile == dwFileSize){  
  64.                     printf("CommuncationThread downdload file ok.\n");  
  65.                     break;  
  66.                 }  
  67.             }  
  68.             HeapFree(GetProcessHeap(),0,lpReadBuf);  
  69.             CloseHandle(hFile);  
  70.             closesocket(socket);  
  71.         }else if (lstrcmpi(szRequest,"get information") == 0)  
  72.         {  
  73.             bytesSent = send(socket,"This is information",lstrlen("This is information")+1,0);  
  74.             if(bytesSent == SOCKET_ERROR){  
  75.                 printf("\tCommuncationThread get infomation error.(%d)\n",WSAGetLastError());  
  76.                 closesocket(socket);  
  77.                 return 1;  
  78.             }  
  79.             printf("\tCommuncationThread(%d)\tsend %d bytes.\n",dwTid,bytesSent);  
  80.         }else{  
  81.             printf("invalid request\n");  
  82.         }  
  83.         HeapFree(GetProcessHeap(),0,szRequest);  
  84.         closesocket(socket);  
  85.         return 0;  
  86.     }  
  87. }  
  88.  
  89. int main(void){  
  90.     WSADATA wsaData;  
  91.     SOCKET ListenSocket = INVALID_SOCKET;  
  92.     SOCKET ClientSocket = INVALID_SOCKET;  
  93.     ADDRINFO * result = NULL;  
  94.     ADDRINFO hints;  
  95.     int iResult;  
  96.  
  97.     iResult = WSAStartup(MAKEWORD(2,2),&wsaData);  
  98.     if(iResult !=0){  
  99.         printf("WSAStartup failed.(%d)\n",iResult);  
  100.         return 1;  
  101.     }  
  102.     ZeroMemory(&hints,sizeof(hints));  
  103.     hints.ai_family = AF_INET;  
  104.     hints.ai_socktype = SOCK_STREAM;  
  105.     hints.ai_protocol = IPPROTO_TCP;  
  106.     hints.ai_flags = AI_PASSIVE;  
  107.     iResult = getaddrinfo(NULL,DEFAULT_PORT,&hints,&result);  
  108.     if(iResult != 0){  
  109.         printf("getaddrinfo failed.(%d)\n",iResult);  
  110.         WSACleanup();  
  111.         return 1;  
  112.     }  
  113.     ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol);  
  114.     if(ListenSocket == INVALID_SOCKET){  
  115.         printf("Create socket failed.(%d)\n",WSAGetLastError());  
  116.         freeaddrinfo(result);  
  117.         WSACleanup();  
  118.         return 1;  
  119.     }  
  120.     iResult = bind(ListenSocket,result->ai_addr,(int)result->ai_addrlen);  
  121.     if(iResult == SOCKET_ERROR){  
  122.         printf("bind failed.(%d)\n",WSAGetLastError());  
  123.         freeaddrinfo(result);  
  124.         closesocket(ListenSocket);  
  125.         WSACleanup();  
  126.         return 1;  
  127.     }  
  128.     printf("bind.\n");  
  129.     freeaddrinfo(result);  
  130.     iResult = listen(ListenSocket,SOMAXCONN);  
  131.     printf("start listen...\n");  
  132.     if(iResult == SOCKET_ERROR){  
  133.         printf("listen error.(%d)\n",WSAGetLastError());  
  134.         closesocket(ListenSocket);  
  135.         WSACleanup();  
  136.         return 1;  
  137.     }  
  138.     while(1){  
  139.         printf("ready to accept\n");  
  140.         ClientSocket = accept(ListenSocket,NULL,NULL);  
  141.         printf("accept a connection.\n");  
  142.         if(ClientSocket == INVALID_SOCKET){  
  143.             printf("accept failed.(%d)\n",WSAGetLastError());  
  144.             closesocket(ListenSocket);  
  145.             break;  
  146.         }  
  147.         if(!CreateThread(NULL,0,CommunicationThread,(LPVOID)ClientSocket,0,NULL)){  
  148.             printf("Create Thread error (%d).\n",GetLastError());  
  149.             break;  
  150.         }  
  151.     }  
  152.     WSACleanup();  
  153.     return 0;  

 












本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/683842,如需转载请自行联系原作者

上一篇:[c/c++]指针数组 pk 数组指针


下一篇:spring中配置请求编码