首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

帮小弟我把下边代码转成BCB

2012-03-01 
帮我把下边代码转成BCBDWORD WINAPI CSendFileServer::ThreadListen(LPVOID lpParam){CSendFileServer* pS

帮我把下边代码转成BCB
DWORD WINAPI CSendFileServer::ThreadListen(LPVOID lpParam)
{
CSendFileServer* pServer = (CSendFileServer*)lpParam;
SOCKET sockListen = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(pServer->m_sClientIP);
sin.sin_port = htons(pServer->m_iListenPort);

if(bind(sockListen, (SOCKADDR*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
closesocket(sockListen);
pServer->m_pWnd->SendMessage(WM_BINDERROR);
return 1;
}

if(listen(sockListen, 5) == SOCKET_ERROR)
{
closesocket(sockListen);
pServer->m_pWnd->SendMessage(WM_LISTENERROR);
return 1;
}

pServer->m_pWnd->SendMessage(WM_STARTLISTEN);

fd_set fdListen;
timeval seltime;
seltime.tv_sec = 0;
seltime.tv_usec = 10000;

while(!pServer->m_bEndListenThread)
{
FD_ZERO(&fdListen);
FD_SET(sockListen, &fdListen);

if(select(0, &fdListen, NULL, NULL, &seltime) <= 0 || !FD_ISSET(sockListen, &fdListen) )
continue;

int len = sizeof(sin);
SOCKET sock = accept(sockListen, (SOCKADDR*)&sin, &len);

PARAMRECV* pParamRecv = new PARAMRECV;
pParamRecv->sock = sock;
pParamRecv->ptr = pServer;

DWORD id;
HANDLE h = CreateThread(NULL, 0, ThreadRecv, pParamRecv, 0, &id);
CloseHandle(h);
}
closesocket(sockListen);
return 0;
}


//功能:接收线程
//参数:LPVOID lpParam
//返回:无意义
DWORD WINAPI CSendFileServer::ThreadRecv(LPVOID lpParam)
{
PARAMRECV* pParam = (PARAMRECV*)lpParam;
CSendFileServer* pServer = (CSendFileServer*)pParam->ptr;

fd_set fdRecv;
timeval seltime;
seltime.tv_sec = 0;
seltime.tv_usec = 10000;

while(1)
{
FD_ZERO(&fdRecv);
FD_SET(pParam->sock, &fdRecv);

if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 || !FD_ISSET(pParam->sock, &fdRecv) )
continue;

MSGREQUEST msgRequest;
int iRecvCnt = recv(pParam->sock, (char*)&msgRequest, sizeof(msgRequest), 0);
if(iRecvCnt <= 0)
break;

//如果是请求文件列表
if(msgRequest.iCommand == FILELIST)
{
pServer->m_pWnd->SendMessage(WM_SENDFILELIST, (WPARAM)&pParam->sock, 0);
}

//如果是请求文件数据
else if(msgRequest.iCommand == FILEDATA)
{
long lFileOffset = msgRequest.lFileOffset;

CFile file;
BOOL bResult = file.Open(pServer->m_sFilePath, CFile::modeRead|CFile::shareDenyNone, NULL);
if(!bResult)
{
break;//如果文件打开失败就终止线程
}

char sSendBuf[SENDSIZE];
while(lFileOffset < msgRequest.lFileLength)
{
int iSeek = file.Seek(lFileOffset, CFile::begin);
int iReadCnt = file.Read(sSendBuf, SENDSIZE);
if(iReadCnt ==0)
break;

int iSendCnt = send(pParam->sock, sSendBuf, iReadCnt, 0);

//如果发送失败就终止线程
if(iSendCnt == -1)
break;
else
{
  pServer->m_pWnd->SendMessage(WM_SENDFILESTATUS, (WPARAM)&msgFileStatus, 0);
  lFileOffset += iSendCnt;
  }
}//end of while(lFileOffset < lFileLength)

file.Close();
}//end of else if(msgRequest.iCommand == FILEDATA)

break;
}//end of while(1)
closesocket(pParam->sock);
delete pParam;
return 0;
}

[解决办法]
大概看了看,除了CFile这个类是MFC的,其他代码在C++Builder中都可以编译.只需把CFile的操作换成C里的FILE或API的CreateFile一族或者VCL中的FileOpen一族就可以了.
------解决方案--------------------


vc的?
要是这里面有vc专有的类,基本上是重写.还不如vc编成dll,cb动调.
[解决办法]
CB也能编译MFC。

[解决办法]

探讨
CB也能编译MFC。

[解决办法]
探讨
这个问题解决了。谢谢楼上几位。
继续问问题:

我定义了一个CSendFileServer类,然后在这个类的某个函数里,我想用SendMessage函数,给某一个窗体,或者程序发消息,让窗体执行消息操作,怎么定义这个窗口句柄
C的代码是在类里定义了
//窗口指针
CWnd* m_pWnd;

我应该怎么改,改成:TForm* m_pWnd,但执行后,不响应消息函数

热点排行