• 大小: 13.36MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-14
  • 语言: 其他
  • 标签: windows  socket  I/O模型  

资源简介

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\link.command.1.tlog
     文件        2748  2015-07-19 19:03  7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\link.read.1.tlog
     文件         836  2015-07-19 19:03  7-IoModel-WinSocket\TCPModel\CompletionSev\Debug\CompletionServ.tlog\link.write.1.tlog
     文件          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个文件信息

评论

共有 条评论