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