일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- modeling
- 윈도우 프로그래밍
- shader
- Mesh Processing
- OpenGL
- 핵심 API로 배우는 윈도우프로그래밍
- 렌더링
- 운영체제
- MFC 윈도우 프로그래밍
- win32
- 윈도우
- 컴퓨터 아키텍쳐
- 윈도우프로그래밍
- MFC
- Win32 API
- 셰이더
- c4d
- 그래픽스기초
- shader programming
- bezier curve
- 셰이더프로그래밍
- 그래픽스
- 컴퓨터 구조
- 베지에 곡선
- 윈도우 구조
- Geometry Modeling
- 오픈지엘
- denoising
- Graphics
- window programming
Archives
- Today
- Total
오다기리 박의 알고리즘 노트
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 8번 풀이 본문
-서버 cpp파일-
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 205 206 207 208 209 210 | //논블로킹 단방향통신에서 서버가 클라이언트로 메시지를 보내는 코드를 추가한다. #include <Windows.h> #include<tchar.h> #include<string.h> #include<stdio.h> #include<math.h> #define WM_ASYNC WM_USER+2 //#pragma comment(linker,"/entry:WinMainCRTStartup /subsystem:console") LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); HINSTANCE hInst; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS WndClass; hInst = hInstance; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = _T("Window Class Name"); RegisterClass(&WndClass); hwnd = CreateWindow( _T("Window Class Name"), _T("Server Window"), WS_OVERLAPPEDWINDOW, 50, 50, 900, 380, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static WSADATA wsadata; static SOCKET s, cs; static SOCKADDR_IN addr = { 0 }, c_addr; static bool MyTurn = TRUE; HPEN blackPen; HBRUSH hBrush; static int red[30] = { 0, }, green[30] = { 255, }, blue[30] = { 0, }; static int server_x = 20, server_y = 300; static int client_x = 820, client_y = 300; static int server_boom_x = 20, server_boom_y = 300; static int client_boom_x = 820, client_boom_y = 300; int size, msgLen; char buffer[100]; int i = 0; switch (iMsg) { case WM_CREATE: WSAStartup(MAKEWORD(2, 2), &wsadata); //1.윈속사용시작하기 s = socket(AF_INET, SOCK_STREAM, 0); //2.소켓 생성-클라이언트가 접속하기를 기다리는 소켓 addr.sin_family = AF_INET; addr.sin_port = 20; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(s, (LPSOCKADDR)&addr, sizeof(addr)); //3.주소와 소켓 연결하기 WSAAsyncSelect(s, hwnd, WM_ASYNC, FD_ACCEPT); //4.소켓s에서 상대방이 접속을 시도하는 이벤트가 발생하면 WM_ASYNC를 hwnd 윈도우에 발생시키위해 설정 if (listen(s, 5) == -1) //5.연결 요구 기다리기 return 0; break; case WM_ASYNC: //WM_ASYNC메시지 발생시 lParam으로 어떤 이벤트가 발생했는지 구분 switch (lParam) { case FD_ACCEPT: //클라이언트가 접속을 시도하면 size = sizeof(c_addr); //클라이언트와 통신하기 위한 소켓cs를 만들어 클라이언트와의 통신을 전담시킨다. cs = accept(s, (LPSOCKADDR)&c_addr, &size); WSAAsyncSelect(cs, hwnd, WM_ASYNC, FD_READ);//클라이언트가 메시지를 보낼 때까지 무한정 기다릴 수 없으므로 FD_READ를 WM_ASYNC에 등록 break; case FD_READ: recv(cs, buffer, 100, 0); if (!_tcscmp(buffer, _T("-1"))) //발사메시지 도착 { SetTimer(hwnd, 2, 15, NULL); MyTurn = TRUE; return 0; } else if (!_tcscmp(buffer, _T("2"))) //오른쪽 이동 메시지 도착 { client_x += 10; InvalidateRgn(hwnd, NULL, TRUE); return 0; } else if (!_tcscmp(buffer, _T("1"))) //왼쪽이동 메시지 도착 { client_x -= 10; InvalidateRgn(hwnd, NULL, TRUE); return 0; } break; default: break; } break; case WM_KEYDOWN: if (wParam == VK_SPACE&&MyTurn) { SetTimer(hwnd, 1, 15, NULL); _stprintf_s(buffer, _T("-1")); //발사 메시지 send(cs, (LPSTR)buffer, 10, 0); MyTurn = FALSE; } else if (wParam == VK_RIGHT) { server_x += 10; server_boom_x = server_x; _stprintf_s(buffer, _T("2")); //오른쪽 이동 메시지 send(cs, (LPSTR)buffer, 10, 0); InvalidateRgn(hwnd, NULL, TRUE); } else if (wParam == VK_LEFT) { server_x -= 10; server_boom_x = server_x; _stprintf_s(buffer, _T("1")); //왼쪽 이동 메시지 send(cs, (LPSTR)buffer, 10, 0); InvalidateRgn(hwnd, NULL, TRUE); } break; case WM_TIMER: switch (wParam) { case 1: server_boom_x += 10; server_boom_y = 0.001f*pow(server_boom_x - 400, 2) + 150; InvalidateRgn(hwnd, NULL, TRUE); if (server_boom_y >= 400) { server_boom_x = server_x; server_boom_y = server_y; KillTimer(hwnd, 1); } break; case 2: client_boom_x -= 10; client_boom_y = 0.001f*pow(client_boom_x - 400, 2) + 150; InvalidateRgn(hwnd, NULL, TRUE); if (client_boom_y >= 400) { client_boom_x = client_x; client_boom_y = client_y; KillTimer(hwnd, 2); } break; } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); hBrush = CreateSolidBrush(RGB(0, 0, 0)); SelectObject(hdc, hBrush); Ellipse(hdc, server_x - 20, server_y - 20, server_x + 20, server_y + 20); hBrush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, hBrush); Ellipse(hdc, client_x - 20, client_y - 20, client_x + 20, client_y + 20); hBrush = CreateSolidBrush(RGB(0, 0, 255)); SelectObject(hdc, hBrush); Ellipse(hdc, server_boom_x - 5, server_boom_y - 5, server_boom_x + 5, server_boom_y + 5); hBrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, hBrush); Ellipse(hdc, client_boom_x - 5, client_boom_y - 5, client_boom_x + 5, client_boom_y + 5); EndPaint(hwnd, &ps); break; case WM_DESTROY: closesocket(s); WSACleanup(); PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); } | cs |
-클라이언트 cpp파일-
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 | //6번소스와 동일 #include <Windows.h> #include<tchar.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #include<iostream> LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); HINSTANCE hInst; #define WM_ASYNC WM_USER+2 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS WndClass; hInst = hInstance; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = _T("Window Class Name"); RegisterClass(&WndClass); hwnd = CreateWindow( _T("Window Class Name"), _T("Client Window"), WS_OVERLAPPEDWINDOW, 50, 50, 900, 380, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static WSADATA wsadata; static SOCKET s; static SOCKADDR_IN addr = { 0 }; static bool MyTurn = FALSE; HPEN blackPen; HBRUSH hBrush; static int red[30] = { 0, }, green[30] = { 255, }, blue[30] = { 0, }; static int server_x = 20, server_y = 300; static int client_x = 820, client_y = 300; static int server_boom_x = 20, server_boom_y = 300; static int client_boom_x = 820, client_boom_y = 300; int msgLen; char buffer[100]; int i = 0; switch (iMsg) { case WM_CREATE: WSAStartup(MAKEWORD(2, 2), &wsadata); //1.윈속 사용시작하기 s = socket(AF_INET, SOCK_STREAM, 0); //2.소켓 생성하기 addr.sin_family = AF_INET; addr.sin_port = 20; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); WSAAsyncSelect(s, hwnd, WM_ASYNC, FD_READ); //서버에서 보낸 데이터를 받아야 하므로 FD_READ 이벤트에 대해 WM_ASYNC윈도우 메시지를 등록 if (connect(s, (LPSOCKADDR)&addr, sizeof(addr)) == -1) //3.연결 요구하기(접속실패시 종료) return 0; break; case WM_ASYNC: //양방향 통신이므로 클라이언트가 다른 메시지를 처리하는 도중 서버에서 데이터를 보내면 switch (lParam) //WM_ASYNC와 lParam이 같이 도착한다. { case FD_READ: recv(s, buffer, 100, 0); if (!_tcscmp(buffer, _T("-1"))) //발사메시지 도착 { SetTimer(hwnd, 1, 15, NULL); MyTurn = TRUE; return 0; } else if (!_tcscmp(buffer, _T("2"))) //오른쪽 이동 메시지 도착 { server_x += 10; InvalidateRgn(hwnd, NULL, TRUE); return 0; } else if (!_tcscmp(buffer, _T("1"))) //왼쪽이동 메시지 도착 { server_x -= 10; InvalidateRgn(hwnd, NULL, TRUE); return 0; } break; default: break; } break; case WM_KEYDOWN: if (wParam == VK_SPACE&&MyTurn) { SetTimer(hwnd, 2, 15, NULL); _stprintf_s(buffer, _T("-1")); //발사 메시지 send(s, (LPSTR)buffer, 10, 0); MyTurn = FALSE; } else if (wParam == VK_RIGHT) { client_x += 10; client_boom_x = client_x; _stprintf_s(buffer, _T("2")); //오른쪽 이동 메시지 send(s, (LPSTR)buffer, 10, 0); InvalidateRgn(hwnd, NULL, TRUE); } else if (wParam == VK_LEFT) { client_x -= 10; client_boom_x = client_x; _stprintf_s(buffer, _T("1")); //왼쪽 이동 메시지 send(s, (LPSTR)buffer, 10, 0); InvalidateRgn(hwnd, NULL, TRUE); } break; case WM_TIMER: switch (wParam) { case 1: server_boom_x += 10; server_boom_y = 0.001f*pow(server_boom_x - 400, 2) + 150; InvalidateRgn(hwnd, NULL, TRUE); if (server_boom_y >= 400) { server_boom_x = server_x; server_boom_y = server_y; KillTimer(hwnd, 1); } break; case 2: client_boom_x -= 10; client_boom_y = 0.001f*pow(client_boom_x - 400, 2) + 150; InvalidateRgn(hwnd, NULL, TRUE); if (client_boom_y >= 400) { client_boom_x = client_x; client_boom_y = client_y; KillTimer(hwnd, 2); } break; } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); hBrush = CreateSolidBrush(RGB(0, 0, 0)); SelectObject(hdc, hBrush); Ellipse(hdc, server_x - 20, server_y - 20, server_x + 20, server_y + 20); hBrush = CreateSolidBrush(RGB(255, 255, 255)); SelectObject(hdc, hBrush); Ellipse(hdc, client_x - 20, client_y - 20, client_x + 20, client_y + 20); hBrush = CreateSolidBrush(RGB(0, 0, 255)); SelectObject(hdc, hBrush); Ellipse(hdc, server_boom_x - 5, server_boom_y - 5, server_boom_x + 5, server_boom_y + 5); hBrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, hBrush); Ellipse(hdc, client_boom_x - 5, client_boom_y - 5, client_boom_x + 5, client_boom_y + 5); EndPaint(hwnd, &ps); break; case WM_DESTROY: closesocket(s); WSACleanup(); PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); } | cs |
'WIN32 API' 카테고리의 다른 글
[WIN32 API] 10. 멀티스레드 (0) | 2018.01.24 |
---|---|
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 9번 풀이(미완성) (0) | 2018.01.09 |
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 5번 풀이 (0) | 2018.01.09 |
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 4번 풀이 (0) | 2018.01.09 |
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 3번 풀이 (0) | 2018.01.09 |