- #include <WinSock2.h>
- #include <WS2tcpip.h>
- #include <windows.h>
- #include <stdio.h>
- #pragma comment(lib,"ws2_32.lib")
- #define DEFAULT_PORT "100000"
- #define BUF_SIZE 4096
- #define MAX_REQUEST 1024
- DWORD WINAPI CommunicationThread(LPVOID lpParameter){
- DWORD dwTid = GetCurrentThreadId();
- SOCKET socket = (SOCKET)lpParameter;
- LPSTR szRequest = (LPSTR)HeapAlloc(GetProcessHeap(),0,MAX_REQUEST);
- int iResult;
- int bytesSent;
- iResult = recv(socket,szRequest,MAX_REQUEST,0);
- if(iResult == 0){
- printf("Connection closed.\n");
- HeapFree(GetProcessHeap(),0,szRequest);
- closesocket(socket);
- return 1;
- }else if(iResult == SOCKET_ERROR){
- printf("Connection error.(%d)\n",WSAGetLastError());
- HeapFree(GetProcessHeap(),0,szRequest);
- closesocket(socket);
- return 1;
- }else if(iResult > 0){
- printf("\tCommunicationThread(%d)\tBytes Received:(%d)\n",dwTid,iResult);
- printf("\tCommunicationThread(%d)\tReceived string:(%s)\n",dwTid,szRequest);
- if(lstrcmpi(szRequest,"download file") == 0){
- HANDLE hFile;
- LPVOID lpReadBuf;
- DWORD dwBytesRead;
- DWORD dwFileSize;
- DWORD dwSendFile = 0;
- hFile = CreateFile("download.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile == INVALID_HANDLE_VALUE){
- printf("\tCommunicationThread\tCould not open file (error %d)\n",GetLastError());
- send(socket,"error",6,0);
- closesocket(socket);
- return 1;
- }
- lpReadBuf = HeapAlloc(GetProcessHeap(),0,BUF_SIZE);
- dwFileSize = GetFileSize(hFile,NULL);
- while(1){
- if(!ReadFile(hFile,lpReadBuf,BUF_SIZE,&dwBytesRead,NULL)){
- printf("\tCommuncationThread could not read from file.(%d)\n",GetLastError());
- closesocket(socket);
- CloseHandle(hFile);
- return 1;
- }
- bytesSent = send(socket,(LPSTR)lpReadBuf,dwBytesRead,0);
- if(bytesSent == SOCKET_ERROR){
- printf("\tCommuncationThread\tsend error %d\n",WSAGetLastError());
- closesocket(socket);
- CloseHandle(hFile);
- return 1;
- }
- printf("\tCommuncationThread(%d)\tsend %d bytes.\n",dwTid,bytesSent);
- dwSendFile +=dwBytesRead;
- if(dwSendFile == dwFileSize){
- printf("CommuncationThread downdload file ok.\n");
- break;
- }
- }
- HeapFree(GetProcessHeap(),0,lpReadBuf);
- CloseHandle(hFile);
- closesocket(socket);
- }else if (lstrcmpi(szRequest,"get information") == 0)
- {
- bytesSent = send(socket,"This is information",lstrlen("This is information")+1,0);
- if(bytesSent == SOCKET_ERROR){
- printf("\tCommuncationThread get infomation error.(%d)\n",WSAGetLastError());
- closesocket(socket);
- return 1;
- }
- printf("\tCommuncationThread(%d)\tsend %d bytes.\n",dwTid,bytesSent);
- }else{
- printf("invalid request\n");
- }
- HeapFree(GetProcessHeap(),0,szRequest);
- closesocket(socket);
- return 0;
- }
- }
- int main(void){
- WSADATA wsaData;
- SOCKET ListenSocket = INVALID_SOCKET;
- SOCKET ClientSocket = INVALID_SOCKET;
- ADDRINFO * result = NULL;
- ADDRINFO hints;
- int iResult;
- iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
- if(iResult !=0){
- printf("WSAStartup failed.(%d)\n",iResult);
- return 1;
- }
- ZeroMemory(&hints,sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- iResult = getaddrinfo(NULL,DEFAULT_PORT,&hints,&result);
- if(iResult != 0){
- printf("getaddrinfo failed.(%d)\n",iResult);
- WSACleanup();
- return 1;
- }
- ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol);
- if(ListenSocket == INVALID_SOCKET){
- printf("Create socket failed.(%d)\n",WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- iResult = bind(ListenSocket,result->ai_addr,(int)result->ai_addrlen);
- if(iResult == SOCKET_ERROR){
- printf("bind failed.(%d)\n",WSAGetLastError());
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- printf("bind.\n");
- freeaddrinfo(result);
- iResult = listen(ListenSocket,SOMAXCONN);
- printf("start listen...\n");
- if(iResult == SOCKET_ERROR){
- printf("listen error.(%d)\n",WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- while(1){
- printf("ready to accept\n");
- ClientSocket = accept(ListenSocket,NULL,NULL);
- printf("accept a connection.\n");
- if(ClientSocket == INVALID_SOCKET){
- printf("accept failed.(%d)\n",WSAGetLastError());
- closesocket(ListenSocket);
- break;
- }
- if(!CreateThread(NULL,0,CommunicationThread,(LPVOID)ClientSocket,0,NULL)){
- printf("Create Thread error (%d).\n",GetLastError());
- break;
- }
- }
- WSACleanup();
- return 0;
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/683842,如需转载请自行联系原作者