본문 바로가기
IT창고/Network

WSAAsyncSelect//화이트보드 클라이언트에서 서버에 좌표보내기

by 창구창고 2007. 1. 22.
반응형
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

struct LINE
{
	POINTS ptFrom;
	POINTS ptTo;
};

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static SOCKET link_socket;
	static POINTS ptFrom ,pt ;
	switch( msg )
	{	
	case WM_LBUTTONDOWN:
		ptFrom = MAKEPOINTS(lParam);
		return 0;
	case WM_MOUSEMOVE:
		if(wParam & MK_LBUTTON)
		{
			POINTS pt = MAKEPOINTS(lParam);

			HDC hdc = GetDC(hwnd);

			MoveToEx(hdc, ptFrom.x, ptFrom.y, 0);
			LineTo	(hdc, pt.x, pt.y);

			ReleaseDC(hwnd, hdc);

			//서버로 보낸다.
			LINE line;
			line.ptFrom = ptFrom;
			line.ptTo	= pt;

			send(link_socket, (char *)&line, sizeof(line), 0);
			ptFrom = pt;
		}
		return 0;
	case WM_USER+100:
		{
			WORD event = WSAGETSELECTEVENT( lParam );
			WORD error = WSAGETSELECTERROR( lParam );

			switch( event)
			{
			case FD_CONNECT:
				{
					if(error != 0)
					{
						MessageBox(0, "Connect Error","",MB_OK);
						return 0;
					}

					SetWindowText(hwnd, "Server에 접속이 되었습니다");

					//이제 data의 도착과 접속 해지에 대해 요청
					WSAAsyncSelect(link_socket, hwnd, WM_USER+100,FD_CLOSE | FD_READ);
				}
				return 0;

			case FD_CLOSE:
				SetWindowText(hwnd, "Server와 접속이 끊어 졌습니다.");
				closesocket(link_socket);
				return 0;

			case FD_READ:
				{
					//  data 도착 - 읽어 낸다.
					LINE line;
					recv(link_socket, (char *)&line, sizeof(line), 0 );

					HDC hdc = GetDC(hwnd);
					MoveToEx(hdc, line.ptFrom.x,	line.ptFrom.y ,0);
					LineTo(hdc, line.ptTo.x,		line.ptTo.y );	
					ReleaseDC(hwnd, hdc);
				}
				return 0;
			}
		}
		return 0;
	case WM_CREATE:
		{
			link_socket = socket(AF_INET, SOCK_STREAM, 0);
			//비동기 상태로 변경한다.
			WSAAsyncSelect(link_socket, hwnd, WM_USER+100, FD_CONNECT);

			SOCKADDR_IN addr;
			addr.sin_family			= AF_INET;
			addr.sin_port			= htons(6000);
			addr.sin_addr.s_addr	= inet_addr("61.81.99.54");

			connect(link_socket, (SOCKADDR *)&addr, sizeof(addr));

		}
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0); 
		return 0;
	}
	return DefWindowProc( hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR	  lpCmdLine, int nShowCmd )
{
	WSADATA wsadata;
	if ( WSAStartup( MAKEWORD(2,2), &wsadata) != 0 )
	{
		MessageBox( 0, "Error", "", MB_OK);
		return 0;
	}

	ATOM atom;
	WNDCLASS wc;
	HWND hwnd;
	MSG msg;
	
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hbrBackground= (HBRUSH)GetStockObject( WHITE_BRUSH ); 
	wc.hCursor		= LoadCursor( 0, IDC_ARROW );
	wc.hIcon		= LoadIcon( 0, IDI_APPLICATION);
	wc.hInstance	= hInstance;
	wc.lpfnWndProc  = WndProc; 
	wc.lpszClassName= "First";
	wc.lpszMenuName = 0;
	wc.style		= 0;

	atom = RegisterClass( &wc);
	
	if ( atom == 0 ) 
	{
		MessageBox( 0, "Fail To RegisterClass", "Error", MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx( 0, "first", "White Board Client", WS_OVERLAPPEDWINDOW,
								CW_USEDEFAULT, 0, CW_USEDEFAULT,0, 0, 0, 
								hInstance, 0); 
	ShowWindow( hwnd, nShowCmd); 
	UpdateWindow( hwnd );

	while ( GetMessage( &msg, 0, 0, 0) ) 
	{							
		TranslateMessage(&msg);
		DispatchMessage( &msg);
	}

	WSACleanup();
	return 0;
}


 
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
struct LINE
{
	POINTS ptFrom;
	POINTS ptTo;
};
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static SOCKET listen_socket, link_socket;

	switch( msg )
	{	
	case WM_USER+100:
		{
			WORD event = WSAGETSELECTEVENT( lParam );
			WORD error = WSAGETSELECTERROR( lParam );

			switch( event )
			{

			case FD_CLOSE:
				SetWindowText(hwnd, "Client가 접속이 끊어 졌습니다.");
				closesocket(link_socket);
				return 0;
			case FD_READ:
				{
					//  data 도착 - 읽어 낸다.
					LINE line;
					recv(link_socket, (char *)&line, sizeof(line), 0 );

					HDC hdc = GetDC(hwnd);
					MoveToEx(hdc, line.ptFrom.x,	line.ptFrom.y ,0);
					LineTo(hdc, line.ptTo.x,		line.ptTo.y );	
					ReleaseDC(hwnd, hdc);
				}
				return 0;
			case FD_ACCEPT:
				{
					SOCKADDR_IN addr;

					int size = sizeof(addr);

					link_socket = accept(listen_socket, (SOCKADDR *)&addr, &size);

					//대기 소켓을 닫는다.
					closesocket(listen_socket);
					
					SetWindowText(hwnd, "Client가 접속 되었습니다.");

					//연결된 소켓을 비동기 소켓을 변경한다.
					WSAAsyncSelect(link_socket, hwnd, WM_USER+100, FD_READ | FD_CLOSE);
				}
				break;
			}
		}
		return 0;
	case WM_CREATE:
		{
			listen_socket = socket( AF_INET, SOCK_STREAM , 0);
			
			//socket을 async 상태로 변경한다.
			//-----------------------------------------------------------
			WSAAsyncSelect( listen_socket, hwnd, WM_USER+100, FD_ACCEPT);
			//누군가 접속을 하면 프로세스에게 알려달라. -> OS에게 요청 : 더이상 억셉트할 필요 없다.
			//-----------------------------------------------------------
			SOCKADDR_IN addr;
			addr.sin_family			= AF_INET;
			addr.sin_port			= htons(6000);
			addr.sin_addr.s_addr	= INADDR_ANY;
			
			bind( listen_socket, (SOCKADDR *)&addr, sizeof(addr));

			listen(listen_socket, 5);

			//이제 accept()차례
		}
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0); 
		return 0;
	}
	return DefWindowProc( hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR	  lpCmdLine, int nShowCmd )
{
	WSADATA wsadata;
	if ( WSAStartup( MAKEWORD(2,2), &wsadata) != 0 )
	{
		MessageBox( 0, "Error", "", MB_OK);
		return 0;
	}

	ATOM atom;
	WNDCLASS wc;
	HWND hwnd;
	MSG msg;
	
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hbrBackground= (HBRUSH)GetStockObject( WHITE_BRUSH ); 
	wc.hCursor		= LoadCursor( 0, IDC_ARROW );
	wc.hIcon		= LoadIcon( 0, IDI_APPLICATION);
	wc.hInstance	= hInstance;
	wc.lpfnWndProc  = WndProc; 
	wc.lpszClassName= "First";
	wc.lpszMenuName = 0;
	wc.style		= 0;

	atom = RegisterClass( &wc);
	
	if ( atom == 0 ) 
	{
		MessageBox( 0, "Fail To RegisterClass", "Error", MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx( 0, "first", "White Board Server", WS_OVERLAPPEDWINDOW,
								CW_USEDEFAULT, 0, CW_USEDEFAULT,0, 0, 0, 
								hInstance, 0); 
	ShowWindow( hwnd, nShowCmd); 
	UpdateWindow( hwnd );

	while ( GetMessage( &msg, 0, 0, 0) ) 
	{							
		TranslateMessage(&msg);
		DispatchMessage( &msg);
	}

	WSACleanup();
	return 0;
}

반응형

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."