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;
}