// DppSocket.cpp : implementation file // #include "stdafx.h" #include "DppSocket.h" //////////////////////////////////////////////////////////////// // Include dialog headers here for direct OnReceive calls //////////////////////////////////////////////////////////////// //#include "vcDP5Dlg.h" // example sockownMainDlg //#include "SelectDP5.h" // example sockownStartDlg //////////////////////////////// //#ifdef _DEBUG //#define new DEBUG_NEW //#undef THIS_FILE //static char THIS_FILE[] = __FILE__; //#endif ///////////////////////////////////////////////////////////////////////////// // CDppSocket CDppSocket::CDppSocket() { m_pEvtDlg = NULL; m_SocketOpen = FALSE; m_DataMode = FALSE; m_Owner = sockownNone; DppPort = 10001; DppAddr = inet_addr("192.168.0.28"); } CDppSocket::~CDppSocket() { m_pEvtDlg = NULL; } // Do not edit the following lines, which are needed by ClassWizard. #if 0 BEGIN_MESSAGE_MAP(CDppSocket, CSocket) //{{AFX_MSG_MAP(CDppSocket) //}}AFX_MSG_MAP END_MESSAGE_MAP() #endif // 0 ///////////////////////////////////////////////////////////////////////////// // CDppSocket member functions //BOOL CDppSocket::CreateSocket(CDialog *pEvtDlg) BOOL CDppSocket::CreateSocket(CWnd *pEvtDlg) { bool broadcast = TRUE; if (m_SocketOpen) { Close(); ResetEventDlg(); } if(!Create(0, SOCK_DGRAM)){ AfxMessageBox("Cannot Open Socket"); } else { m_SocketOpen = TRUE; } if (m_SocketOpen) { SetSockOpt(SO_BROADCAST, &broadcast, sizeof(BOOL)); SetEventDlg(pEvtDlg); } return m_SocketOpen; } void CDppSocket::CloseSocket() { ResetEventDlg(); } BOOL CDppSocket::IsSocketOpen() { return (m_SocketOpen); } void CDppSocket::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class if ((nErrorCode == 0) && (m_pEvtDlg != NULL)) { ::SendMessage(m_pEvtDlg->GetSafeHwnd(), WM_DPPSOCKET_RCV_BUFFER, (WPARAM) NULL, (LPARAM) nErrorCode); // rcv byte //m_pEvtDlg->m_hWnd //switch (m_Owner) { // case sockownMainDlg: // //((CvcDP5Dlg*)m_pEvtDlg)->OnReceive(nErrorCode); // break; // case sockownStartDlg: // //((CSelectDP5*)m_pEvtDlg)->OnReceive(nErrorCode); // break; //} } CAsyncSocket::OnReceive(nErrorCode); } //void CDppSocket::SetEventDlg(CDialog *pEvtDlg) void CDppSocket::SetEventDlg(CWnd *pEvtDlg) { m_pEvtDlg = pEvtDlg; } void CDppSocket::ResetEventDlg() { m_pEvtDlg = NULL; } bool CDppSocket::SendPacketInet(unsigned char Buffer[], CDppSocket *DppSock) { int PLen; // socket uses int value SOCKADDR_IN RemoteAddr; int dwBytes; PLen = (Buffer[4] * 256) + Buffer[5] + 8; RemoteAddr.sin_family = AF_INET; RemoteAddr.sin_port = htons(DppSock->DppPort); RemoteAddr.sin_addr.s_addr = DppSock->DppAddr; dwBytes = DppSock->SendTo(Buffer, PLen, (const SOCKADDR* )&RemoteAddr, sizeof(RemoteAddr)); if(SOCKET_ERROR == dwBytes){ //CString str; //str.Format("Error Code: %i.", GetLastError()); //AfxMessageBox(str + " Unable To Send Packet. Please Check Network Connection."); } return true; }