www.pudn.com > 串口以太网通信技术转换.rar > DSock.cpp


// DSock.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CtoE.h" 
#include "DSock.h" 
#include "comm.h" 
#include "backform.h" 
#include "mainfrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDSock 
 
CDSock::CDSock() 
{ 
	char appfile[100]; 
	GetCurrentDirectory(100, appfile); 
	strcat(appfile, "\\app.ini"); 
 
	port = GetPrivateProfileInt("LOCAL", "port", -1, appfile); 
	target_port = GetPrivateProfileInt("TARGET", "port", -1, appfile); 
	char temp[20]; 
	GetPrivateProfileString("TARGET", "ip", "", temp, 20, appfile); 
	target_ip = temp; 
} 
 
CDSock::~CDSock() 
{ 
	Close(); 
} 
 
 
// Do not edit the following lines, which are needed by ClassWizard. 
#if 0 
BEGIN_MESSAGE_MAP(CDSock, CSocket) 
	//{{AFX_MSG_MAP(CDSock) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
#endif	// 0 
 
///////////////////////////////////////////////////////////////////////////// 
// CDSock member functions 
 
int CDSock::InitSocket() 
{ 
	int err = Create(port, SOCK_DGRAM); 
	if(err == FALSE) 
	{ 
		err = GetLastError(); 
		return 0; 
	} 
	return 1; 
} 
 
void CDSock::OnReceive(int nErrorCode)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	char buf[300]; 
	unsigned int pp = target_port; 
	memset(buf, 0, 300); 
	int err = ReceiveFrom(buf, 300, target_ip, pp); 
 
	CMainFrame *p = (CMainFrame *)AfxGetApp()->m_pMainWnd; 
	p->com->SendString(buf, err); 
	TRACE("SOCK: %d %x\n", err, buf[0]); 
		CBackForm *f = (CBackForm *)(p->GetActiveFrame()->GetActiveView()); 
		f->m_recvstr = buf; 
		f->UpdateData(FALSE); 
 
	CSocket::OnReceive(nErrorCode); 
} 
 
int CDSock::SendString(char *buf, int len) 
{ 
	int err = SendTo(buf, len, target_port, target_ip); 
	TRACE("err= %d\n",err); 
	return err; 
}