// CEViewerDlg.cpp : implementation file // #include "stdafx.h" #include "CEViewer.h" #include "CEViewerDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define uchar unsigned char CListBox* gpcListBox; CSocket* gSocket; bool gThreadRun; DWORD WINAPI ClientThread(void *psocket) { CString strString; CString strMain; uchar Buff[4048+2]; int ListCount=0; CString strError; CString strBuffer; int retval; /* ** Run the thread untill someone says stop */ while(true == gThreadRun) { /* ** Send a request */ Buff[0] = 102; Buff[1] = 0; if (gSocket->Send(Buff, 1) == SOCKET_ERROR) { strError.Format(_T("Failed to send on client socket: %d"), gSocket->GetLastError()); gSocket->Close(); delete gSocket; gSocket = NULL; AfxMessageBox (strError,0,0); return FALSE; } /* ** Get a responce */ retval = gSocket->Receive(Buff, 4048); if (SOCKET_ERROR == retval) { strError.Format(_T("Failed to send on client socket: %d"), gSocket->GetLastError()); gSocket->Close(); delete gSocket; gSocket = NULL; AfxMessageBox (strError,0,0); return FALSE; } Sleep(5); /* ** Display data in the list box at the end */ if(1 == retval) { strBuffer = _T("No data in queue"); gpcListBox->InsertString(ListCount,strBuffer); } else { strBuffer.Format(_T("%02x %02x %02x %02x %02x %02x"),Buff[0],Buff[1],Buff[2],Buff[3],Buff[4],Buff[5]); gpcListBox->InsertString(ListCount,strBuffer); } /* ** If the list box if full delete the top item */ if(ListCount > 8) { gpcListBox->DeleteString(0); } else { ListCount++; } } /* ** close the socket */ gSocket->Close(); delete gSocket; gSocket = NULL; } ///////////////////////////////////////////////////////////////////////////// // CCEViewerDlg dialog CCEViewerDlg::CCEViewerDlg(CWnd* pParent /*=NULL*/) : CDialog(CCEViewerDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCEViewerDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCEViewerDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCEViewerDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCEViewerDlg, CDialog) //{{AFX_MSG_MAP(CCEViewerDlg) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCEViewerDlg message handlers BOOL CCEViewerDlg::OnInitDialog() { CString strServer; CString strError; UINT lngPort; CString strBuffer; HANDLE hThread=NULL; CDialog::OnInitDialog(); gSocket = NULL; // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon CenterWindow(GetDesktopWindow()); // center to the hpc screen /* ** Create and then connect the socket */ gSocket = (CSocket*) new CSocket(); if (!gSocket->Create()) { strError.Format(_T("Failed to create client socket: %d! Close and restart app."), gSocket->GetLastError()); AfxMessageBox (strError,0,0); return FALSE; } strServer = _T("206.125.93.79"); lngPort = 5432; if (!gSocket->Connect(strServer, lngPort)) { strError.Format(_T("Failed to connect: %d."), gSocket->GetLastError()); AfxMessageBox (strError,0,0); gSocket->Close(); delete gSocket; gSocket = NULL; return FALSE; } /* ** Get a handle on the list box */ gpcListBox = (CListBox*)this->GetDlgItem(IDC_LISTBOX); /* ** Start the thread up and running */ gThreadRun = true; hThread = CreateThread(NULL, 0, &ClientThread, (LPVOID)&gpcListBox, 0, NULL); return TRUE; // return TRUE unless you set the focus to a control } BOOL CCEViewerDlg::DestroyWindow() { /* ** Close the thread by setting the run to false */ gThreadRun = false; Sleep(1000); /* ** If the socket is still up close it */ if(NULL == gSocket) { gSocket->Close(); delete gSocket; } return CDialog::DestroyWindow(); }