Сетевые крестики нолики
Рефераты >> Коммуникации и связь >> Сетевые крестики нолики

thread_draw = CreateThread(

NULL,65536,ThreadAction_cl,(LPVOID)hWnd,CREATE_SUSPENDED,&threadID

);

ResumeThread(thread_draw);

started=true;

}

DWORD WINAPI ThreadAction_drawme(LPVOID param) {

HDC hdc = GetDC((HWND)param);

Ellipse(hdc,2 * SIDE, 2 * SIDE, (2+1) * SIDE,(2+1) * SIDE);

ReleaseDC((HWND)param, hdc);

SetEvent(finished_draw);

return(2);

}

void initme(){

s = socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);

saddr.sin_family=AF_INET; /* Target address is IP */

saddr.sin_port=htons(SERV_PORT);

saddr.sin_addr.s_addr = INADDR_ANY;

s_cl = socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);

caddr.sin_family = AF_INET;

caddr.sin_addr.s_addr = INADDR_ANY; //= inet_addr("127.168.86.167");

caddr.sin_port = htons(CLNT_PORT);

}

void startserv(HWND hWnd) {

// Handle для Thread'а, используется в Resume/Suspend/Destroy

DWORD threadID;

// ThreadID, используется для

//[13:05:31] <Dalth>

//OpenThread, по-моему, и для некоторых других целей

if (bind(s,SACONV(&saddr),sizeof(saddr))!=0){

MessageBox(NULL,"bind at server error","bind",MB_OK);

};

finished = CreateEvent(NULL,TRUE,FALSE,NULL);

thread = CreateThread(

NULL,65536,ThreadAction_serv,(LPVOID)hWnd,CREATE_SUSPENDED,&threadID

);

ResumeThread(thread);

}

bool clientconnect(HWND hWnd) {

CloseHandle(finished_cl);

DWORD threadID;

int rc = bind(s_cl,SACONV(&caddr),sizeof(caddr));

if ( rc != 0 ) {

}

// here was bug

finished_cl = CreateEvent(NULL,TRUE,FALSE,NULL);

thread_cl = CreateThread(

NULL,65536,ThreadAction_cl,(LPVOID)hWnd,CREATE_SUSPENDED,&threadID

);

ResumeThread(thread_cl);

return(true);

}

void ServerStop(HWND hWnd) {

CloseHandle(finished);

CloseHandle(finished_draw);

CloseHandle(finished_cl);

CloseHandle(finished_put);

started = false;

closesocket(s);

closesocket(s_cl);

}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,

int nCmdShow){

// TODO: Place code here.

MSG msg;

HACCEL hAccelTable;

// Initialize global strings

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

LoadString(hInstance, IDC_SERVE, szWindowClass, MAX_LOADSTRING);

MyRegisterClass(hInstance);

// Perform application initialization:

if (!InitInstance (hInstance, nCmdShow))

{

return FALSE;

}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SERVE);

// Main message loop:

while (GetMessage(&msg, NULL, 0, 0)) {

if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return msg.wParam;

}

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = (WNDPROC)WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SERVE);

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = (LPCSTR)IDC_SERVE;

wcex.lpszClassName = szWindowClass;

wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);

}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {

HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

// hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

// CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, 200, 256, NULL, NULL, hInstance, NULL);

if (!hWnd) {

return FALSE;

}

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

return TRUE;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

PAINTSTRUCT ps;

HDC hdc;

RECT rt;

int wmId, wmEvent,rc;

TCHAR szHello[MAX_LOADSTRING];

LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

unsigned char i,j, x,y;

switch (message)

{

case WM_CREATE:

WSADATA WSAData;

// Инициализация и проверка версии Windows Sockets

rc = WSAStartup(MAKEWORD(1, 1), &WSAData);

initme();

if (rc != 0){

MessageBox(NULL, "WSAStartup Error", "Error", MB_OK);

return FALSE;

} else {

return TRUE;

}

case WM_COMMAND:

wmId = LOWORD(wParam);

wmEvent = HIWORD(wParam);

// Parse the menu selections:

switch (wmId)

{

case IDM_ABOUT:

DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);

break;

case IDM_EXIT:

DestroyWindow(hWnd);

break;

case IDM_START:

if (! iamclient) {

selected = true;

iamserv = true;

startserv(hWnd);

memset(&ar,0,sizeof(ar));

ar.step = 1;

} else {

MessageBox(NULL,"I am already client .","tip",MB_OK);

}

break;

case IDM_STOP:

selected = false;

// if (iamserv) ServerStop(hWnd);

iamserv = false;

iamclient = false;

break;

case IDM_CONNECT:

if ( ! iamserv && clientconnect(hWnd)) {

memset(&ar,0,sizeof(ar));

iamclient = true;

selected = true;

} else {

MessageBox(NULL,"I am already server .","tip",MB_OK);

}

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

break;

case WM_LBUTTONDOWN:

x = (char)(LOWORD(lParam) / SIDE);

y = (char)(HIWORD(lParam) / SIDE );

sstep = ( 2 * (char)(ar.step / 2 ) != (char)ar.step);

if (iamserv && ! iamclient ) {

if (ar.matr[x][y] == 0 ) {

ar.matr[x][y] = PLUS;

ar.step = ar.step + 1;

putplus(hWnd,PLUS,x,y);

}

}

cstep = ( 2 * (char)(ar.step / 2 ) == (char)ar.step);

if (! iamserv && iamclient) {

if (ar.matr[x][y] == 0 ) {

ar.matr[x][y] = ZERO;

ar.step = ar.step + 1;

putplus(hWnd,ZERO,x,y);

}

}

// if ( 2 * (char)(ar.step / 2 ) == (char)ar.step) ar.matr[x][y] = ZERO;

// if ( 2 * (char)(ar.step / 2 ) != (char)ar.step) ar.matr[x][y] = PLUS;

// drawme(hWnd);

break;

case WM_RBUTTONDOWN:

x = (char)(LOWORD(lParam) / SIDE );

y = (char)(HIWORD(lParam) / SIDE );

ar.matr[x][y] = PLUS;

drawme(hWnd);

break;

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here .

GetClientRect(hWnd, &rt);

for (i=1;i<=3;i++) {

for (j=1;j<=3;j++) {

Rectangle(hdc,SIDE *i,SIDE*j,SIDE * i + SIDE,SIDE *j + SIDE);

}

}

EndPaint(hWnd, &ps);


Страница: