学的比较匆忙,有错误请指出
//user define #include <WinSock2.h> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") #define BTN_1 0x01 #define WM_SOCKET WM_USER+1 #ifndef DEBUG_DISABLE #define DEBUG_ENTERED(M) MessageBox(NULL,\ M,\ "Tip",\ MB_OK); #else #define DEBUG_ENTERED(M) #endif // 全局变量: HINSTANCE hInst; // 当前实例 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 SOCKET sListen; FD_SET fdset; char writeBuf[100]; HWND hWin; // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); //初始化窗体 void CreateChildWindow(HWND hParent) { RECT rect; GetWindowRect(hParent,&rect); HWND hBtn1=CreateWindow("BUTTON", "Listen", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, (rect.right-rect.left)/2, (rect.bottom-rect.top)/2, 70,25, hParent, (HMENU)BTN_1, hInst, 0); } bool InitialSocket(SOCKET sock,u_short port,HWND hWnd) { SOCKADDR_IN serverAddr; serverAddr.sin_family=AF_INET; serverAddr.sin_port=htons(port); serverAddr.sin_addr.s_addr=htonl(ADDR_ANY); char buf[200]; if(bind(sock,PSOCKADDR(&serverAddr),sizeof(serverAddr))) { sprintf(buf,"bind error!error code:%d",WSAGetLastError()); DEBUG_ENTERED(buf) WSACleanup(); return false; } WSAAsyncSelect(sListen,hWnd,WM_SOCKET,FD_READ|FD_ACCEPT|FD_CLOSE); return true; } void OnLsBtnClicked() { char buf[200]; if(listen(sListen,5)) { sprintf(buf,"listen error!error code:%d\n",WSAGetLastError()); DEBUG_ENTERED(buf) WSACleanup(); } else { sprintf(buf,"Now Listen"); DEBUG_ENTERED(buf) } } void HandleSocket(WPARAM wParam,LPARAM lParam) { if(WSAGETSELECTERROR(lParam)) { //有错误 closesocket((SOCKET)wParam); return; } switch(WSAGETSELECTEVENT(lParam)) { case FD_ACCEPT: { SOCKET newSocket=accept((SOCKET)wParam,NULL,NULL); if(newSocket==INVALID_SOCKET) { DEBUG_ENTERED("INVALID SOCKET!"); return; } FD_SET(newSocket,&fdset); sprintf(writeBuf,"New connection!"); UpdateWindow(hWin); } break; case FD_READ: recv((SOCKET)wParam,writeBuf,sizeof(writeBuf),0); //UpdateWindow(hWin); SendMessage(hWin,WM_PAINT,0,0); break; case FD_CLOSE: sprintf(writeBuf,"Connection closed!"); UpdateWindow(hWin); break; } } //程序入口 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_WSAASYNCSELECT, szWindowClass, MAX_LOADSTRING); FD_ZERO(&fdset); ZeroMemory(writeBuf,sizeof(writeBuf)); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WSAASYNCSELECT); // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // // 注释: // // 仅当希望在已添加到 Windows 95 的 // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时, // 才需要此函数及其用法。调用此函数 // 十分重要,这样应用程序就可以获得关联的 // “格式正确的”小图标。 // 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_WSAASYNCSELECT); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_WSAASYNCSELECT; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // 函数: InitInstance(HANDLE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); hWin=hWnd; //Socket初始化 WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); sListen=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(InitialSocket(sListen,25418,hWnd)) { MessageBox(NULL, "Socket初始化成功", "Tip", MB_OK|MB_ICONINFORMATION); FD_SET(sListen,&fdset); } return TRUE; } // // 函数: WndProc(HWND, unsigned, WORD, LONG) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; RECT rect; GetWindowRect(hWnd,&rect); switch (message) { case WM_CREATE: CreateChildWindow(hWnd); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; case BTN_1: switch(wmEvent) { case BN_CLICKED: OnLsBtnClicked(); break; } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: 在此添加任意绘图代码... DrawText(hdc,writeBuf,-1,&rect,DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: WSACleanup(); PostQuitMessage(0); break; case WM_SOCKET: HandleSocket(wParam,lParam); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “关于”框的消息处理程序。 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; }