간단한 서버 클라이언트 프로그램
1. 서버에서 왼쪽마우스를 클릭하여 accept함수를 구동시킴
2. 클라이언트에서 왼쪽마우스를 클릭하여 connect함수를 구동시킴
전송
결과 : 연결되고 나서 서버 -----------------------------> 클라이언트
"Hello World"
3. 클라이언트에서 오른쪽마우스를 클릭하여 화면에 문자열을 삭제
4. 다시 1~3번 과정을 반복하여 문자열을 화면에 표시
끝~~~~~~~~~~~~~~~~~~~~~
server 소스
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
void ErrorHandling(char* message);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("Server");
void error_handling(char *message);
WSADATA wsaData;
SOCKET hServSock, hClntSock;
SOCKADDR_IN servAddr, clntAddr;
int szClntAddr;
char message[]="Hello World!";
HDC hdc;
HWND Hwnd;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while (GetMessage(&Message,NULL,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return (int)Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
Hwnd = hWnd;
switch (iMessage) {
case WM_DESTROY:
closesocket(hServSock);
WSACleanup();
PostQuitMessage(0);
return 0;
case WM_CREATE:
hdc = GetDC(hWnd);
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHandling("WSAStartup() error!");
hServSock=socket(PF_INET, SOCK_STREAM, 0);
if(hServSock==INVALID_SOCKET)
ErrorHandling("socket() error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(atoi("8080"));
if(bind(hServSock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
{
ErrorHandling("bind() error");
}
if(listen(hServSock, 5)==SOCKET_ERROR)
ErrorHandling("listen() error");
ReleaseDC(Hwnd, hdc);
return 0;
case WM_LBUTTONDOWN:
szClntAddr=sizeof(clntAddr);
hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr,&szClntAddr);
if(hClntSock==INVALID_SOCKET)
ErrorHandling("accept() error");
send(hClntSock, message, sizeof(message), 0);
closesocket(hClntSock);
return 0;
case WM_RBUTTONDOWN:
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
void ErrorHandling(char* message)
{
hdc = GetDC(Hwnd);
TextOut(hdc, 200, 200, message, 5);
ReleaseDC(Hwnd, hdc);
}
client 소스
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
void ErrorHandling(char* message);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("Client");
WSADATA wsaData;
SOCKET hSocket;
SOCKADDR_IN servAddr;
char message[30];
TCHAR *message1 = "HellO!";
TCHAR *error = "error";
int strLen;
HDC hdc;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while (GetMessage(&Message,NULL,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return (int)Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
switch (iMessage) {
case WM_DESTROY:
WSACleanup();
PostQuitMessage(0);
return 0;
case WM_CREATE:
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
ErrorHandling("WSAStartup() error!");
return 0;
case WM_LBUTTONDOWN:
hSocket=socket(PF_INET, SOCK_STREAM, 0);
if(hSocket==INVALID_SOCKET)
ErrorHandling("hSocketet() error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servAddr.sin_port=htons(atoi("8080"));
hdc = GetDC(hWnd);
if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr))==SOCKET_ERROR)
{
ErrorHandling("connect() error!");
TextOut(hdc, 300, 300, error, 5);
}
strLen=recv(hSocket, message, sizeof(message)-1, 0);
message1 = &message[0];
TextOut(hdc, 100, 100, message1, strlen(message1));
ReleaseDC(hWnd, hdc);
closesocket(hSocket);
return 0;
case WM_RBUTTONDOWN:
InvalidateRect(hWnd, NULL, TRUE);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
void ErrorHandling(char* message)
{
TextOut(hdc, 200, 200, error, 5);
}