_REMOVING BLOCKING NETWORK I/O FROM WINDOWS PROGRAMS_ by George F. Frazier and Derek Yenzer Listing One BOOL TMainDialog::TransmitDoneMessage() { HEADER_DATA toTransmit; toTransmit.message = kchDoneMessage; if(m_CommunicationSocket.Send(&toTransmit, sizeof(toTransmit)) != sizeof(toTransmit)) { return(FALSE); } return(TRUE); } // TMainDialog::TransmitDoneMessage BOOL TMainDialog::TransmitPixelMessage(LPPIXEL_DATA lpData) { HEADER_DATA toTransmit; toTransmit.message = kchPixelMessage; if(m_CommunicationSocket.Send(&toTransmit, sizeof(toTransmit)) != sizeof(toTransmit)) { return(FALSE); } if(m_CommunicationSocket.Send(lpData, sizeof(PIXEL_DATA)) != sizeof(PIXEL_DATA)) { return(FALSE); } return(TRUE); } // TMainDialog::TransmitPixelMessage BOOL TMainDialog::TransmitLineMessage(LPLINE_DATA lpData) { HEADER_DATA toTransmit; toTransmit.message = kchLineMessage; if(m_CommunicationSocket.Send(&toTransmit, sizeof(toTransmit)) != sizeof(toTransmit)) { return(FALSE); } if(m_CommunicationSocket.Send(lpData, sizeof(LINE_DATA)) != sizeof(LINE_DATA)) { return(FALSE); } return(TRUE); } // TMainDialog::TransmitLineMessage ------------------------ receive code ------------------------ void TMainDialog::ReadSocketData() { HEADER_DATA toRead; if(m_CommunicationSocket.Receive(&toRead, sizeof(toRead)) != sizeof(toRead)) { // ... handle error ... return; } while(toRead.message != kchDoneMessage) { if(!ProcessMessage(toRead.message)) { // ... handle error ... return; } if(m_CommunicationSocket.Receive(&toRead, sizeof(toRead)) != sizeof(toRead)) { // ... handle error ... return; } } // ... normal close ... } // TMainDialog::ReadSocketData BOOL TMainDialog::ProcessMessage(char chMessage) { BOOL bConnectionOK = FALSE; switch(chMessage) { case(kchPixelMessage): bConnectionOK = ProcessPixelMessage(); break; case(kchLineMessage): bConnectionOK = ProcessLineMessage(); break; } return(bConnectionOK); } // TMainDialog::ProcessMessage BOOL TMainDialog::ProcessPixelMessage() { PIXEL_DATA toRead; if(m_CommunicationSocket.Receive(&toRead,sizeof(toRead)) != sizeof(toRead)) { return(FALSE); } // ... draw pixel ... return(TRUE); } // TMainDialog::ProcessPixelMessage BOOL TMainDialog::ProcessLineMessage() { LINE_DATA toRead; if(m_CommunicationSocket.Receive(&toRead,sizeof(toRead)) != sizeof(toRead)) { return(FALSE); // ... draw line ... return(TRUE); } // TMainDialog::ProcessLineMessage Listing Two void TMainDialog::ReadSocketData() { HEADER_DATA toRead; if(m_CommunicationSocket.Receive(&toRead,sizeof(toRead)) != sizeof(toRead)) { // ... handle error ... return; } while(toRead.message != kchDoneMessage) { if(!ProcessMessage(toRead.message)) { // ... handle error ... return; } if(m_CommunicationSocket.Receive(&toRead, sizeof(toRead)) != sizeof(toRead)) { // ... handle error ... return; } } // ... normal close ... } // TMainDialog::ReadSocketData BOOL TMainDialog::ProcessMessage(char chMessage) { BOOL bConnectionOK = FALSE; switch(chMessage) { case(kchPixelMessage): bConnectionOK = ProcessPixelMessage(); break; case(kchLineMessage): bConnectionOK = ProcessLineMessage(); break; } return(bConnectionOK); } // TMainDialog::ProcessMessage BOOL TMainDialog::ProcessPixelMessage() { PIXEL_DATA toRead; if(m_CommunicationSocket.Receive(&toRead,sizeof(toRead)) != sizeof(toRead)) { return(FALSE); } // ... draw pixel ... return(TRUE); } // TMainDialog::ProcessPixelMessage BOOL TMainDialog::ProcessLineMessage() { LINE_DATA toRead; if(m_CommunicationSocket.Receive(&toRead,sizeof(toRead)) != sizeof(toRead)) { return(FALSE); } // ... draw line ... return(TRUE); } // TMainDialog::ProcessLineMessage Example 1: char i; Recv(s, &i, sizeof(char), NULL); if (i != '0') { ErrorHandler(); } else { ComputeResult(i); }