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 186 187 188
| #define FD_SETSIZE 128 #include <WinSock2.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #pragma comment(lib,"ws2_32.lib") #define _WINSOCK_DEPRECATED_NO_WARNINGS
fd_set allSockets;
BOOL WINAPI fun(DWORD dwCtrType) { switch(dwCtrType){ case CTRL_CLOSE_EVENT: for (u_int i = 0; i < allSockets.fd_count; i++) { closesocket(allSockets.fd_array[i]); } WSACleanup(); }
return TRUE; }
int main(void) { SetConsoleCtrlHandler(fun, TRUE);
WORD wdVersion = MAKEWORD(2, 2); WSADATA wdSockMsg; int nRes = WSAStartup(wdVersion, &wdSockMsg); if (nRes != 0) { switch (nRes) { case WSASYSNOTREADY: printf("系统配置问题,重启电脑,检查ws2_32库是否存在,或者是否在环境配置目录下"); break; case WSAVERNOTSUPPORTED: printf("要使用的版本不支持,请更新网络库"); break; case WSAEINPROGRESS: printf("Windows Sockets实现可能限制同时使用它的应用程序的数量"); break; case WSAEPROCLIM: printf("当前函数运行期间,由于某些原因造成阻塞,会返回这个操作码,其他操作均禁止"); break; case WSAEFAULT: printf("参数写错了"); break; } return 0; } if (HIBYTE(wdSockMsg.wVersion) != 2 || LOBYTE(wdSockMsg.wVersion) != 2) { WSACleanup(); return 0; }
SOCKET socketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == socketServer) { WSACleanup(); return 0; } struct sockaddr_in si; si.sin_family = AF_INET; si.sin_port = htons(12345); si.sin_addr.s_addr = htonl(2130706433); int bres = bind(socketServer, (const struct sockaddr*)&si, sizeof(si)); if (SOCKET_ERROR == bres) { int a = WSAGetLastError(); closesocket(socketServer); WSACleanup(); return 0;
} if (SOCKET_ERROR == listen(socketServer, SOMAXCONN)) { int a = WSAGetLastError(); closesocket(socketServer); WSACleanup(); return 0; } FD_ZERO(&allSockets); FD_SET(socketServer, &allSockets); while (1) { fd_set readSockets = allSockets; fd_set writeSockets = allSockets; fd_set errorSockets = allSockets; struct timeval st; st.tv_sec = 3; st.tv_usec = 0; int nRes = select(0, &readSockets, &writeSockets, &errorSockets, &st); if (nRes == 0) { continue; } else if (nRes > 0) { for (u_int i = 0; i < errorSockets.fd_count; i++) { char str[100] = { 0 }; int len = 99; if (SOCKET_ERROR == getsockopt(errorSockets.fd_array[i], SOL_SOCKET, SO_ERROR, str, &len)) { printf("无法得到错误信息\n"); } printf("%s\n", str); }
for (u_int i = 0; i < writeSockets.fd_count; i++) { if (SOCKET_ERROR == send(writeSockets.fd_array[i], "ok", strlen("ok"), 0)) { int a = WSAGetLastError(); } } for (u_int i = 0; i < readSockets.fd_count; i++) { if (readSockets.fd_array[i] == socketServer) { SOCKET socketClient = accept(socketServer, NULL, NULL); if (socketClient == INVALID_SOCKET) { continue; } FD_SET(socketClient, &allSockets); } else{ char strBuf[1500] = { 0 }; int nRecv = recv(readSockets.fd_array[i], strBuf, 1500, 0); { if (nRecv == 0) { SOCKET socketTemp = readSockets.fd_array[i]; FD_CLR(readSockets.fd_array[i], &allSockets); closesocket(socketTemp); } else if (nRecv > 0) { printf(strBuf); } else { int a = WSAGetLastError(); switch (a) { case 10054: { SOCKET socketTemp = readSockets.fd_array[i]; FD_CLR(readSockets.fd_array[i], &allSockets); closesocket(socketTemp); } } } } } } } else { break; } }
for (u_int i = 0; i < allSockets.fd_count; i++) { closesocket(allSockets.fd_array[i]); } WSACleanup(); system("pause"); return 0;
}
|