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

case (WSAEINVAL): return "invalid socket operation";

case (WSAEISCONN): return "already connected";

case (WSAENETUNREACH): return "network unreachable";

case (WSAENOBUFS): return "no buffer space available";

case (WSAENOTSOCK): return "not a socket";

case (WSAETIMEDOUT): return "timeout";

case (WSAEWOULDBLOCK): return "socket nonblocking, but operation blocks";

case (WSAEACCES): return "broadcast address given";

}

return "Unknown error";

}

int cmpstruct(pack st, pack nd){

int i,j,k=0;

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

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

if(st.matr[i][j] != nd.matr[i][j]) {

k++;

}

}

}

return(k);

}

int testnew(pack st, pack nd){

int i,j,k=0;

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

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

if((st.matr[i][j] != nd.matr[i][j]) &&

st.matr[i][j] == 0 && nd.matr[i][j] != 0) {

k = k + 1;

}

}

}

return(k);

}

PTHREAD_DATA dt=NULL;

DWORD WINAPI ThreadAction_serv(LPVOID param) {

union packd p;

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

// struct sockaddr addr;

struct sockaddr_in addr;

addr.sin_addr.s_addr = INADDR_ANY;

int size = sizeof(caddr),tmp = -1,e;

fd_set rfds;

struct timeval tv;

HWND hWnd = HWND(param);

FD_ZERO(&rfds);

FD_SET(s, &rfds);

tv.tv_sec = 8192;

tv.tv_usec = 0;

int retval = select(1, &rfds, NULL, NULL, &tv);

while (retval) {

e = recvfrom(s,p.buff, sizeof(p.buff)+1,0,(sockaddr*)&caddr,&size);

caddr.sin_port = htons(CLNT_PORT);

switch(p.p.action){

case('p'):

Sleep(1);

p.p = ar;

e = sendto(s_cl, p.buff, sizeof(p)+1,0,(PSOCKADDR)&caddr, sizeof(caddr));

if (e == -1) err("serv ping send");

started = true;

tmp = testnew(ar,p.p);

if (tmp < 2 && tmp != 0 ){

ar = p.p;

}

drawme(hWnd);

break;

case('i'):

tmp = testnew(ar,p.p);

if (tmp < 2 && tmp != -1) {

ar = p.p;

drawme(hWnd);

p.p.action = 'r';

e = sendto(s_cl, p.buff, sizeof(p)+1,0,(PSOCKADDR)&caddr, sizeof(caddr));

if (e == -1) err("serv reply send");

}

break;

case('r'):

tmp = testnew(ar,p.p);

trd++;

tr = 0;

break;

}

tv.tv_sec = 4 * 60;

retval = select(1, &rfds, NULL, NULL, &tv);

}

MessageBox(NULL,"end of serv die to timeout .","tip",MB_OK);

closesocket(s);

SetEvent(finished);

return(0);

}

DWORD WINAPI ThreadAction_cl(LPVOID param) {

union packd p;

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

struct sockaddr_in addr;

int size = sizeof(addr),tmp,e;

fd_set rfds;

struct timeval tv;

HWND hWnd = HWND(param);

lstrcpy(p.buff,"pest string at client start .");

saddr.sin_addr.s_addr = inet_addr("192.168.86.171");

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

p.p.action = 'p';

sendto(s, p.buff, sizeof(p)+1,0,(PSOCKADDR)&saddr, sizeof(saddr));

if (e == -1) err("client 1st ping send");

FD_ZERO(&rfds);

FD_SET(s_cl, &rfds);

tv.tv_sec = 4 * 60;

tv.tv_usec = 0;

int retval;

/* SetEvent(finished);

closesocket(s_cl);

return(0);

*/

retval = select(1, &rfds, NULL, NULL, &tv);

while (retval) {

e = recvfrom(s_cl,p.buff,sizeof(p.buff)+1,0,(sockaddr*)&addr,&size);

saddr.sin_port = htons(SERV_PORT);

switch(p.p.action) {

case('p'):

p.p = ar;

e = sendto(s, p.buff, sizeof(p)+1,0,(PSOCKADDR)&saddr, sizeof(saddr));

if (e == -1) err("client ping send");

started = true;

if (testnew(ar,p.p) < 2) {

ar = p.p;

}

drawme(hWnd);

break;

case('i'):

tmp = testnew(ar,p.p);

if (tmp < 2 && tmp != 0 ) {

ar = p.p;

drawme(hWnd);

p.p.action = 'r';

e = sendto(s, p.buff, sizeof(p)+1,0,(PSOCKADDR)&saddr, sizeof(saddr));

if (e == -1) err("client reply error");

}

break;

case('r'):

tmp = testnew(ar,p.p);

trd++;

tr = 0;

if (tmp != 0 ){

MessageBox(NULL,"something wrong","At server got: ",MB_OK);

}

break;

}

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

retval = select(1, &rfds, NULL, NULL, &tv);

}

MessageBox(NULL,"end of client (ie timeout .)","tip",MB_OK);

closesocket(s_cl);

SetEvent(finished);

return(0);

}

DWORD WINAPI ThreadAction_put(LPVOID param) {

union packd pa;

/*CString ss;

ss= (char*)param;

pa.buff = ss;

*/

/* char * ss = (char *)param;

for (int i = 0 ; i <= sizeof(pa.buff); i++) {

pa.buff[i] = ss[i];

}

*/ pa.p = ar;

pa.p.action = 'i';

int e;

tr ++;

while (tr >= trd) {

if (! iamserv && iamclient) {

saddr.sin_port = htons(SERV_PORT);

e = sendto(s, pa.buff, sizeof(pa.buff)+1, 0,(PSOCKADDR)&saddr, sizeof(saddr));

} ;

if (iamserv && ! iamclient){

caddr.sin_port = htons(CLNT_PORT);

e = sendto(s_cl, pa.buff, sizeof(pa.buff)+1, 0,(PSOCKADDR)&caddr, sizeof(caddr));

}

Sleep(1);

}

trd = 0; tr = 0;

return(0);

}

void putplus(HWND hWnd, char flag,char i, char j) {

union packd p;

// p.p = ar;

// p.p.action = 'i';

int e, timer;

if (ar.matr[i][j]==0) ar.matr[i][j] = flag;

ar.action = 'i';

p.p = ar;

drawme(hWnd);

CloseHandle(finished_put);

DWORD threadID;

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

thread_put = CreateThread(

NULL,65536,ThreadAction_put,(LPVOID)p.buff,CREATE_SUSPENDED,&threadID

);

ResumeThread(thread_put);

return;

// return(true);

}

void drawme(HWND hWnd){

HDC hdc = GetDC(hWnd);

DWORD threadID;

int x,y;

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

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

if (ar.matr[x][y] == ZERO) Ellipse(hdc,x*SIDE,y*SIDE,(x+1)*SIDE,(y+1)*SIDE);

// putplus(PLUS,x,y);

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

MoveToEx(hdc,x*SIDE,y*SIDE,NULL);

LineTo(hdc,(x+1)*SIDE,(y+1)*SIDE);

MoveToEx(hdc,(x+1)*SIDE,y*SIDE,NULL);

LineTo(hdc,x*SIDE,(y+1)*SIDE);

}

}

}

// MoveTo(x * side, y* side);

// LineTo((x+1)* side, (y+1)*side);

/* if (x < 4 && x>0 && y < 4 && y > 0) {

Ellipse(hdc,x*side,y*side,(x+1)*side,(y+1)*side);

putplus(PLUS,x,y);

MoveToEx(hdc, x*side,y*side,NULL);

LineTo(hdc,(x+1)*side,(y+1)*side);

MoveToEx(hdc,(x+1)*side,y*side,NULL);

LineTo(hdc,x*side,(y+1)*side);

}

*/

// putminus(x,y);

ReleaseDC(hWnd, hdc);

return;

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


Страница: