Home > Software engineering >  Or about MFC under networking two computers communicate with each other
Or about MFC under networking two computers communicate with each other

Time:12-05

TCP assistant now and my software can send messages to each other and the pop-up dialog box 123, but still my software after sending the message, message dialog list box and can not play a dialog box, for unknown reasons,
The code is as follows:
Void DNetWorkChess: : OnBnClickedBconnect ()
{
//TODO: add the control notification handler code
Cstrings SocketInfo=_T (" ");
TCHAR TBuffer [4096]={0};

If (m_itype==0)
{
if(! AfxSocketInit ())
AfxMessageBox (_T (" network interface initialization failed "), MB_OK | MB_ICONSTOP);

If (m_servSocket m_hSocket==INVALID_SOCKET)
{
The UpdateData (true);

BOOL bFlag=m_servSocket. Create (m_iServPort SOCK_STREAM, FD_ACCEPT);

if(! BFlag)
{
SocketInfo. The Format (_T (" % d server port network interface to create failure!" ), m_iServPort);
AfxMessageBox (SocketInfo, MB_OK, 0);
M_servSocket. Close ();
The PostQuitMessage (0);
return;
}
}
if(! M_servSocket. Listen (1))
{
Int nErrorCode=m_servSocket. GetLastError ();
If (nErrorCode!=WSAEWOULDBLOCK)
{
SocketInfo. The Format (_T (" server network interface code error!" ));
AfxMessageBox (SocketInfo, MB_OK, 0);
M_servSocket. Close ();
The PostQuitMessage (0);
return;
}
}
The else
{
MultiByteToWideChar (0, 0, "listen... ", 1, TBuffer, 4069);
M_LRMSG. InsertString (0, TBuffer);

GetDlgItem (IDC_BClientSend) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_BServerSend) - & gt; The EnableWindow (TRUE);
}
}
Else if (m_itype==1)
{
M_clientSocket. ShutDown (2);
M_clientSocket. M_hSocket=INVALID_SOCKET;
M_clientSocket. M_bConnected=FALSE;

The UpdateData (true);

Memcpy (m_CServAdr m_SServAdr, sizeof (m_CServAdr));
M_szPort=m_iServPort;
The SetTimer (3200, NULL);
TryCount=0;
}
}
Void DNetWorkChess: : OnTimer (UINT_PTR nIDEvent)
{
//TODO: add the message handler code and/or invoke the default
If (m_itype==1)
{
if(! AfxSocketInit ())
AfxMessageBox (_T (" network interface initialization failed "), MB_OK | MB_ICONSTOP);

If (m_clientSocket m_hSocket==INVALID_SOCKET)
{
BOOL bFlag=m_clientSocket. Create (0, SOCK_STREAM, FD_CONNECT);

if(! BFlag)
{
AfxMessageBox (_T (" the end of the service network interface error, "), MB_OK, 0).
M_clientSocket. Close ();
The PostQuitMessage (0);
return;
}
}

M_clientSocket. Connect (m_SServAdr m_szPort);
If (m_clientSocket. M_bConnected)
{
GetDlgItem (ID_BConnect) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_RSERVER) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_RCLIENT) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_EServAdr) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_ESERVPORT) - & gt; The EnableWindow (FALSE);
GetDlgItem (IDC_BCloseNet) - & gt; The EnableWindow (TRUE);

GetDlgItem (IDC_BClientSend) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_BServerSend) - & gt; The EnableWindow (FALSE);
}
The else
{
GetDlgItem (ID_BConnect) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_RSERVER) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_RCLIENT) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_EServAdr) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_ESERVPORT) - & gt; The EnableWindow (TRUE);
GetDlgItem (IDC_BCloseNet) - & gt; The EnableWindow (FALSE);
}
TryCount++;

If (TryCount>=10 | | m_clientSocket. M_bConnected)
{
KillTimer (3);
If (TryCount>=10)
AfxMessageBox (_T (" connection "), MB_OK, 0).
return;
}
}
CDialogEx: : OnTimer (nIDEvent);
}

Void DNetWorkChess: : OnBnClickedBServerSend ()
{
//TODO: add the control notification handler code
TCHAR TCHBuffer [4096].

If (m_servSocket. M_pSocket - & gt; M_bConnected)
{
M_EMessage. GetWindowTextW (TCHBuffer, sizeof (TCHBuffer));
WideCharToMultiByte (TCHBuffer CP_ACP, 0, 1, m_servSocket. M_pSocket - & gt; M_szBuffer, sizeof (TCHBuffer), NULL, NULL);

M_servSocket. M_pSocket - & gt; AsyncSelect (FD_WRITE);
M_EMessage. SetWindowText (_T (" "));
}
}

Void DNetWorkChess: : OnBnClickedBClientSend ()
{
//TODO: add the control notification handler code
TCHAR TCHBuffer [4096].

If (m_clientSocket. M_bConnected)
{
M_EMessage. GetWindowTextW (TCHBuffer, sizeof (TCHBuffer));
WideCharToMultiByte (TCHBuffer CP_ACP, 0, 1, m_clientSocket. M_szBuffer, sizeof (TCHBuffer), NULL, NULL);

M_clientSocket. AsyncSelect (FD_WRITE);
M_EMessage. SetWindowText (_T (" "));
}
}


//MySocket member function
Void MySocket: : OnConnect (int nErrorCode)
{
//TODO: add in this dedicated code and/or call the base class
TCHAR TBuffer [4096]={0};

If (nErrorCode==0)
{
M_bConnected=TRUE;

CMainFrame * pFrame=(CMainFrame *) AfxGetApp () - & gt; M_pMainWnd;
PDlg DNetWorkChess *=(DNetWorkChess *) pFrame - & gt; The GetActiveWindow ();

Memcpy (m_szBuffer, "connected,", sizeof (m_szBuffer));

M_szBuffer MultiByteToWideChar (0, 0, 1, TBuffer, sizeof (m_szBuffer));

PDlg - & gt; M_LRMSG. InsertString (0, TBuffer);

If (pDlg - & gt; M_bPureMan)
PDlg - & gt; M_LRMSG. InsertString (0, L "pure mode,");

Memset (m_szBuffer, 0, sizeof (m_szBuffer));

AsyncSelect (FD_READ);
}

CAsyncSocket: : OnConnect (nErrorCode);
}

Void MySocket: : OnReceive (int nErrorCode)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related