virtual int Receive (
void* lpbuf,
int nbuflen,
int nflags = 0
);
first set the event AsyncSelect (fd_read) parameters that are of interest to the socket
, the virtual function onreceive () is scanned and the receive () receives the data when it scans to the data to be received.
Lpbuf
Enter a buffer for the data.
Nbuflen
The byte length of the lpbuf.
Nflags
Specifies the method that was invoked. Socket options and parameter nflags depend on this function semantics. The latter constructs any one of the following values with C + +
OROperator:
In a detailed introduction to the data
Msg_peekPeep. The data is copied to the buffer, but is not canceled from the input queue.
msg_oob processing out-of-band data. return value
If no error occurs, receive returns the number of bytes received. Returns 0 if the connection is closed. Otherwise, thesocket_error value is returned, and a specific error code can be retrieved by calling GetLastError. The following error applies to this member function:
wsanotinitialised 's successful afxsocketinit must occur before this API is used.
Wsaenetdown The Windows socket implementation failed to detect the network subsystem.
Wsaenotconn socket is not connected.
Block Windows Socket operation in the wsaeinprogress progress.
The wsaenotsock descriptor is not a socket.
Wsaeopnotsupp Msg_oob specified, however, the socket is not a type sock_stream.
The wsaeshutdown socket is closed, and when the ShutDown call has NHow set to 0 or 2, it is not possible to invoke the receive of the socket.
The wsaewouldblock socket is marked as unoccupied and the Receive operation blocks.
The data in the wsaemsgsize is too large to fit into the specified buffer and is truncated.
The wsaeinval socket has not been bound to bind.
wsaeconnaborted virtual circuits are aborted due to timeouts or other failures.
The virtual circuit resets the wsaeconnresetremotely.
virtual void onreceive (
int nerrorcode
);
void cmyasyncsocket::onreceive (int nerrorcode) //Cmyasyncsocket is
//derived from CAsyncSocket
{
static int i = 0;
i++;
TCHAR buff[4096];
int nread;
Nread = Receive (buff, 4096);
Switch (nread)
{case
0: Close
();
break;
Case SOCKET_ERROR:
if (GetLastError ()!= Wsaewouldblock)
{
AfxMessageBox (_t ("ERROR occurred"));
Close ();
}
break;
Default:
Buff[nread] = _t (' ");//terminate the string
CString sztemp (buff);
M_strrecv + = sztemp; M_STRRECV is a CString declared
//in Cmyasyncsocket
if (Sztemp.comparenocase (_t ("Bye")) = = 0)
{
shutdown ();
S_eventdone.setevent ();
}
Casyncsocket::onreceive (Nerrorcode);
}