| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 464 人关注过本帖
标题:socket 编程
取消只看楼主 加入收藏
bowengo
Rank: 2
等 级:论坛游民
帖 子:13
专家分:25
注 册:2010-5-23
结帖率:80%
收藏
 问题点数:0 回复次数:0 
socket 编程
这个程序为什么只能运行一次啊???不是循环打印吗?

#include "stdafx.h"
#include "windows.h"
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")

WSAEVENT hEvent[2];
DWORD WINAPI ThreadProc1(LPVOID);
DWORD WINAPI ThreadProc2(LPVOID);

int main(int argc, char* argv[])
{
    SOCKET sockApp;
    SOCKET sockIp;
    int len;
    int err;
    char buff[1024];
    DWORD dwEvent;
    SOCKADDR_IN addrApp;
    SOCKADDR_IN addrIp;

    // socket init

    WSADATA wsaData;
    WORD wsaVersion = MAKEWORD(2, 0);
    WSAStartup(wsaVersion, &wsaData);

    sockApp = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockApp == INVALID_SOCKET)
        return -1;

    sockIp = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockIp == INVALID_SOCKET)
        return -1;

    addrApp.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addrApp.sin_family = AF_INET;
    addrApp.sin_port = htons(5000);

    addrIp.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    addrIp.sin_family = AF_INET;
    addrIp.sin_port = htons(6000);

    err = bind(sockApp, (SOCKADDR*)&addrApp, sizeof(SOCKADDR));
    if (err == SOCKET_ERROR)
    {
        closesocket(sockApp);
        return -1;
    }

    err = bind(sockIp, (SOCKADDR*)&addrIp, sizeof(SOCKADDR));
    if (err == SOCKET_ERROR)
    {
        closesocket(sockApp);
        return -1;
    }

    hEvent[0] = WSACreateEvent();
    hEvent[1] = WSACreateEvent();
    if (hEvent[0] == WSA_INVALID_EVENT
        && hEvent[1] == WSA_INVALID_EVENT)
    {
        printf("Event create failed.");
        return -1;
    }

    WSAEventSelect(sockApp,hEvent[0],FD_READ);
    WSAEventSelect(sockIp,hEvent[1],FD_READ);

   

    while(true)
    {
        dwEvent = WSAWaitForMultipleEvents(2,hEvent,FALSE,WSA_INFINITE,FALSE);
        
        

        if (dwEvent == WSA_WAIT_EVENT_0)
        {
            printf("1");
            Sleep(500);
        }

        if (dwEvent == WSA_WAIT_EVENT_0 + 1)
        {
            printf("2");
            Sleep(500);
        }
   
        WSAResetEvent(hEvent[dwEvent - WSA_WAIT_EVENT_0]);
    }
    return 0;
}
搜索更多相关主题的帖子: socket 
2010-06-10 17:33
快速回复:socket 编程
数据加载中...
 
   



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

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