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 | #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <WinSock2.h> #include <tchar.h> #include <string.h> using namespace std; #pragma comment(lib,"ws2_32.LIB") #define COUNT 5 #define SIZEOFTHREAD 3 class MultiClient { char ServerIp[100]; char ClientIp[100]; SOCKET sock; SOCKET Mysock; struct sockaddr_in myaddr; public: MultiClient() { memset(ClientIp, '\0', sizeof(char) * 100); cout << "Server Ip :"; cin >> ServerIp; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); Mysock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); int size = sizeof(myaddr); bool result = getsockname(Mysock, (struct sockaddr*)&myaddr, &size); if (result) { strcpy_s(ClientIp, inet_ntoa(myaddr.sin_addr)); cout << "ClientIp : " << ClientIp << endl; } else { cout << "Get My Ip failed" << endl; return; } } void Run() { try { if (sock == INVALID_SOCKET) throw "Do not make Socket"; struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(9000); addr.sin_addr.s_addr = inet_addr(ServerIp);//문자열 10진수 주소를 바이너리주소로 바꿔줌 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { throw "connect error"; } int count = COUNT; while (count > 0) { send(sock, ClientIp, strlen(ClientIp), 0); char recvbuf[200]; memset(recvbuf, '\0', sizeof(char) * 200); int recvsize = recv(sock, recvbuf, 200, 0); cout << "Recive buf :" << recvbuf << " how many recive ? : "<< recvsize << endl; int temp = atoi(recvbuf); temp += 10; cout << "temp : " << temp << endl; char sendBuf[10]; memset(sendBuf, '\0', sizeof(char) * 10); _itoa(temp, sendBuf, 10); cout << "Send buf : " << sendBuf << endl; send(sock, sendBuf, strlen(sendBuf), 0); cout <<"after buf : "<<sendBuf <<" count : " << count << endl; count--; } char quit[10] = "quit"; send(sock, quit, strlen(quit), 0); } catch (char* msg) { cout << msg << endl; LPVOID lpOSMsg; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpOSMsg, 0, NULL); cout << "[" << (char*)lpOSMsg << "]" << endl; LocalFree(lpOSMsg); } if (sock != INVALID_SOCKET) { //Sleep(1000); cout << "close socket" << endl; closesocket(sock); } } }; DWORD WINAPI ThreadProc(LPVOID pParam) { cout << "Enter Thread" << endl; MultiClient* mc = (MultiClient*)pParam; mc->Run(); return 0; } int main(int argc, char* argv[]) { WSADATA wsaData; BYTE nMajor = 2, nMinor = 2; WORD wVersionRequested = MAKEWORD(nMinor, nMajor); if (WSAStartup(wVersionRequested, &wsaData) == SOCKET_ERROR) { cout << "socket initialize failed" << endl; return -1; } if (LOBYTE(wsaData.wVersion) != nMajor || HIBYTE(wsaData.wVersion) != nMinor) { cout << "socket version not equal" << endl; WSACleanup(); return -1; } while (true) { MultiClient mc[SIZEOFTHREAD]; HANDLE hThread[SIZEOFTHREAD]; DWORD dwThreadId[SIZEOFTHREAD]; for (int i = 0; i < SIZEOFTHREAD; i++) { hThread[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&mc[i], 0, &dwThreadId[i]); } WaitForMultipleObjects(SIZEOFTHREAD, hThread, TRUE, INFINITE); /*for (int i = 0; i < SIZEOFTHREAD; i++) mc[i].Run(); */ cout << "IF exit : exit" << endl; char temp[10]; cin >> temp; if (strcmp(temp, "exit") == 0) break; } WSACleanup(); return 0; } | cs |
'서버프로그래밍' 카테고리의 다른 글
내 머릿속에 남아있는 HANDlE의 의의 (0) | 2018.05.27 |
---|---|
가변길이 패킷 (0) | 2018.05.27 |
브로드 캐스트 주소 구하기 (0) | 2018.05.27 |
서버 주소 변환 함수 (0) | 2018.05.27 |
간단한 서버 (0) | 2018.05.27 |