Triggering events in different dialogs with CAsyncSocket
I just learned some MFC basics this past semester and thought I would
enhance my knowledge by diving into the world of sockets.
My idea was to create a Tic Tac Toe game that could be played over the
interwebs. :-)
I implemented the game as a Dialog based application which uses blank
buttons to form a "grid". (A strange way I know, but I got the idea
from an exercise in the text). Whenever someone clicks on a button, the
text of the button is set to either an "X' or an "O".
I set it up so that the application can act as either a server or
client. And, whenever someone (whoevers turn it is) decides where to
move and clicks the corresponding button to mark that spot, a message
is sent to the other player with information about where the person
moved. The problem arises when I try to set the text on the button of
the dialog that received the message. I am able to call AfxMessageBox
successfully and display the message that arrived, but all else fails.
I've tried doing this in OnReceive using the following code:
//MySocket is a subclass of CASyncSocket
void MySocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
TCHAR buff[8];
int len;
len = Receive(buff, 8);
switch(len) {
case 0:
Close();
break;
case SOCKET_ERROR:
// Error handling is done here
break;
default:
buff[iLen] = 0;
CString temp(buff);
// this works great!
AfxMessageBox(temp);
}
if(nErrorCode==0)
{
//m_pDlg is a CDialog* used to chain function calls to the parent
Dialog
((CTicTacToeDlg*)m_pDlg)->SetButtonText(temp)
//^^Crashes the dialog the received the message!
}
CAsyncSocket::OnReceive(nErrorCode);
}
Intuitively I can see why the program crashes, but I suppose my lack of
understanding of MFC in general is what is hindering me here.
Any help is appreciated!
TIA