An asynchronous socket was used in the recent project, using the completion port to do the E; Fully asynchronous implementations such as Accept,receive,send (multithreading)
Then there are multiple ports to use, and later think of the wrapper to complete the port socket, and then when the event is triggered callback function, there is no need to manually engage n multiple threads or something
If so, the test example is as follows:
#include "stdafx.h" #include <string.h> #include <stdlib.h> #include <stdio.h>//callback function Implementation Test # define __ callbacktestfun__/* callback Function * * The callback function implementation body must be a static method, and should use "__stdcall" declaration; You should accept the parameter conversion cost class pointer before you can manipulate members of this class * that is: static void __stdcall Fun (void* pthis,...) */#ifdef __callbacktestfun__typedef void (__stdcall*pondata) (void*,unsigned int,void*); typedef void (__stdcall* ponreccive) (void*,unsigned int); class Tsocketobj{private:pondata pondata,pp; Ponreccive ponreccive;void* tag;protected:void OnData () {Pondata (tag,123, "1234");} Public:int no;//unsigned char type; 0:tcp 1:udpunsigned int sock;void* Pdata;char name[21]; Tsocketobj *next,*prior; Tsocketobj () {memset (this,0,sizeof (Tsocketobj));} Initialization is also important tsocketobj (void* Obj) {memset (this,0,sizeof (tsocketobj)); tag=obj;printf ("\nobj:%x\n", tag);} void Setcallback (void* obj,pondata ondata,ponreccive onreccive) {tag=obj;pondata=ondata;ponreccive=onreccive;} void Setp (Pondata p) {pp=p;} void Go () {if (pondata) Pondata (Tag,sock,name), if (ponreccive) ponreccive (Tag,no), if (pP) pp (tag,sock,name);}}; Class Tsocket{tsocketobj socks;public:int m; Tsocket () {}tsocket (int n) {Set (n);} static void __stdcall OnData (void* pthis,unsigned int no,void* d) {printf ("\nthis is:%d sock:%d\tname:%s", ((tsocket*) pTh is)->m,no,d);} friend void __stdcall onreccive (void* pthis,unsigned int d) {printf ("\n\tno:%d\n", d);} This cannot be done with a callback function void __stdcall Ongo (void* pthis,unsigned int no,void* d) {printf ("\n\tongo m:%d\n", m);} void Set (int d) {m=1000+d;socks. no=d;socks.sock=100*d+78;sprintf (Socks.name, "sock_%d", d); socks. Setcallback (This, OnData, onreccive);//pondata p=& (This->ongo);//socks. SETP ((void*) &this->ongo);} void Show () {socks. Go ();}}; #endifint Main (int argc, char* argv[]) {int iarr[10]={1,5,3,2,7,4,9,6,8,0};//brdno//callback function implementation Test #ifdef __CALLBACKTESTFUN __tsocket tr;tr. Set (7); Tr.show (); Tsocket Ty;ty. Set (8); ty.show (); #endifscanf ("%d", Iarr); return 0;}
This is a complete example, reproduced please specify : http://www.cnblogs.com/lzpong/
Test code implemented by C + + callback function