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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <tchar.h> #include <WinSock2.h> using namespace std; #pragma comment(lib,"ws2_32.LIB") #define SERVERPORT 9000 #define BUFSIZE 512 void err_quit(const TCHAR* msg) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); MessageBox(NULL, (LPCTSTR)lpMsgBuf, msg, MB_ICONERROR); LocalFree(lpMsgBuf); exit(1); } void err_display(const TCHAR* msg) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); printf("[%s] %s", msg, (char*)lpMsgBuf); LocalFree(lpMsgBuf); exit(1); } class SocketStart { WSADATA wsaData; BYTE nMajor, nMinor; WORD wVersionRequested; public: SocketStart():nMajor(2),nMinor(2) { wVersionRequested = MAKEWORD(nMinor, nMajor); }; void Start() { if (WSAStartup(wVersionRequested, &wsaData) == SOCKET_ERROR) cout << "소켓 초기화 실패" << endl; Check(); return; } void Check() { if (LOBYTE(wsaData.wVersion) != nMinor || HIBYTE(wsaData.wVersion) != nMajor) { cout << "소켓 버전 불일치" << endl; WSACleanup(); return; } } }; class ListenSock { public: SOCKET lstnsock; private: struct sockaddr_in addr; int addrlen; int share_value; public: ListenSock() {} void Initialize() { share_value = 0; lstnsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (lstnsock == INVALID_SOCKET) err_quit("listen error"); addr.sin_family = AF_INET; addr.sin_port = htons(SERVERPORT); addr.sin_addr.s_addr = htonl(INADDR_ANY); Bind_Listen(); } void Bind_Listen() { if (bind(lstnsock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) err_quit("bind error"); if (listen(lstnsock, SOMAXCONN) == SOCKET_ERROR) err_quit("listen error"); } }; int share = 0; CRITICAL_SECTION cs; DWORD WINAPI ProcessClient(LPVOID arg); class MultiServer { ListenSock Lstnsock; HANDLE hThread; struct sockaddr_in cliaddr; int addrlen; public: MultiServer(){ } ~MultiServer() { DeleteCriticalSection(&cs); } void Initialize() { InitializeCriticalSection(&cs); addrlen = sizeof(cliaddr); Lstnsock.Initialize(); } void Run() { while (1) { SOCKET commsock = accept(Lstnsock.lstnsock, (struct sockaddr*)&cliaddr, &addrlen); if (commsock == INVALID_SOCKET) { err_display("accept error"); break; } printf("\n[TCP Server] Client Connect : IP Address = %s, Port = %d\n ", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); hThread = CreateThread(NULL, 0, ProcessClient, (LPVOID)commsock, 0, NULL); if (hThread == NULL) { closesocket(commsock); } else { CloseHandle(hThread); } } closesocket(Lstnsock.lstnsock); } }; DWORD WINAPI ProcessClient(LPVOID arg) { SOCKET serv = (SOCKET)arg; int retval; int addrlen; char buf[BUFSIZE + 1]; addrlen = sizeof(serv); while (1) { memset(buf, '\0', sizeof(char)*(BUFSIZE + 1)); retval = recv(serv, buf, BUFSIZE, 0); if (retval == SOCKET_ERROR) { err_display(_T("recv()")); break; } else if (strcmp(buf,"quit") == 0) break; cout << "retval : " << retval << endl; buf[retval] = '\0'; printf("TCP Client : %s\n", buf);//여기까지는 잘받음 EnterCriticalSection(&cs); char sendBuf[100]; memset(sendBuf, '\0', sizeof(char) * 100); sprintf(sendBuf, "%d", share); // _itoa(share, sendBuf, 100); cout << "SendBuf : " << sendBuf << endl; int sendSize = send(serv, sendBuf, 100, 0); cout << "SendSize : " << sendSize << endl; char recvBuf[100] = { '\0' }; retval = recv(serv, recvBuf, 100, 0); recvBuf[retval] = '\0'; cout << "how many size ? : " << retval <<" recvBuf : " << recvBuf << endl; share = atoi(recvBuf); printf("share : %d", share); LeaveCriticalSection(&cs); } closesocket(serv); printf("[TCP 서버] 클라이언트 종료: IP 주소 = %s\n", buf); return 0; } int main(int argc, char* argv[]) { SocketStart SockStart; MultiServer serv; SockStart.Start(); serv.Initialize(); serv.Run(); return 0; } | cs |
'서버프로그래밍' 카테고리의 다른 글
내 머릿속에 남아있는 HANDlE의 의의 (0) | 2018.05.27 |
---|---|
가변길이 패킷 (0) | 2018.05.27 |
브로드 캐스트 주소 구하기 (0) | 2018.05.27 |
서버 주소 변환 함수 (0) | 2018.05.27 |
간단한 Multi클라이언트 (0) | 2018.05.27 |