Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320714
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: 系统运维

2011-01-18 05:07:25


#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>

#include "resolve.h"
#include "public.h"

DWORD WINAPI SendThread(LPVOID lpParam) {
    SOCKET s;

    char *buf = NULL;
    int buflen = 0;
    int i;
    int rc;
    int total = 0;

    s = (SOCKET)lpParam;

    buf = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BYTE) * gBufferSize);
    if (buf == NULL) {
        fprintf(stderr, "HeapAlloc failed.\n");
        ExitProcess(-1);
    }
    buflen = gBufferSize;

    memset(buf, '#', gBufferSize);

    if (gProtocol == IPPROTO_TCP) {
        for (i = 0; i < gSendCount; ++i) {
            int idx = 0;
            int nleft = buflen;

            while (nleft > 0) {
                rc = send(s, &buf[idx], nleft, 0);
                if (rc == SOCKET_ERROR) {
                    fprintf(stderr, "send failed.\n");
                    return -1;
                }

                nleft -= rc;
                idx += rc;

                total += rc;
            }
        }

        shutdown(s, SD_SEND);
    }

    printf("send %d\n", total);

    HeapFree(GetProcessHeap(), 0, buf);

    ExitThread(0);

    return 0;
}

DWORD WINAPI ReceiveThread(LPVOID lpParam) {
    SOCKET s;

    char *buf = NULL;
    int buflen = 0;
    int rc;
    int total = 0;

    s = (SOCKET)lpParam;

    buflen = gBufferSize;
    buf = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BYTE) * buflen);
    if (buf == NULL) {
        fprintf(stderr, "HeapAlloc failed.\n");
        ExitProcess(-1);
    }
    
    while (true) {
        if (gProtocol == IPPROTO_TCP) {
            rc = recv(s, buf, buflen, 0);
        } else {
            ExitProcess(-1);
        }

        if ((rc == SOCKET_ERROR) || (rc == 0)) {
            break;
        }

        total += rc;
    }

    printf("recv %d.\n", total);
    HeapFree(GetProcessHeap(), 0, buf);

    ExitThread(0);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    WSADATA wsd;
    SOCKET s;
    int rc;

    HANDLE hThreads[2];

    struct addrinfo *reslocal = NULL;
    struct addrinfo *resremote = NULL;
    struct addrinfo *ptr = NULL;

    ULONG startCount;
    
    if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) {
        fprintf(stderr, "Unable to load winsock.\n");
        return -1;
    }

    resremote = ResolveAddress(gSrvAddr, gPort, gAddressFamily, gSocketType, gProtocol);
    if (resremote == NULL) {
        fprintf(stderr, "resolve address failed.\n");
        return -1;
    }

    ptr = resremote;
    while (ptr) {
        reslocal = ResolveAddress(gClientAddr, "0", ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
        if (reslocal == NULL) {
            fprintf(stderr, "resolve local address failed.\n");
            return -1;
        }

        PrintAddress(ptr->ai_addr, ptr->ai_addrlen);
        printf("\n");

        s = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
        if (s == INVALID_SOCKET) {
            fprintf(stderr, "socket failed.\n");
            return -1;
        }        

        rc = bind(s, reslocal->ai_addr, reslocal->ai_addrlen);
        if (rc == SOCKET_ERROR) {
            fprintf(stderr, "bind failed.\n");
            return -1;
        }

        freeaddrinfo(reslocal);

        if (gProtocol == IPPROTO_TCP) {
            rc = connect(s, ptr->ai_addr, ptr->ai_addrlen);
            if (rc == SOCKET_ERROR) {
                printf("connect failed.\n");
                closesocket(s);
                s = INVALID_SOCKET;
            } else {
                break;
            }
        } else {
            return -1;
        }

        ptr = ptr->ai_next;
    }

    if (s == INVALID_SOCKET) {
        fprintf(stderr, "Unable to connect.\n");
        WSACleanup();
        return -1;
    }

    startCount = GetTickCount();

    hThreads[0] = CreateThread(NULL, 0, SendThread, (LPVOID)s, 0, NULL);
    if (hThreads[0] == NULL) {
        fprintf(stderr, "Create thread 0 failed.\n");
        return -1;
    }

    hThreads[1] = CreateThread(NULL, 0, ReceiveThread, (LPVOID)s, 0, NULL);
    if (hThreads[1] == NULL)
    {
        fprintf(stderr, "Create thread 1 failed.\n");
        return -1;
    }

    while (true) {
        rc = WaitForMultipleObjects(2, hThreads, TRUE, 5000);

        if (rc == WAIT_TIMEOUT) {
            printf("Wait time out.\n");
        } else if (rc == WAIT_FAILED) {
        } else {
            break;
        }
    }

    CloseHandle(hThreads[0]);
    CloseHandle(hThreads[1]);

    freeaddrinfo(resremote);
    closesocket(s);

    WSACleanup();
    return 0;
}


阅读(2555) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~