MSDN上的客户端代码(Winsock Client Source Code)

  1 #undef UNICODE
  2 
  3 #define WIN32_LEAN_AND_MEAN
  4 
  5 #include <windows.h>
  6 #include <winsock2.h>
  7 #include <ws2tcpip.h>
  8 #include <stdlib.h>
  9 #include <stdio.h>
 10 
 11 // Need to link with Ws2_32.lib
 12 #pragma comment (lib, "Ws2_32.lib")
 13 // #pragma comment (lib, "Mswsock.lib")
 14 
 15 #define DEFAULT_BUFLEN 512
 16 #define DEFAULT_PORT "27015"
 17 
 18 int __cdecl main(void) 
 19 {
 20     WSADATA wsaData;
 21     int iResult;
 22 
 23     SOCKET ListenSocket = INVALID_SOCKET;
 24     SOCKET ClientSocket = INVALID_SOCKET;
 25 
 26     struct addrinfo *result = NULL;
 27     struct addrinfo hints;
 28 
 29     int iSendResult;
 30     char recvbuf[DEFAULT_BUFLEN];
 31     int recvbuflen = DEFAULT_BUFLEN;
 32     
 33     // Initialize Winsock
 34     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 35     if (iResult != 0) {
 36         printf("WSAStartup failed with error: %d\n", iResult);
 37         return 1;
 38     }
 39 
 40     ZeroMemory(&hints, sizeof(hints));
 41     hints.ai_family = AF_INET;
 42     hints.ai_socktype = SOCK_STREAM;
 43     hints.ai_protocol = IPPROTO_TCP;
 44     hints.ai_flags = AI_PASSIVE;
 45 
 46     // Resolve the server address and port
 47     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
 48     if ( iResult != 0 ) {
 49         printf("getaddrinfo failed with error: %d\n", iResult);
 50         WSACleanup();
 51         return 1;
 52     }
 53 
 54     // Create a SOCKET for connecting to server
 55     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
 56     if (ListenSocket == INVALID_SOCKET) {
 57         printf("socket failed with error: %ld\n", WSAGetLastError());
 58         freeaddrinfo(result);
 59         WSACleanup();
 60         return 1;
 61     }
 62 
 63     // Setup the TCP listening socket
 64     iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
 65     if (iResult == SOCKET_ERROR) {
 66         printf("bind failed with error: %d\n", WSAGetLastError());
 67         freeaddrinfo(result);
 68         closesocket(ListenSocket);
 69         WSACleanup();
 70         return 1;
 71     }
 72 
 73     freeaddrinfo(result);
 74 
 75     iResult = listen(ListenSocket, SOMAXCONN);
 76     if (iResult == SOCKET_ERROR) {
 77         printf("listen failed with error: %d\n", WSAGetLastError());
 78         closesocket(ListenSocket);
 79         WSACleanup();
 80         return 1;
 81     }
 82 
 83     // Accept a client socket
 84     ClientSocket = accept(ListenSocket, NULL, NULL);
 85     if (ClientSocket == INVALID_SOCKET) {
 86         printf("accept failed with error: %d\n", WSAGetLastError());
 87         closesocket(ListenSocket);
 88         WSACleanup();
 89         return 1;
 90     }
 91 
 92     // No longer need server socket
 93     closesocket(ListenSocket);
 94 
 95     // Receive until the peer shuts down the connection
 96     do {
 97 
 98         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
 99         if (iResult > 0) {
100             printf("Bytes received: %d\n", iResult);
101 
102         // Echo the buffer back to the sender
103             iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
104             if (iSendResult == SOCKET_ERROR) {
105                 printf("send failed with error: %d\n", WSAGetLastError());
106                 closesocket(ClientSocket);
107                 WSACleanup();
108                 return 1;
109             }
110             printf("Bytes sent: %d\n", iSendResult);
111         }
112         else if (iResult == 0)
113             printf("Connection closing...\n");
114         else  {
115             printf("recv failed with error: %d\n", WSAGetLastError());
116             closesocket(ClientSocket);
117             WSACleanup();
118             return 1;
119         }
120 
121     } while (iResult > 0);
122 
123     // shutdown the connection since we're done
124     iResult = shutdown(ClientSocket, SD_SEND);
125     if (iResult == SOCKET_ERROR) {
126         printf("shutdown failed with error: %d\n", WSAGetLastError());
127         closesocket(ClientSocket);
128         WSACleanup();
129         return 1;
130     }
131 
132     // cleanup
133     closesocket(ClientSocket);
134     WSACleanup();
135 
136     return 0;
137 }
原文地址:https://www.cnblogs.com/wwping/p/2454979.html