1. DbThread.h处理
在DbThread.h里加入一些必要的头文件,和一些必要的全局变量等,具体如下,
#include "AdcInf.h"
#include <WINSOCK2.H>
将protected:
CDbThread();
virtual ~CDbThread();
改为
public:
CDbThread(); // protected constructor used by dynamic creation
virtual ~CDbThread();
定议一个构造函数CDbThread(CAdcInf *rs);
公共变量
CAdcInf *rs;
int sockSrv;
int sockClient;
SOCKADDR_IN addrSrv;
SOCKADDR_IN addrClient;
char buf[20];
2. DbThread.c的处理
在DbThread.c中,完成构造函数,完善线程初始化与退出,及线程运行。代码如下
CDbThread::CDbThread(CAdcInf *rs)
{
this->rs=rs;
}
BOOL CDbThread::InitInstance()
{
// TODO: perform and per-thread initialization here
//初始化TCP服务器
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup();
return TRUE;
}
sockSrv=socket(AF_INET,SOCK_STREAM,0);
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(1001);
bind(sockSrv,(SOCKADDR *)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,2);
return TRUE;
}
int CDbThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
//清除TCP
closesocket(sockSrv);
WSACleanup();
return CWinThread::ExitInstance();
}
|