• 大小: 39KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-05
  • 语言: 其他
  • 标签: socket  

资源简介

socket I/O WSAAsyncSelect模型示例源码。visual studio工程。

资源截图

代码片段和文件信息

//async_select_client.cpp: 定义应用程序的入口点。
//

#include “stdafx.h“
#include “async_select_client.h“

#include 
#include 
#include 

#pragma comment(lib “Ws2_32.lib“)

const std::string kIP = “127.0.0.1“;
const u_short kPort = 10001;


#define WUM_SOCKET (WM_USER+1)

HWND g_hwnd;

void TraceMsgA(const char *lpFormat ...) {
if (!lpFormat)
return;

char *pMsgBuffer = NULL;
unsigned int iMsgBufCount = 0;

va_list arglist;
va_start(arglist lpFormat);
HRESULT hr = STRSAFE_E_INSUFFICIENT_BUFFER;
while (hr == STRSAFE_E_INSUFFICIENT_BUFFER) {
iMsgBufCount += 1024;
if (pMsgBuffer) {
free(pMsgBuffer);
pMsgBuffer = NULL;
}
pMsgBuffer = (char*)malloc(iMsgBufCount * sizeof(char));
if (!pMsgBuffer) {
break;
}

hr = StringCchVPrintfA(pMsgBuffer iMsgBufCount lpFormat arglist);
}
va_end(arglist);
if (hr == S_OK) {
OutputDebugStringA(pMsgBuffer);
}

if (pMsgBuffer) {
free(pMsgBuffer);
pMsgBuffer = NULL;
}
}


LRESULT CALLBACK WndProc(HWND hWnd UINT message WPARAM wParam LPARAM lParam)
{
int err;
SOCKET s;
char buf[100] = { 0 };

switch (message) {
// (4)
case WUM_SOCKET:
if (WSAGETSELECTERROR(lParam)) {
closesocket((SOCKET)wParam);
TraceMsgA(“connect failed %d\n“ WSAGETSELECTERROR(lParam));
break;
}

switch (WSAGETSELECTEVENT(lParam))
{
case FD_CONNECT:
TraceMsgA(“connection to server\n“);
break;
case FD_READ:
err = recv((SOCKET)wParam buf 100 0);
if (err > 0) {
TraceMsgA(“recv: %s\n“ buf);
}
else if (err == 0) {
closesocket((SOCKET)wParam);
TraceMsgA(“connection closed\n“);
}
else {
closesocket((SOCKET)wParam);
TraceMsgA(“recv failed GLE: %d\n“ WSAGetLastError());
}
break;
case FD_WRITE:
break;
case FD_CLOSE:
closesocket((SOCKET)wParam);
TraceMsgA(“connection closed\n“);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd message wParam lParam);
}
return 0;
}


void InitWindow(HINSTANCE hInstance) {
WCHAR szWindowClass[100] = L“Test“;

WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.lpszClassName = szWindowClass;

RegisterClassExW(&wcex);

g_hwnd = CreateWindowW(szWindowClass L“client“ WS_OVERLAPPEDWINDOW
CW_USEDEFAULT 0 CW_USEDEFAULT 0 nullptr nullptr hInstance nullptr);

if (g_hwnd) {
ShowWindow(g_hwnd SW_SHOW);
UpdateWindow(g_hwnd);
}
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance
_In_opt_ HINSTANCE hPrevInstance
_In_ LPWSTR    lpCmdLine
_In_ int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

InitWindow(hInstance);

WSADATA wsaData;
WORD wVersionRequested = MAKEWORD

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-12 16:16  async_select_client\
     文件        1471  2017-12-12 11:52  async_select_client\async_select_client.sln
     目录           0  2017-12-12 16:16  async_select_client\async_select_client\
     文件        8016  2017-12-12 15:28  async_select_client\async_select_client\async_select_client.cpp
     文件          80  2017-12-12 11:52  async_select_client\async_select_client\async_select_client.h
     文件       46227  2017-11-20 10:10  async_select_client\async_select_client\async_select_client.ico
     文件        6714  2017-12-12 11:52  async_select_client\async_select_client\async_select_client.rc
     文件        8341  2017-12-12 15:04  async_select_client\async_select_client\async_select_client.vcxproj
     文件        1812  2017-12-12 11:52  async_select_client\async_select_client\async_select_client.vcxproj.filters
     文件         165  2017-12-12 15:03  async_select_client\async_select_client\async_select_client.vcxproj.user
     文件        1500  2017-12-12 11:52  async_select_client\async_select_client\resource.h
     文件       46227  2017-11-20 10:10  async_select_client\async_select_client\small.ico
     文件         340  2017-12-12 11:52  async_select_client\async_select_client\stdafx.cpp
     文件         690  2017-12-12 11:52  async_select_client\async_select_client\stdafx.h
     文件         370  2017-12-12 11:52  async_select_client\async_select_client\targetver.h
     目录           0  2017-12-12 16:16  async_select_srv\
     文件        1462  2017-12-12 11:27  async_select_srv\async_select_srv.sln
     目录           0  2017-12-12 16:16  async_select_srv\async_select_srv\
     文件        9528  2017-12-12 15:24  async_select_srv\async_select_srv\async_select_srv.cpp
     文件          80  2017-12-12 11:27  async_select_srv\async_select_srv\async_select_srv.h
     文件       46227  2017-11-20 10:10  async_select_srv\async_select_srv\async_select_srv.ico
     文件        6660  2017-12-12 11:27  async_select_srv\async_select_srv\async_select_srv.rc
     文件        8326  2017-12-12 14:37  async_select_srv\async_select_srv\async_select_srv.vcxproj
     文件        1800  2017-12-12 11:27  async_select_srv\async_select_srv\async_select_srv.vcxproj.filters
     文件        1476  2017-12-12 11:27  async_select_srv\async_select_srv\resource.h
     文件       46227  2017-11-20 10:10  async_select_srv\async_select_srv\small.ico
     文件         334  2017-12-12 11:27  async_select_srv\async_select_srv\stdafx.cpp
     文件         690  2017-12-12 11:27  async_select_srv\async_select_srv\stdafx.h
     文件         370  2017-12-12 11:27  async_select_srv\async_select_srv\targetver.h

评论

共有 条评论