WsaAsyncSelect服务器端代码



#include <iostream>
#include <Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
#define WM_SOCKET WM_USER+100
#define MAX_LEN 500
SOCKET s_Sever;
HWND hwnd;


bool InitSock();
int CreateWnd( HINSTANCE hInstance);
//LRESULT CallWindowProc(WNDPROC lpPrevWndFunc,HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam );


int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
int err = -1;

CreateWnd(hInstance);
// InitSock();


return 0;
}
bool InitSock()
{
//1 初始化套接字
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) 
{
return false;
}




if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return false; 
}
//2 创建套接字
s_Sever = socket(AF_INET, SOCK_STREAM, 0);
//if(s_Sever)
//绑定套接字
SOCKADDR_IN addrLocal;
addrLocal.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrLocal.sin_port = htons(6000);
addrLocal.sin_family = AF_INET;
err = bind(s_Sever,(SOCKADDR*)&addrLocal,sizeof(addrLocal));
if(err == SOCKET_ERROR)
{
return false;
}
err = WSAAsyncSelect(s_Sever, hwnd,WM_SOCKET,FD_ACCEPT|FD_CLOSE|FD_READ|FD_WRITE);
if (SOCKET_ERROR == err)
{
MessageBox(hwnd, "WsaAsyncSelect error","error", MB_OK);
}


//监听套接字
err = listen(s_Sever,5);
if(err == SOCKET_ERROR)
{
return false;
}

return true;  //秋风秋雨秋煞人
}
//回调函数
LRESULT CALLBACK WinCallBack(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch (uMsg)
{
case WM_SOCKET:
        {
            // 取得有事件发生的套节字句柄
            SOCKET s = wParam;
            // 查看是否出错
            if(WSAGETSELECTERROR(lParam))
            {
                ::closesocket(s);
                return 0;
            }
            // 处理发生的事件
            switch(WSAGETSELECTEVENT(lParam))
            {
            case FD_ACCEPT:        // 监听中的套接字检测到有连接进入
                {
                    SOCKET client = ::accept(s, NULL, NULL);
                    ::WSAAsyncSelect(client, hwnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE);
                }
                break;
            case FD_WRITE:
                {
                }
                break;
            case FD_READ:
                {
                    char szText[MAX_LEN] = { 0 };
                    if(::recv(s, szText, MAX_LEN, 0) == -1)
                        ::closesocket(s);
                    else
                        printf("接收数据:%s\n", szText);
                }
                break;
            case FD_CLOSE:
                { 
                    ::closesocket(s);
                }
                break;
            }
        }
        return 0;
    case WM_DESTROY:
        ::PostQuitMessage(0) ;
        return 0 ;
    }

    // 将我们不处理的消息交给系统做默认处理
    return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}
//创建窗口
int CreateWnd( HINSTANCE hInstance)
{
char szClassName[] = "MainWClass";    
    WNDCLASSEX wndclass;
    // 用描述主窗口的参数填充WNDCLASSEX结构
    wndclass.cbSize = sizeof(wndclass);    
    wndclass.style = CS_HREDRAW|CS_VREDRAW;    
    wndclass.lpfnWndProc = WinCallBack;    
    wndclass.cbClsExtra = 0;        
    wndclass.cbWndExtra = 0;        
    wndclass.hInstance = NULL;        
    wndclass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);    
    wndclass.hCursor = ::LoadCursor(NULL, IDC_ARROW);        
    wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);    
    wndclass.lpszMenuName = NULL;        
    wndclass.lpszClassName = "MainWClass";//szClassName ;
    wndclass.hIconSm = NULL;    
    if(RegisterClassEx(&wndclass) == FALSE)
return -1;


//create windows
  hwnd = ::CreateWindowEx( 
        0,                        
        szClassName,            
        TEXT(""),    
        WS_OVERLAPPEDWINDOW,    
        CW_USEDEFAULT,    
        CW_USEDEFAULT,        
        CW_USEDEFAULT,    
        CW_USEDEFAULT,            
        NULL,                
        NULL,        
        NULL,    
        NULL); 
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) 
{
return false;
}



if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return false; 
}
//2 创建套接字
s_Sever = socket(AF_INET, SOCK_STREAM, 0);
//if(s_Sever)
//绑定套接字
SOCKADDR_IN addrLocal;
addrLocal.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrLocal.sin_port = htons(6000);
addrLocal.sin_family = AF_INET;
err = bind(s_Sever,(SOCKADDR*)&addrLocal,sizeof(addrLocal));
if(err == SOCKET_ERROR)
{
return false;
}
err = WSAAsyncSelect(s_Sever, hwnd,WM_SOCKET,FD_ACCEPT|FD_CLOSE|FD_READ|FD_WRITE);
if (SOCKET_ERROR == err)
{
MessageBox(hwnd, "WsaAsyncSelect error","error", MB_OK);
}

//监听套接字
err = listen(s_Sever,5);
if(err == SOCKET_ERROR)
{
return false;
}
MSG msg;
while(GetMessage(&msg,hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

你可能感兴趣的:(WsaAsyncSelect服务器端代码)