> вот тут весь мой БРЕД !!! > > > > #include <windows.h> > #include <stdio.h> > #define WM_SELECT WM_USER+1 > > LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); > > bool ServerSock(); > > char *str=new char[0x100]; > char *sendbuf=new char[0x100]; > char *getbuf=new char[0x1000]; > char s[0x100]; > int len; > int err; > > MSG msg; > HWND hWnd; > RECT rect; > > SOCKET sock,newsock; > WSADATA ws; > sockaddr_in addr, newaddr; > > int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE > hPrevInstance,LPSTR lpCmdLine,int nCmdShow) > { > > WNDCLASS wndclass ; > > 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) COLOR_WINDOW; > wndclass.lpszMenuName = NULL ; > wndclass.lpszClassName = "name" ; > > if(!RegisterClass(&wndclass)) > {MessageBox(NULL,"RegisterClass",NULL,NULL); return 0;} > > hWnd=CreateWindow("name","Win Socket > Async",WS_OVERLAPPEDWINDOW, > 10,10,350,250,NULL,NULL,hInstance,NULL); > > ShowWindow(hWnd,nCmdShow); > UpdateWindow(hWnd); > > ServerSock(); > > while(GetMessage(&msg,NULL,0,0)) > { > TranslateMessage(&msg); > DispatchMessage(&msg); > } > return msg.wParam; > return 0; > > } > LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM > wParam,LPARAM lParam) > { > PAINTSTRUCT ps; > HDC hdc; > int size; > > switch(msg) > { > case WM_CREATE: > len=err=0; *************Bot ona, cheremuha****************** > getbuf=sendbuf=""; *********************************************************
> str="Server sleep"; > break; > case WM_PAINT: > hdc = BeginPaint(hWnd, &ps); > > GetClientRect(hWnd,&rect); > len=0; > while(str[len]!='\0') > len++; > TextOut(hdc,10,10,str,len); > len=0; > while(getbuf[len]!='\0') > len++; > TextOut(hdc,10,30,getbuf,len); > > EndPaint(hWnd, &ps); > break; > case WM_DESTROY: > shutdown(sock,1); > closesocket(sock); > WSACleanup(); > PostQuitMessage(0); > return 1; > break; > case WM_SELECT: > switch(WSAGETSELECTEVENT(lParam)) > { > case FD_ACCEPT: > str="CLIENT CONNECT"; > InvalidateRect(hWnd,&rect,1); > size =sizeof(newaddr); > > newsock=accept(sock,(sockaddr*)&newaddr,&size); > if(newsock==INVALID_SOCKET) > { > str="invalid client > socket"; > > InvalidateRect(hWnd,&rect,1); > shutdown(newsock,1); > closesocket(newsock); > } > break; > case FD_READ: > err=recv(newsock,getbuf,0x100,0); > if(err==SOCKET_ERROR) > { > str="bad recv"; > switch(WSAGetLastError()) > { > case WSANOTINITIALISED: > str="1"; > break; > case WSAENETDOWN: > str="2"; > break; > case WSAEFAULT: > str="3"; > break; > case WSAENOTCONN: > str="4"; > break; > case WSAEINTR: > str="5"; > break; > case WSAEINPROGRESS: > str="6"; > break; > case WSAENETRESET: > str="7"; > break; > case WSAENOTSOCK: > str="8"; > break; > case WSAEOPNOTSUPP: > str="9"; > break; > case WSAESHUTDOWN: > str="10"; > break; > case WSAEWOULDBLOCK: > str="11"; > break; > case WSAEMSGSIZE: > str="12"; > break; > case WSAEINVAL: > str="13"; > break; > case WSAECONNABORTED: > str="14"; > break; > case WSAETIMEDOUT: > str="15"; > break; > case WSAECONNRESET: > str="16"; > break; > } > } > else > { > getbuf[err]='\0'; > > MessageBox(NULL,getbuf,NULL,NULL); > } > InvalidateRect(hWnd,&rect,1); > break; > case FD_WRITE: > // InvalidateRect(hWnd,&rect,1); > // MessageBox(NULL,getbuf,NULL,NULL); > break; > } > break; > default: > return > DefWindowProc(hWnd,msg,wParam,lParam); > break; > } > return 0; > } > bool ServerSock() > { > if(WSAStartup(0x101,&ws)) > {str="WSAStartup > Error";InvalidateRect(hWnd,&rect,1);return 0;} > > sock=socket(AF_INET,SOCK_STREAM,0); > addr.sin_addr.s_addr=INADDR_ANY; > addr.sin_family=AF_INET; > addr.sin_port=htons(80); > > if(bind(sock,(sockaddr*)&addr,sizeof(addr))) > {str="this port is used"; > InvalidateRect(hWnd,&rect,1); return 0;} > > if(listen(sock,5)) > {str="listen Error"; > InvalidateRect(hWnd,&rect,1);return 0;} > > WSAAsyncSelect(sock,hWnd,WM_SELECT,FD_ACCEPT|FD_READ|FD_CLO > SE); > > str="soket init !!!"; > InvalidateRect(hWnd,&rect,1); > > return 1; > > }
|