1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #include <winsock2.h> #include <iostream> #include <assert.h> #include <vector>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
const u_short kPort = 10001; const std::string kHelloServer = "hello, I'm server.";
HANDLE g_event_array[WSA_MAXIMUM_WAIT_EVENTS]; SOCKET g_socket_array[WSA_MAXIMUM_WAIT_EVENTS]; int g_index = 0;
template<typename T> void RemoveArrayIndex(T arr[], int total, int index) { if (index > total) return;
for (int i = index; i < (total - 1); i++) { arr[i] = arr[i + 1]; } }
int main() { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData);
do { g_socket_array[g_index] = ::socket(AF_INET, SOCK_STREAM, 0); if (g_socket_array[0] == INVALID_SOCKET) { std::cout << "create socket failed, GLE: " << WSAGetLastError() << std::endl; break; }
struct sockaddr_in addr = { 0 }; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(kPort); if (bind(g_socket_array[g_index], reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR) { std::cout << "bind failed, GLE: " << WSAGetLastError() << std::endl; break; }
g_event_array[g_index] = WSACreateEvent();
WSAEventSelect(g_socket_array[g_index], g_event_array[g_index], FD_ACCEPT | FD_READ | FD_WRITE | FD_CLOSE);
if (listen(g_socket_array[g_index], 5) == SOCKET_ERROR) { std::cout << "listen failed, GLE: " << WSAGetLastError() << std::endl; break; } std::cout << "listen on port: " << kPort << std::endl;
while (true) { DWORD ret = WSAWaitForMultipleEvents(g_index + 1, g_event_array, FALSE, WSA_INFINITE, FALSE); if(ret == WSA_WAIT_FAILED || ret == WSA_WAIT_TIMEOUT) continue;
for (int i = 0; i <= g_index; i++) { ret = WSAWaitForMultipleEvents(1, &g_event_array[i], FALSE, 0, FALSE); if (ret == WSA_WAIT_FAILED || ret == WSA_WAIT_TIMEOUT) { continue; }
WSANETWORKEVENTS network_events; WSAEnumNetworkEvents(g_socket_array[i], g_event_array[i], &network_events);
if (network_events.lNetworkEvents & FD_ACCEPT) { if (network_events.iErrorCode[FD_ACCEPT_BIT] != 0) { std::cout << "FD_ACCEPT failed with code: " << network_events.iErrorCode[FD_ACCEPT_BIT] << "\n"; break; }
SOCKET s = accept(g_socket_array[i], NULL, NULL); if (s == SOCKET_ERROR) { std::cout << "accept failed, GLE: " << WSAGetLastError() << std::endl; break; }
if (g_index >= WSA_MAXIMUM_WAIT_EVENTS - 1) { std::cout << "too many connection\n"; closesocket(s); break; } g_index++;
g_event_array[g_index] = WSACreateEvent(); g_socket_array[g_index] = s;
std::cout << "new connection\n";
int err = send(s, (const char*)kHelloServer.c_str(), kHelloServer.length(), 0); if (err == SOCKET_ERROR) { std::cout << "send failed, GLE: " << WSAGetLastError() << std::endl; break; } }
if (network_events.lNetworkEvents & FD_READ) { if (network_events.iErrorCode[FD_READ_BIT] != 0) { std::cout << "FD_READ failed with code: " << network_events.iErrorCode[FD_READ_BIT] << "\n"; break; }
char buf[100] = { 0 }; int err = recv(g_socket_array[i], buf, 100, 0); if (err > 0) { std::cout << "recv: " << buf << std::endl; } else if (err == 0) { std::cout << "connection closed." << std::endl;
closesocket(g_socket_array[i]); WSACloseEvent(g_event_array[i]);
RemoveArrayIndex(g_socket_array, g_index, i); RemoveArrayIndex(g_event_array, g_index, i);
g_index--; } else { std::cout << "recv failed, GLE: " << WSAGetLastError() << std::endl;
closesocket(g_socket_array[i]); WSACloseEvent(g_event_array[i]);
RemoveArrayIndex(g_socket_array, g_index, i); RemoveArrayIndex(g_event_array, g_index, i);
g_index--; } }
if (network_events.lNetworkEvents & FD_WRITE) { if (network_events.iErrorCode[FD_WRITE_BIT] != 0) { std::cout << "FD_WRITE failed with code: " << network_events.iErrorCode[FD_WRITE_BIT] << "\n"; break; }
}
if (network_events.lNetworkEvents & FD_CLOSE) { if (network_events.iErrorCode[FD_CLOSE_BIT] != 0) { std::cout << "FD_CLOSE failed with code: " << network_events.iErrorCode[FD_CLOSE_BIT] << "\n"; break; }
closesocket(g_socket_array[i]); WSACloseEvent(g_event_array[i]);
RemoveArrayIndex(g_socket_array, g_index, i); RemoveArrayIndex(g_event_array, g_index, i);
g_index--; } } } } while (false);
for (int i = 0; i <= g_index; i++) { closesocket(g_socket_array[i]); WSACloseEvent(g_event_array[i]); }
WSACleanup(); return 0; }
|