资源简介
windows下7种winsocket的I/O编程模型的源代码,均为可直接运行代码,有详细注释。
每一种I/O模型的详细编程步骤可以参考配套文章:http://blog.csdn.net/u011787119/article/details/46848391
代码片段和文件信息
// 完成端口模型,服务器
#include
#include
#define DEFAULT_BUF_LEN 1024
#define DEFAULT_PORT 8086
#pragma comment(lib “ws2_32.lib“)
typedef enum // 操作类型
{
OP_RECV
OP_SEND
}OPERATION_TYPE;
typedef struct // 自定义单I/O操作相关数据
{
WSAOVERLAPPED overlap;
WSABUF Buffer;
char szMessage[DEFAULT_BUF_LEN];
DWORD NumberOfBytesRecvd;
DWORD NumberOfBytesSend;
DWORD Flags;
OPERATION_TYPE OperationType;
}PER_IO_DATA *LPPER_IO_DATA;
typedef struct // 自定义单句柄数据
{
SOCKET Socket;
}PER_HANDLE_DATA *LPPER_HANDLE_DATA;
DWORD WINAPI WorkerThread(LPVOID);
int main()
{
// Initialize Windows Socket library
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2 2) &wsaData);
if (iResult != 0)
{
printf(“WSAStartup failed with error: %d“ iResult);
return 1;
}
// Create listening socket,using WSASocket()
SOCKET ServSocket = INVALID_SOCKET;
ServSocket = WSASocket(AF_INET SOCK_STREAM IPPROTO_IP NULL 0 WSA_FLAG_OVERLAPPED);
if (ServSocket == INVALID_SOCKET)
{
printf(“socket failed with error: %d“ WSAGetLastError());
WSACleanup();
return 1;
}
// Bind
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_addr.S_un.S_addr = INADDR_ANY;
addrServ.sin_port = htons(DEFAULT_PORT);
iResult = bind(ServSocket (SOCKADDR*)&addrServ sizeof(SOCKADDR_IN));
if (iResult == SOCKET_ERROR)
{
printf(“bind faield with error: %d\n“ WSAGetLastError());
closesocket(ServSocket);
WSACleanup();
return 1;
}
// Listen
iResult = listen(ServSocket SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
printf(“listen faield with error: %d\n“ WSAGetLastError());
closesocket(ServSocket);
WSACleanup();
return 1;
}
printf(“server(CompletionServ) is listening ......\n“);
// Create completion port
HANDLE CompletionPort = NULL;
CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE NULL 0 0);
if (CompletionPort == NULL)
{
printf(“CreateIoCompletionPort failed\n“);
return 1;
}
// Create worker thread by the numbers of cpu
DWORD dwThreadId;
SYSTEM_INFO systeminfo;
GetSystemInfo(&systeminfo);
for (int i = 0; i < systeminfo.dwNumberOfProcessors; i++)
{
HANDLE ThreadHandle;
ThreadHandle = CreateThread(NULL 0 WorkerThread CompletionPort 0 &dwThreadId);
if (ThreadHandle == NULL)
{
printf(“CreateThread failed with error %d\n“ GetLastError());
return 1;
}
CloseHandle(ThreadHandle);
}
// 处理客户端的连接请求
while (TRUE)
{
// Accept a connection
SOCKET AcceSocket = INVALID_SOCKET;
SOCKADDR_IN addrClient;
int addrClienLen = sizeof(SOCKADDR_IN);
AcceSocket = accept(ServSocket (SOCKADDR*)&addrClient &addrClienLen);
if (AcceSocket == INVALID_SOCKET)
{
printf(“accept failed with error: %d\n“ WSAGetLastError());
break;
}
printf(“a cilent has connect successful its ip: %s\n“ inet_ntoa(addrClient.si
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-07-19 19:07 7-IoModel-WinSocket\
目录 0 2015-07-19 19:06 7-IoModel-WinSocket\TCPModel\
目录 0 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\
文件 4151 2015-07-19 11:41 7-IoModel-WinSocket\TCPModel\CompletionSev\CompletionSev.vcxproj
文件 942 2015-07-12 09:40 7-IoModel-WinSocket\TCPModel\CompletionSev\CompletionSev.vcxproj.filters
目录 0 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\
文件 1981 2015-07-18 13:04 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.Build.CppClean.log
目录 0 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\
文件 874 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\cl.command.1.tlog
文件 19476 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\CL.read.1.tlog
文件 852 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\CL.write.1.tlog
文件 210 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\CompletionServ.lastbuildstate
文件 1528 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\li
文件 2748 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\li
文件 836 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\li
文件 99 2014-08-09 23:02 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionSev.lastbuildstate
文件 1885 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionSev.log
文件 44382 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\main.obj
文件 527360 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\vc120.idb
文件 176128 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\vc120.pdb
文件 7382 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\CompletionSev\main.cpp
目录 0 2015-07-18 13:04 7-IoModel-WinSocket\TCPModel\Debug\
文件 32768 2015-07-18 13:04 7-IoModel-WinSocket\TCPModel\Debug\ASockClien.exe
文件 265156 2015-07-18 13:04 7-IoModel-WinSocket\TCPModel\Debug\ASockClien.ilk
文件 486400 2015-07-18 13:04 7-IoModel-WinSocket\TCPModel\Debug\ASockClien.pdb
文件 33792 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\Debug\CompletionServ.exe
文件 327940 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\Debug\CompletionServ.ilk
文件 601088 2015-07-19 19:03 7-IoModel-WinSocket\TCPModel\Debug\CompletionServ.pdb
文件 33792 2014-08-09 23:02 7-IoModel-WinSocket\TCPModel\Debug\CompletionSev.exe
文件 266356 2014-08-09 23:02 7-IoModel-WinSocket\TCPModel\Debug\CompletionSev.ilk
文件 461824 2014-08-09 23:02 7-IoModel-WinSocket\TCPModel\Debug\CompletionSev.pdb
............此处省略164个文件信息
相关资源
- Qt5.6.1环境下基于Windows API的按键精灵
- cmake-3.10.1-win64-x64.msi
- BNC-2.11.1-Windows
- windows版本glade安装包
- HelixServerUnlimite(Helix server 11 for Wind
- windows程序设计王艳平第二版随书光盘
- Dell R420、R520、R620、R720、T420、T620安装
- openssl-1.1.1库win32-64
- RSA签名验签工具windows_V1.4
- V8 windows版本开发库(Debug)
- Quectel_LTE_Windows_USB_Driver_V2.1.6.zip
- .net websocket简单
- cef_binary_3.2623.1395.g3034273_windows32
- WRK以及相关的分析工具.7z
- jdk-8u151-windows-x64 jdk1.8_151
- opencv-4.0.0-windows版本
- OpenSSL 1.1.1a for Windows32位、64位,已编
- 《Windows内核安全与驱动开发》随书光
- NetLogo-6.1.1-64.msi
- mongodb-compass-1.20.5-win32-x64.zip
- ArduPilot-Arduino-1.0.3-windows
- jre-8u241-windows-x64.exe
- Gephi-0.9.1 windows版本
- 《Windows驱动开发技术详解》完整清晰
- windows下Qt+tesseract4.1工程与tesseract4.1所
- elasticsearch-6.3.2windows安装包.rar
- Clash-Windows.7z
- windbg (Debugging Tools for Windows)
- ActiveSync windows xp、7、8包含4.5、6.0、
- windows 访问LINUX 分区ExtFS for Windows 4.
评论
共有 条评论