WSAAccept与WSAAccept的ConditionAcceptFunction

来源:互联网 发布:淘宝直播间入口 编辑:程序博客网 时间:2024/05/17 03:23
#include <winsock2.h>#include <stdio.h>#include <windows.h>#pragma comment(lib,"ws2_32.lib")/* Define an example conditional function that depends on the pQos field *//*WSAAccept回调函数ConditionAcceptFunc*/int CALLBACK ConditionAcceptFunc(    LPWSABUF lpCallerId,//the address of connected entity  客户端的地址    LPWSABUF lpCallerData,//    LPQOS pQos,//服务质量    LPQOS lpGQOS,//组服务质量,deserved,一般为NULL    LPWSABUF lpCalleeId,    LPWSABUF lpCalleeData,    GROUP FAR * g,    DWORD_PTR dwCallbackData//重WSAAccept传过来的数据    ){    if (pQos != NULL) {        RtlZeroMemory(pQos, sizeof(QOS));        return CF_ACCEPT;//网络事件    } else        return CF_REJECT;} int main() {    /* Declare and initialize variables */    WSADATA wsaData;    SOCKET ListenSocket, AcceptSocket;    struct sockaddr_in saClient;    int iClientSize = sizeof(saClient);    u_short port = 10000;    char* ip;    sockaddr_in service;    int error;    /* Initialize Winsock */    error = WSAStartup(MAKEWORD(2,2), &wsaData);    if (error) {        printf("WSAStartup() failed with error: %d\n", error);        return 1;    }    /* Create a TCP listening socket */    ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);    if (ListenSocket == INVALID_SOCKET) {          printf("socket() failed with error: %d\n", WSAGetLastError() );        WSACleanup();        return 1;    }    /*-----------------------------------------       *  Set up the sock addr structure that the listening socket     *  will be bound to. In this case, the structure holds the  local IP address and the port specified.     */    service.sin_family = AF_INET;    service.sin_port = htons(port);    hostent* thisHost;    thisHost = gethostbyname("");    ip = inet_ntoa (*(struct in_addr *)*thisHost->h_addr_list);    service.sin_addr.s_addr = inet_addr(ip);    /*-----------------------------------------     *  Bind the listening socket to the IP address.  and port number specified by the sockaddr structure.     */    error = bind(ListenSocket, (SOCKADDR *) &service, sizeof(SOCKADDR));    if (error == SOCKET_ERROR) {          printf("bind() failed with error: %d\n", WSAGetLastError() );        closesocket(ListenSocket);ListenSocket = INVALID_SOCKET ;        WSACleanup();        return 1;    }       /* Make the socket listen for incoming connection requests */    error = listen(ListenSocket, 1);    if (error == SOCKET_ERROR) {          printf("listen() failed with error: %d\n", WSAGetLastError() );        closesocket(ListenSocket);ListenSocket = INVALID_SOCKET ;        WSACleanup();        return 1;    }    printf("Listening...\n");       /*-----------------------------------------     *  Accept an incoming connnection request on the     *  listening socket and transfer control to the  accepting socket.     */     AcceptSocket = WSAAccept(ListenSocket, (SOCKADDR*) &saClient, &iClientSize, //WSAAccept函数,使用非Overlapped        &ConditionAcceptFunc, NULL);if(AcceptSocket == INVALID_SOCKET){printf("the accept socket is errror:%d",GetLastError());closesocket(AcceptSocket) ;AcceptSocket = INVALID_SOCKET ;closesocket(ListenSocket) ;ListenSocket = INVALID_SOCKET ;WSACleanup() ;return -1 ;}printf("one client is connected!\n");SOCKADDR_IN clientAddr ;int len = sizeof(clientAddr);ZeroMemory(&clientAddr,len);getpeername(AcceptSocket,(SOCKADDR*)&clientAddr,&len);printf("the client address: %s port :\n",inet_ntoa(clientAddr.sin_addr),ntohs(clientAddr.sin_port)) ;char buff[1000] ;gethostname(buff,1000) ;printf("the host address :%s\n",buff) ;    /*  Now do some work with the AcceptSocket      *  At this point, the application could     *  handle data transfer on the socket, or other socket  functionality./     */     Sleep(INFINITE);    /* Then clean up and quit */    closesocket(AcceptSocket);AcceptSocket = INVALID_SOCKET ;    closesocket(ListenSocket);ListenSocket = INVALID_SOCKET ;    WSACleanup();    return 0;}

0 0
原创粉丝点击