| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 608 人关注过本帖
标题:写了个最简单的完成端口服务端但是为什么收不到消息?
只看楼主 加入收藏
wxf041041
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-11-1
结帖率:100%
收藏
 问题点数:0 回复次数:0 
写了个最简单的完成端口服务端但是为什么收不到消息?
// IOCP.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")

struct handleData
{
    SOCKET clientsocket;
    SOCKADDR_IN clientaddr;
    int nOperatorType;
};

struct iOData
{
    OVERLAPPED theoverlapped;
    WSABUF buff;
    char recvbuff[MAXBYTE];
};

void InitWinSock()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
   
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
        return;
    }
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        WSACleanup( );
        return;
    }
}

int  GetLocalMoShineInfo(SYSTEM_INFO& sysinfo)
{
    GetSystemInfo(&sysinfo);
    int nCPU = sysinfo.dwNumberOfProcessors;
    return nCPU;
}


DWORD WINAPI threadfun(LPVOID lparam)
{
    bool bRet;
    DWORD dwNumRead;
    handleData handledata;
    iOData         overlappdata;
    HANDLE hCompletion = (HANDLE)lparam;
    LPOVERLAPPED lpOver = NULL;
    while (TRUE)
    {
        bRet = GetQueuedCompletionStatus(hCompletion,&dwNumRead,(LPDWORD)&handledata,(LPOVERLAPPED*)&overlappdata,INFINITE);
        if (bRet==0&&lpOver!=NULL)
        {
            //closesocket(handledata.clientsocket);
            printf("&Egrave;&iexcl;°ü&Ecirc;§°&Uuml;!");
        }
        else if (bRet!=0)
        {
            printf("&Egrave;&iexcl;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;°ü!");
        }
    }
    return 0;
}

int main(int argc, char* argv[])
{
    InitWinSock();
    SOCKET Listens = socket(AF_INET,SOCK_STREAM,0);

    if (Listens==INVALID_SOCKET)
    {
        printf("&acute;&acute;&frac12;¨&Igrave;×&frac12;&Oacute;×&Ouml;&Ecirc;§°&Uuml;!");
        return 0;
    }

    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(7000);
    addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

    if (bind(Listens,(sockaddr*)&addr,sizeof(addr)))
    {
        printf("°ó&para;¨&Ecirc;§°&Uuml;!");
        return 0;
    }
    HANDLE hIoCptport = CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,0);
    if (hIoCptport==NULL)
    {
        printf("&acute;&acute;&frac12;¨IO&Iacute;ê&sup3;&Eacute;&para;&Euml;&iquest;&Uacute;&Ecirc;§°&Uuml;!");
        return 0;
    }
    SYSTEM_INFO sysinfo;
    int nCPU = GetLocalMoShineInfo(sysinfo);
    int nThread = nCPU*2+2;
    for (int i = 0;i<nThread;i++)
    {
        HANDLE handle = CreateThread(NULL,0,threadfun,(LPVOID)hIoCptport,0,0);
        if ( handle )
        {
            CloseHandle(handle);
        }
    }
    listen(Listens,SOMAXCONN);

    printf("·&thorn;&Icirc;&ntilde;&AElig;÷&AElig;&ocirc;&para;&macr;,&iquest;&ordf;&Ecirc;&frac14;&frac14;à&Igrave;&yacute;&iexcl;¤&iexcl;¤&iexcl;¤&iexcl;¤&iexcl;¤&iexcl;¤\r\n");

    while (TRUE)
    {
        struct handleData* pKey = NULL;
        sockaddr_in clientaddr;
        int clientaddlength = sizeof(clientaddr);
        SOCKET clientsock = accept(Listens,(sockaddr*)&clientaddr,&clientaddlength);

        printf("&Oacute;&ETH;&Egrave;&Euml;&frac12;&oslash;&Agrave;&acute;&Aacute;&Euml;\r\n");
        if (clientsock==INVALID_SOCKET)
        {
            printf("&frac12;&Oacute;&Ecirc;&Otilde;&Ecirc;§°&Uuml;!");
            return 0;
        }
        pKey = new handleData();

        if ( pKey )
        {
            pKey->clientsocket = clientsock;

            pKey->clientaddr = clientaddr;

            //pKey->nOperatorType = OP_READ;
        }

        CreateIoCompletionPort((HANDLE)(pKey->clientsocket),hIoCptport,(DWORD)pKey,0);
        iOData* poverlapped = new iOData();

        if (poverlapped)
        {
            poverlapped->buff.buf  = poverlapped->recvbuff;
            poverlapped->buff.len = MAXBYTE;
        }
        DWORD dwRecv = 0;
        DWORD dwFlag = 0;

        WSARecv(pKey->clientsocket,&(poverlapped->buff),1,&dwRecv,&dwFlag,&(poverlapped->theoverlapped),NULL);

        int nError = GetLastError();
    }
    return 0;
}
搜索更多相关主题的帖子: void comment include 服务端 
2012-06-22 00:40
快速回复:写了个最简单的完成端口服务端但是为什么收不到消息?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043575 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved