首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

大家救命:WSAAsyncEvent的具体用法解决方法

2012-01-06 
大家救命:WSAAsyncEvent的具体用法因为用cgi写的东西,所以必须要控制台的程序。WSAAsyncSelect就不能用了,

大家救命:WSAAsyncEvent的具体用法
因为用cgi写的东西,所以必须要控制台的程序。WSAAsyncSelect就不能用了,刚才在网上查了下,可以用WSAAsyncEvent模式。但是竟找不到任何的具体使用方法...
    请哪位用过的或者有相关资料的兄弟姐妹帮忙下       不胜感激
    使用方法,示例代码都可以       谢谢了

[解决办法]
怎么不可以用,可以用啊.在main里边先创建一个窗口,然后启动消息循环就可以了。

《windows网络编程技术》第8章里有示例代码
[解决办法]
// Module Name: eventselect.cpp
//
// Description:
//
// This sample illustrates how to develop a simple echo server Winsock
// application using the WSAEventSelect() I/O model. This sample is
// implemented as a console-style application and simply prints
// messages when connections are established and removed from the server.
// The application listens for TCP connections on port 5150 and accepts them
// as they arrive. When this application receives data from a client, it
// simply echos (this is why we call it an echo server) the data back in
// it 's original form until the client closes the connection.
//
// Compile:
//
// cl -o eventselect eventselect.cpp ws2_32.lib
//
// Command Line Options:
//
// eventselect.exe
//
// Note: There are no command line options for this sample.

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>

#define PORT 5150
#define DATA_BUFSIZE 8192

typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;

BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Event);

DWORD EventTotal = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS];

void main(void)
{
SOCKET Listen;
SOCKET Accept;
SOCKADDR_IN InternetAddr;
DWORD Event;
WSANETWORKEVENTS NetworkEvents;
WSADATA wsaData;
DWORD Ret;
DWORD Flags;
DWORD RecvBytes;
DWORD SendBytes;

if ((Ret = WSAStartup(0x0202, &wsaData)) != 0)
{
printf( "WSAStartup() failed with error %d\n ", Ret);
return;
}

if ((Listen = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf( "socket() failed with error %d\n ", WSAGetLastError());
return;
}

CreateSocketInformation(Listen);

if (WSAEventSelect(Listen, EventArray[EventTotal - 1], FD_ACCEPT|FD_CLOSE) == SOCKET_ERROR)
{
printf( "WSAEventSelect() failed with error %d\n ", WSAGetLastError());
return;
}

InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);

if (bind(Listen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf( "bind() failed with error %d\n ", WSAGetLastError());
return;
}

if (listen(Listen, 5))
{
printf( "listen() failed with error %d\n ", WSAGetLastError());
return;
}


热点排行