基于重叠I/O的服务器实践(完成例程)

服务端

#include "winsock2.h"

#pragma comment(lib, "ws2_32.lib")
#include
using namespace std;

#define MAXDATASIZE 200
typedef struct
{
    WSAOVERLAPPED overlap;
    WSABUF Buffer;
    char szMessage[MAXDATASIZE];
    DWORD NumberOfBytesRecvd;
    DWORD Flags;
    SOCKET sClient;
}RER_IO_OPERATION_DATA, *LPRER_IO_OPERATION_DATA;

SOCKET g_sNewClientConnection;
BOOL g_bNewConnectionArrived = FALSE;

void CALLBACK CompletionROUTINE(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlag)
{
    LPRER_IO_OPERATION_DATA lpPerIOData = (LPRER_IO_OPERATION_DATA)lpOverlapped;
    if (dwError != 0 || cbTransferred == 0)
    {
        closesocket(lpPerIOData->sClient);
        HeapFree(GetProcessHeap(), 0, lpPerIOData);
    }
    else
    {
        lpPerIOData->szMessage[cbTransferred] = '\0';
        send(lpPerIOData->sClient, lpPerIOData->szMessage, cbTransferred, 0);
        memset(&lpPerIOData->overlap, 0, sizeof(WSAOVERLAPPED));
        lpPerIOData->Buffer.len = MAXDATASIZE;
        lpPerIOData->Buffer.buf = lpPerIOData->szMessage;
        WSARecv(lpPerIOData->sClient, &lpPerIOData->Buffer, 1,
            &lpPerIOData->NumberOfBytesRecvd, &lpPerIOData->Flags,
            &lpPerIOData->overlap, CompletionROUTINE);
    }
}

DWORD WINAPI WorkerThread(LPVOID lpParam)
{
    LPRER_IO_OPERATION_DATA lpPerIoData = 0;
    while (TRUE)
    {
        if (g_bNewConnectionArrived)
        {
            lpPerIoData = (LPRER_IO_OPERATION_DATA)HeapAlloc(
                GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RER_IO_OPERATION_DATA));
            lpPerIoData->Buffer.len = MAXDATASIZE;
            lpPerIoData->Buffer.buf = lpPerIoData->szMessage;
            lpPerIoData->sClient = g_sNewClientConnection;
            WSARecv(lpPerIoData->sClient, &lpPerIoData->Buffer, 1,
                &lpPerIoData->NumberOfBytesRecvd, &lpPerIoData->Flags,
                &lpPerIoData->overlap, CompletionROUTINE);
            g_bNewConnectionArrived = FALSE;
        }
        SleepEx(1000, TRUE);
    }
    return 0;
}



int main()
{
    WSADATA wsadata;
    WSAStartup(0X0202, &wsadata);
    SOCKET sListen = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(27105);
    local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    bind(sListen, (SOCKADDR*)&local, sizeof(local));
    listen(sListen, 3);

    DWORD dwThreadId;
    CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId);
    struct sockaddr_in client;
    while (true)
    {
        g_sNewClientConnection = accept(sListen, (SOCKADDR*)&client, NULL);
        g_bNewConnectionArrived = TRUE;
        cout << inet_ntoa(client.sin_addr) << endl;
        cout << ntohs(client.sin_port) << endl;

    }

客户端

#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
#include
using namespace std;
#define BUF_SIZE 2000

int main()
{
    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR)
    {
        printf("WSAStartup() 无法初始化!\n");
        return -1;
    }

    SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET)
    {
        printf("socket() 出错!\n");
        WSACleanup();
        return -1;
    }

    struct sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_port = htons(27105);
    char cp[] = "127.0.0.1";
    clientService.sin_addr.S_un.S_addr = inet_addr(cp);

    if (connect(ConnectSocket, (sockaddr*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
    {
        printf("connect() 出错!\n");
        closesocket(ConnectSocket);
        WSACleanup(); system("pause");
        return -1;
    }

    int byteSent, byteRecv;
    char buf[BUF_SIZE];

    while (true)
    {

        printf("Input a string to send: ");
        cin >> buf;
        byteSent = send(ConnectSocket, buf, strlen(buf), 0);
        if (byteSent == SOCKET_ERROR)
        {
            printf("send() error!\n");
            closesocket(ConnectSocket);
            ::WSACleanup();
            return -1;
        }
        printf("send() successfully!\n");

        byteRecv = recv(ConnectSocket, buf, BUF_SIZE, 0);
        if (byteRecv == 0 || byteRecv == WSAECONNRESET)
        {
            printf("Connection Closed!\n");
            break;
        }

        buf[byteRecv] = '\0';
        printf("Recv from Server: %s\n", buf);
        if (strcmp(buf, "quit") == 0)
        {
            printf("quit!\n");
            break;
        }
    }

    closesocket(ConnectSocket);
    WSACleanup();
    system("pause");
    return 0;
}


    closesocket(sListen);
    WSACleanup();
    system("pause");
    return 0;
}

你可能感兴趣的:(网络通信)