windows io完成埠例項

2021-10-03 07:52:12 字數 3548 閱讀 5799

#pragma once

#include

#pragma comment(lib,"ws2_32.lib")

#define data_bufsize 512

enum

;typedef

struct io_operation_data

io_operation_data;

class

iocp

;

#include

"iocp.h"

#include

#include

std::list> iocplist;

handle hcomport =

null;

iocp::

iocp

(socket s, sockaddr_in sin)

iocp::

~iocp()

bool iocp::

recvasyndata()

}return true;}

bool iocp::

sendasyndata()

}return true;

}void

eraseiocp

(iocp* iocp)}}

unsigned

int __stdcall service_work_thread

(void

* context)if(

!ret)

elseif(

null

else

}else

io_operation_data* pio =

containing_record

;switch

(pio-

>type)

break

;default

:break;}

}}return0;

}int

createiocompletionport

(int port)

getsysteminfo

(&systeminfo)

; dword cpunum = systeminfo.dwnumberofprocessors;

//執行緒池

for(

unsigned

int i =

0; i < cpunum *2;

++i)

}int ret =0;

wsadata wsadata;if(

(ret =

wsastartup

(0x0202

,&wsadata))!=

0)socket slisten = invalid_socket;if(

(slisten =

wsasocket

(af_inet, sock_stream,0,

null,0

)== invalid_socket)

sockaddr_in sin;

sin.sin_family = af_inet;

sin.sin_addr.s_un.s_addr =

htonl

(inaddr_any)

; sin.sin_port =

htons

(port);if

(bind

(slisten,

(sockaddr*

)&sin,

sizeof

(sin)

)== socket_error)if(

listen

(slisten, somaxconn)

) socket saccept = invalid_socket;

sockaddr_in saddr_in;

int length =

sizeof

(saddr_in)

;int total =0;

while

(true

)++total;

if(total %

10==0)

iocp* iocp =

newiocp

(saccept, saddr_in)

; iocplist.

push_back

(iocp)

;//繫結完成埠if(

createiocompletionport

((handle)saccept, hcomport,

(dword)iocp,0)

==null)if

(!iocp-

>

recvasyndata()

)}//銷毀執行緒池

for(

unsigned

int i =

0; i < cpunum *2;

++i)

waitformultipleobjects

(cpunum *

2, hthreadhandle, true, infinite)

;return0;

}int

main

(int argc,

char

* ar**)

#include

#pragma comment(lib,"ws2_32.lib")

#include /*

date |change

-----------------------------------------

2017-7-31 |socket tcp測試客戶端

*/void

tcp_client()

char buffer[

1024];

sprintf_s

(buffer,

1024

,"hi client msg.(%d)"

, sclient)

;//sleep(1000);

int ret =

send

(sclient, buffer,

strlen

(buffer),0

);ret =

recv

(sclient, buffer,

sizeof

(buffer),0

);buffer[ret]

='\0'

;//printf("%s\n", buffer);

//closesocket(sclient);

}int

main

(int argc,

char

* ar**)

wsacleanup()

;return0;

}

關鍵函式

createiocompletionport

getqueuedcompletionstatus

postqueuedcompletionstatus

這就好比自旋鎖比互斥鎖的效率高一樣,前者不會進入睡眠,後者會

c cli 之非同步Socket完成埠例項

net 3.5的socket非同步完成埠 當時是c 寫的,主要是為了實際專案應用,一段時間過去後在使用過程中發現了一些小問題,並按照上級要求更改為c cli的 重新修改並再次發布,希望給c cli初學者一點點參考。如發現程式中問題希望告知於我!程式主要功能 1,使用.net 3.5 socketas...

完成埠實現echo tcp server

include stdio.h include winsock2.h pragma comment lib,ws2 32.lib define port 5150 define data bufsize 8192 typedef struct char buffer data bufsize int...

I O 完成埠實現

這篇文章是繼承上篇 windows 同步裝置 i o 與非同步裝置 i o 未讀過的讀者可以去看看再來看這篇文章哈。建立新的i o完成埠 i o完成可能是最複雜的windows核心物件了,為了建立乙個i o完成端 們需要呼叫createiocompletionport函式 handle winapi...