WSAEnumNetworkEvents Function

来源:互联网 发布:产品样本制作软件 编辑:程序博客网 时间:2024/06/05 01:59
WSAEnumNetworkEvents Function  


The WSAEnumNetworkEvents function discovers occurrences of network events for the indicated socket, clear internal network event records, and reset event objects (optional). 


Syntax
C++ 
int WSAEnumNetworkEvents(
  __in   SOCKET s,
  __in   WSAEVENT hEventObject,
  __out  LPWSANETWORKEVENTS lpNetworkEvents
);


Parameters
s [in] 
A descriptor identifying the socket.


hEventObject [in] 
An optional handle identifying an associated event object to be reset.


lpNetworkEvents [out] 
A pointer to a WSANETWORKEVENTS structure that is filled with a record of network events that occurred and any associated error codes. 


Return Value
The return value is zero if the operation was successful. Otherwise, the value SOCKET_ERROR is returned, and a specific error number can be retrieved by calling WSAGetLastError. 


Error code Meaning 
WSANOTINITIALISED A successful WSAStartup call must occur before using this function. 
 
WSAENETDOWN The network subsystem has failed.
 
WSAEINVAL One of the specified parameters was invalid.
 
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
 
WSAENOTSOCK The descriptor is not a socket.
 
WSAEFAULT The lpNetworkEvents parameter is not a valid part of the user address space. 
 


 


Remarks
The WSAEnumNetworkEvents function is used to discover which network events have occurred for the indicated socket since the last invocation of this function. It is intended for use in conjunction with WSAEventSelect, which associates an event object with one or more network events. The recording of network events commences when WSAEventSelect is called with a nonzero lNetworkEvents parameter and remains in effect until another call is made to WSAEventSelect with the lNetworkEvents parameter set to zero, or until a call is made to WSAAsyncSelect. 


WSAEnumNetworkEvents only reports network activity and errors nominated through WSAEventSelect. See the descriptions of select and WSAAsyncSelect to find out how those functions report network activity and errors. 


The socket's internal record of network events is copied to the structure referenced by lpNetworkEvents, after which the internal network events record is cleared. If the hEventObject parameter is not NULL, the indicated event object is also reset. The Windows Sockets provider guarantees that the operations of copying the network event record, clearing it and resetting any associated event object are atomic, such that the next occurrence of a nominated network event will cause the event object to become set. In the case of this function returning SOCKET_ERROR, the associated event object is not reset and the record of network events is not cleared. 


The lNetworkEvents member of the WSANETWORKEVENTS structure indicates which of the FD_XXX network events have occurred. The iErrorCode array is used to contain any associated error codes with the array index corresponding to the position of event bits in lNetworkEvents. Identifiers such as FD_READ_BIT and FD_WRITE_BIT can be used to index the iErrorCode array. Note that only those elements of the iErrorCode array are set that correspond to the bits set in lNetworkEvents parameter. Other parameters are not modified (this is important for backward compatibility with the applications that are not aware of new FD_ROUTING_INTERFACE_CHANGE and FD_ADDRESS_LIST_CHANGE events). 


The following error codes can be returned along with the corresponding network event.


Event: FD_CONNECT




Error code Meaning 
WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. 
WSAECONNREFUSED The attempt to connect was forcefully rejected. 
WSAENETUNREACH The network cannot be reached from this host at this time. 
WSAENOBUFS No buffer space is available. The socket cannot be connected. 
WSAETIMEDOUT An attempt to connect timed out without establishing a connection 


 


Event: FD_CLOSE




Error code Meaning 
WSAENETDOWN The network subsystem has failed. 
WSAECONNRESET The connection was reset by the remote side. 
WSAECONNABORTED The connection was terminated due to a time-out or other failure. 


 


Event: FD_ACCEPT


Event: FD_ADDRESS_LIST_CHANGE


Event: FD_GROUP_QOS


Event: FD_QOS


Event: FD_OOB


Event: FD_READ


Event: FD_WRITE




Error code Meaning 
WSAENETDOWN The network subsystem has failed. 


 


Event: FD_ROUTING_INTERFACE_CHANGE




Error code Meaning 
WSAENETUNREACH The specified destination is no longer reachable. 
WSAENETDOWN The network subsystem has failed. 


 


Example Code 
The following example demonstrates the use of the WSAEnumNetworkEvents function.


  // Declare and initialize variables
  SOCKET SocketArray[WSA_MAXIMUM_WAIT_EVENTS], 
    ListenSocket;
  WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
  WSANETWORKEVENTS NetworkEvents;
  sockaddr_in InetAddr;
  DWORD EventTotal = 0, Index, i;
  
  //-------------------------
  // Create a listening socket
  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  InetAddr.sin_family = AF_INET;
  InetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  InetAddr.sin_port = htons(27015);
  
  //-------------------------
  // Bind the listening socket
  bind (ListenSocket, (SOCKADDR *) &InetAddr, sizeof(InetAddr));
  
  //-------------------------
  // Create a new event
  NewEvent = WSACreateEvent();
  
  //-------------------------
  // Associate event types FD_ACCEPT and FD_CLOSE
  // with the listening socket and NewEvent
  WSAEventSelect(ListenSocket, NewEvent, FD_ACCEPT|FD_CLOSE);
  
  //-------------------------
  // Start listening on the socket
  listen(ListenSocket, 10);
  
  //-------------------------
  // Add the socket and event to the arrays, increment number of events
  SocketArray[EventTotal] = ListenSocket;
  EventArray[EventTotal] = NewEvent;
  EventTotal++;
  
  //-------------------------
  // Wait for network events on all sockets
  Index = WSAWaitForMultipleEvents(EventTotal, 
    EventArray, 
    FALSE, 
    WSA_INFINITE, 
    FALSE);
  Index = Index - WSA_WAIT_EVENT_0;
  
  //-------------------------
  // Iterate through all events and enumerate
  // if the wait does not fail.
  for (i = Index; i < EventTotal; i++) {
    Index = WSAWaitForMultipleEvents(1, 
      &EventArray[i], 
      TRUE, 
      1000, 
      FALSE);
    if ((Index != WSA_WAIT_FAILED) && (Index != WSA_WAIT_TIMEOUT)) {
      WSAEnumNetworkEvents(SocketArray[i],
        EventArray[i],
        &NetworkEvents);
    }
  }
  
  //...
  
  
//-------------------------
// Declare and initialize variables
SOCKET SocketArray[WSA_MAXIMUM_WAIT_EVENTS], 
  ListenSocket;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
WSANETWORKEVENTS NetworkEvents;
sockaddr_in InetAddr;
DWORD EventTotal = 0, Index, i;


//-------------------------
// Create a listening socket
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
InetAddr.sin_family = AF_INET;
InetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InetAddr.sin_port = htons(27015);


//-------------------------
// Bind the listening socket
bind (ListenSocket, (SOCKADDR *) &InetAddr, sizeof(InetAddr));


//-------------------------
// Create a new event
NewEvent = WSACreateEvent();


//-------------------------
// Associate event types FD_ACCEPT and FD_CLOSE
// with the listening socket and NewEvent
WSAEventSelect(ListenSocket, NewEvent, FD_ACCEPT|FD_CLOSE);


//-------------------------
// Start listening on the socket
listen(ListenSocket, 10);


//-------------------------
// Add the socket and event to the arrays, increment number of events
SocketArray[EventTotal] = ListenSocket;
EventArray[EventTotal] = NewEvent;
EventTotal++;


//-------------------------
// Wait for network events on all sockets
Index = WSAWaitForMultipleEvents(EventTotal, 
  EventArray, 
  FALSE, 
  WSA_INFINITE, 
  FALSE);
Index = Index - WSA_WAIT_EVENT_0;


//-------------------------
// Iterate through all events and enumerate
// if the wait does not fail.
for (i = Index; i < EventTotal; i++) {
  Index = WSAWaitForMultipleEvents(1, 
    &EventArray[i], 
    TRUE, 
    1000, 
    FALSE);
  if ((Index != WSA_WAIT_FAILED) && (Index != WSA_WAIT_TIMEOUT)) {
    WSAEnumNetworkEvents(SocketArray[i],
      EventArray[i],
      &NetworkEvents);
  }
}


//...






Requirements
Minimum supported client
 Windows 2000 Professional 
Minimum supported server
 Windows 2000 Server 
Header
 Winsock2.h 
Library
 Ws2_32.lib 
DLL
 Ws2_32.dll 


See Also 
Winsock Reference
Winsock Functions
WSAEventSelect
 


 



0 0
原创粉丝点击