C++ 技术中心

   :: 首页 :: 联系 ::  :: 管理
  160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

公告

郑重声明:本BLOG所发表的原创文章,作者保留一切权利。必须经过作者本人同意后方可转载,并注名作者(天空)和出处(CppBlog.com)。作者Email:coder@luckcoder.com

留言簿(27)

搜索

  •  

最新随笔

最新评论

评论排行榜

1. 服务器端代码:


#include 
"stdafx.h"

#include 
<WINSOCK2.H>
#include 
<stdio.h>

#define PORT    5150
#define MSGSIZE 1024

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

typedef 
enum
{
    RECV_POSTED
}
OPERATION_TYPE;

typedef 
struct
{
    WSAOVERLAPPED  overlap;
    WSABUF         Buffer;
    
char           szMessage[MSGSIZE];
    DWORD          NumberOfBytesRecvd;
    DWORD          Flags;
    OPERATION_TYPE OperationType;
}
PER_IO_OPERATION_DATA, *LPPER_IO_OPERATION_DATA;

DWORD WINAPI WorkerThread(LPVOID);

int main()
{
    WSADATA                 wsaData;
    SOCKET                  sListen, sClient;
    SOCKADDR_IN             local, client;
    DWORD                   i, dwThreadId;
    
int                     iaddrSize = sizeof(SOCKADDR_IN);
    HANDLE                  CompletionPort 
= INVALID_HANDLE_VALUE;
    SYSTEM_INFO             systeminfo;
    LPPER_IO_OPERATION_DATA lpPerIOData 
= NULL;

    
// Initialize Windows Socket library
    WSAStartup(0x0202&wsaData);

    
// Create completion port
    CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 00);

    
// Create worker thread
    GetSystemInfo(&systeminfo);
    
for (i = 0; i < systeminfo.dwNumberOfProcessors; i++)
    
{
        CreateThread(NULL, 
0, WorkerThread, CompletionPort, 0&dwThreadId);
    }


    
// Create listening socket
    sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    
// Bind
    local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    local.sin_family 
= AF_INET;
    local.sin_port 
= htons(PORT);
    bind(sListen, (
struct sockaddr *)&local, sizeof(SOCKADDR_IN));

    
// Listen
    listen(sListen, 3);

    
while (TRUE)
    
{
        
// Accept a connection
        sClient = accept(sListen, (struct sockaddr *)&client, &iaddrSize);
        printf(
"Accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));

        
// Associate the newly arrived client socket with completion port
        CreateIoCompletionPort((HANDLE)sClient, CompletionPort, (DWORD)sClient, 0);

        
// Launch an asynchronous operation for new arrived connection
        lpPerIOData = (LPPER_IO_OPERATION_DATA)HeapAlloc(
            GetProcessHeap(),
            HEAP_ZERO_MEMORY,
            
sizeof(PER_IO_OPERATION_DATA));
        lpPerIOData
->Buffer.len = MSGSIZE;
        lpPerIOData
->Buffer.buf = lpPerIOData->szMessage;
        lpPerIOData
->OperationType = RECV_POSTED;
        WSARecv(sClient,
            
&lpPerIOData->Buffer,
            
1,
            
&lpPerIOData->NumberOfBytesRecvd,
            
&lpPerIOData->Flags,
            
&lpPerIOData->overlap,
            NULL);
    }


    PostQueuedCompletionStatus(CompletionPort, 
0xFFFFFFFF0, NULL);
    CloseHandle(CompletionPort);
    closesocket(sListen);
    WSACleanup();
    
return 0;
}


DWORD WINAPI WorkerThread(LPVOID CompletionPortID)
{
    HANDLE                  CompletionPort
=(HANDLE)CompletionPortID;
    DWORD                   dwBytesTransferred;
    SOCKET                  sClient;
    LPPER_IO_OPERATION_DATA lpPerIOData 
= NULL;

    
while (TRUE)
    
{
        GetQueuedCompletionStatus(
            CompletionPort,
            
&dwBytesTransferred,
            (PULONG_PTR)
&sClient,
            (LPOVERLAPPED 
*)&lpPerIOData,
            INFINITE);
        
if (dwBytesTransferred == 0xFFFFFFFF)
        
{
            
return 0;
        }


        
if (lpPerIOData->OperationType == RECV_POSTED)
        
{
            
if (dwBytesTransferred == 0)
            
{
                
// Connection was closed by client
                closesocket(sClient);
                HeapFree(GetProcessHeap(), 
0, lpPerIOData);        
            }

            
else
            
{
                lpPerIOData
->szMessage[dwBytesTransferred] = '\0';
                send(sClient, lpPerIOData
->szMessage, dwBytesTransferred, 0);

                
// Launch another asynchronous operation for sClient
                memset(lpPerIOData, 0sizeof(PER_IO_OPERATION_DATA));
                lpPerIOData
->Buffer.len = MSGSIZE;
                lpPerIOData
->Buffer.buf = lpPerIOData->szMessage;
                lpPerIOData
->OperationType = RECV_POSTED;
                WSARecv(sClient,
                    
&lpPerIOData->Buffer,
                    
1,
                    
&lpPerIOData->NumberOfBytesRecvd,
                    
&lpPerIOData->Flags,
                    
&lpPerIOData->overlap,
                    NULL);
            }

        }

    }

    
return 0;
}



2. 客户端代码片断:
void CTestClientDlg::OnBnClickedOk()
{
    
// TODO: 在此添加控件通知处理程序代码
    
//CDialogEx::OnOK();

    UpdateData(TRUE);

    
byte b1,b2,b3,b4;
    
char strIpAddress[20]={0};
    m_IpCtrl.GetAddress(b1,b2,b3,b4);
    sprintf(strIpAddress,
"%d.%d.%d.%d",b1,b2,b3,b4);
    
    TCHAR buffer[
1024];
    sprintf(buffer,_T(
"%d"),m_port);

    SOCKET sockClient
=socket(AF_INET,SOCK_STREAM,0);
    SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr
=inet_addr(strIpAddress);
    addrSrv.sin_family
=AF_INET;
    addrSrv.sin_port
=htons((UINT)m_port);

    
//向服务器发出连接请求
    connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));


    
char recvBuf[100];
    
//接收数据

    send(sockClient,
"This is lisi",strlen("This is lisi")+1,0);

    recv(sockClient,recvBuf,
100,0);
    printf(
"%s\n",recvBuf);

    
//关闭套接字
    closesocket(sockClient);
}
    
posted on 2011-03-01 11:45 C++技术中心 阅读(2918) 评论(2)  编辑 收藏 引用 所属分类: Windows 网络编程

Feedback

# re: 完成端口示例 2011-03-01 22:56 liquanhai
想问问楼主的程序是怎么试的,我弄了一下没有成功,想请教一下?
  回复  更多评论
  

# re: 完成端口示例 2011-03-02 09:01 C++技术中心
@liquanhai
复制到VC里就行了.已经在VC2010中编译通过,复制到blog上来的.
客户端是代码片段,需要自已调整一下代码.  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理