오다기리 박의 알고리즘 노트

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 2번 풀이 본문

WIN32 API

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 9장 2번 풀이

오다기리 박 2018. 1. 9. 17:59

-서버 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
211
212
213
214
215
216
217
218
219
//논블로킹 단방향통신에서 서버가 클라이언트로 메시지를 보내는 코드를 추가한다.
#include <Windows.h>
#include<tchar.h>
#include<string.h>
#include<stdio.h>
#include"resource.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 = MAKEINTRESOURCE(IDR_MENU1);
    WndClass.lpszClassName = _T("Window Class Name");
 
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(
        _T("Window Class Name"),
        _T("Server Window"),
        WS_OVERLAPPEDWINDOW,
        50,
        50,
        700,
        700,
        NULL,
        NULL,
        hInstance,
        NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
 
#define IDC_BUTTON1 102
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static WSADATA wsadata;
    static SOCKET s, cs;
    static TCHAR msg[200];
    static HWND hButton1;
 
 
    static SOCKADDR_IN addr = { 0 }, c_addr;
    static int recv_x1 = 0, recv_x2 = 0, recv_y1 = 0, recv_y2 = 0;
    static int recv_shape = 0;
    static int pos_y = 0;
    static int mx = 0, my = 0;
 
    static bool Drag = false;
    static int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
    static int shape = 0;
    static int token2[5];
 
    int  size, msgLen;
    char buffer[100];
    char *context;
    char *token;
    int i = 0;
 
    switch (iMsg)
    {
    case WM_CREATE:
        hButton1 = (HWND)CreateWindow(_T("button"), _T("전송"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 30005020, hwnd, (HMENU)IDC_BUTTON1, hInst, NULL);
        WSAStartup(MAKEWORD(22), &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:
            msgLen = recv(cs, buffer, 1000);
            buffer[msgLen] = NULL;
#ifdef _UNICODE                    //프로젝트의 문자집합이 유니코드로 설정되어있다면
            msgLen = MultiByteToWideChar(CP_ACP, 0, buffer, strlen(buffer), NULLNULL);
            MultiByteToWideChar(CP_ACP, 0, buffer, strlen(buffer), msg, msgLen);
            msg[msgLen] = NULL;
#else
            strcpy_s(msg, buffer);    //msg는 TCHAR형으로 선언되었기 때문에 문자 집합에 따라 WCHAR나 char로 자동변환된다
#endif // _UNICODE
            InvalidateRgn(hwnd, NULL, TRUE);
            break;
        default:
            break;
        }
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_BUTTON1:
            if (cs == INVALID_SOCKET)
            {
                MessageBox(hwnd, _T("소켓cs가 INVALID_SOCKET"), _T("알림"), NULL);
                break;
            }
            _stprintf_s(buffer, _T("%d,%d,%d,%d,%d"), shape, x1, y1, x2, y2);
            send(cs, buffer, 200);
            break;
        case ID_LINE:
            shape = 1;
            break;
        case ID_ELLIPSE:
            shape = 2;
            break;
        case ID_RECTANGLE:
            shape = 3;
            break;
        }
        break;
    case WM_LBUTTONDOWN:
        Drag = true;
        x1 = LOWORD(lParam);
        y1 = HIWORD(lParam);
        x2 = LOWORD(lParam);
        y2 = HIWORD(lParam);
        break;
    case WM_MOUSEMOVE:
        if (Drag)
        {
            x2 = LOWORD(lParam);
            y2 = HIWORD(lParam);
            InvalidateRgn(hwnd, NULL, TRUE);
        }
        break;
    case WM_LBUTTONUP:
        Drag = false;
        x2 = LOWORD(lParam);
        y2 = HIWORD(lParam);
        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        if (msg != NULL && !Drag)
        {
            context = NULL;
            token = strtok_s(msg, ","&context);            //context에는 분리된 후 남은 문자열이 들어간다.
            while (token != NULL)
            {
                token2[i++= atoi(token);
                token = strtok_s(NULL","&context);
                pos_y += 20;
            }
            recv_shape = token2[0];
            recv_x1 = token2[1];
            recv_y1 = token2[2];
            recv_x2 = token2[3];
            recv_y2 = token2[4];
            InvalidateRgn(hwnd, NULL,FALSE);
        }
        switch (recv_shape)
        {
        case 1:
            MoveToEx(hdc, recv_x1, recv_y1, NULL);
            LineTo(hdc, recv_x2, recv_y2);
            break;
        case 2:
            Ellipse(hdc, recv_x1, recv_y1, recv_x2, recv_y2);
            break;
        case 3:
            Rectangle(hdc, recv_x1, recv_y1, recv_x2, recv_y2);
            break;
        }
        switch (shape)
        {
        case 1:
            MoveToEx(hdc, x1, y1, NULL);
            LineTo(hdc, x2, y2);
            break;
        case 2:
            Ellipse(hdc, x1, y1, x2, y2);
            break;
        case 3:
            Rectangle(hdc, x1, y1, x2, y2);
            break;
        }
        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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//6번소스와 동일
#include <Windows.h>
#include<tchar.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include"resource.h"
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 = MAKEINTRESOURCE(IDR_MENU1);
    WndClass.lpszClassName = _T("Window Class Name");
 
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(
        _T("Window Class Name"),
        _T("Client Window"),
        WS_OVERLAPPEDWINDOW,
        50,
        50,
        700,
        700,
        NULL,
        NULL,
        hInstance,
        NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
#define IDC_BUTTON1 102
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 TCHAR msg[200];
    static HWND hButton1;
 
    
    static int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
    static int shape = 0;
    static int recv_x1 = 0, recv_x2 = 0, recv_y1 = 0, recv_y2 = 0;
    static int recv_shape = 0;
    
    static int token2[5];
    static int mx = 0, my = 0;
    static bool Drag = false;
    
    int msgLen;
    char buffer[100];
    char *context;
    char *token;
    int pos_y = 0;
    int i = 0;
 
    switch (iMsg)
    {
    case WM_CREATE:
        hButton1 = (HWND)CreateWindow(_T("button"), _T("전송"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 30005020, hwnd, (HMENU)IDC_BUTTON1, hInst, NULL);
        WSAStartup(MAKEWORD(22), &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:
            msgLen = recv(s, buffer, 1000);
            buffer[msgLen] = NULL;
#ifdef _UNICODE                    //프로젝트의 문자집합이 유니코드로 설정되어있다면
            msgLen = MultiByteToWideChar(CP_ACP, 0, buffer, strlen(buffer), NULLNULL);
            MultiByteToWideChar(CP_ACP, 0, buffer, strlen(buffer), msg, msgLen);
            msg[msgLen] = NULL;
#else
            strcpy_s(msg, buffer);    //msg는 TCHAR형으로 선언되었기 때문에 문자 집합에 따라 WCHAR나 char로 자동변환된다
#endif // _UNICODE
            InvalidateRgn(hwnd, NULL, TRUE);
            break;
        default:
            break;
        }
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_BUTTON1:
            if (s == INVALID_SOCKET)
            {
                MessageBox(hwnd, _T("소켓s가 INVALID_SOCKET"), _T("알림"), NULL);
                break;
            }
            _stprintf_s(buffer, _T("%d,%d,%d,%d,%d"), shape, x1, y1, x2, y2);
            send(s, buffer, 200);
            break;
        case ID_LINE:
            shape = 1;
            break;
        case ID_ELLIPSE:
            shape = 2;
            break;
        case ID_RECTANGLE:
            shape = 3;
            break;
        }
        break;
    case WM_LBUTTONDOWN:
        Drag = true;
        x1 = LOWORD(lParam);
        y1 = HIWORD(lParam);
        x2 = LOWORD(lParam);
        y2 = HIWORD(lParam);
        //InvalidateRgn(hwnd, NULL, TRUE);
        break;
    case WM_MOUSEMOVE:
        if (Drag)
        {
            x2 = LOWORD(lParam);
            y2 = HIWORD(lParam);
            InvalidateRgn(hwnd, NULL, TRUE);
        }
        break;
    case WM_LBUTTONUP:
        Drag = false;
        x2 = LOWORD(lParam);
        y2 = HIWORD(lParam);
        InvalidateRgn(hwnd, NULL, TRUE);
        break;
 
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        if (msg != NULL && !Drag)
        {
            context = NULL;
            token = strtok_s(msg, ","&context);            //context에는 분리된 후 남은 문자열이 들어간다.
            while (token != NULL)
            {
                token2[i++= atoi(token);
                //TextOut(hdc, 0, pos_y, token, _tcslen(token));
                token = strtok_s(NULL","&context);
                pos_y += 20;
            }
            recv_shape = token2[0];
            recv_x1 = token2[1];
            recv_y1 = token2[2];
            recv_x2 = token2[3];
            recv_y2 = token2[4];
            InvalidateRgn(hwnd, NULL, FALSE);
        }
        
        switch (recv_shape)
        {
        case 1:
            MoveToEx(hdc, recv_x1, recv_y1, NULL);
            LineTo(hdc, recv_x2, recv_y2);
            break;
        case 2:
            Ellipse(hdc, recv_x1, recv_y1, recv_x2, recv_y2);
            break;
        case 3:
            Rectangle(hdc, recv_x1, recv_y1, recv_x2, recv_y2);
            break;
        }
        switch (shape)
        {
        case 1:
            MoveToEx(hdc, x1, y1, NULL);
            LineTo(hdc, x2, y2);
            break;
        case 2:
            Ellipse(hdc, x1, y1, x2, y2);
            break;
        case 3:
            Rectangle(hdc, x1, y1, x2, y2);
            break;
        }
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        closesocket(s);
        WSACleanup();
        PostQuitMessage(0);
        break;
 
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
cs