18,363
社区成员




UINT SendRecvProc(LPVOID lparam)
{
RECVPARAM *pRecvData=(RECVPARAM *)lparam;
CClientApp* pThis = (CClientApp*)pRecvData->pVoid;
//AfxSocketInit(NULL);
CSocket *sockClient = new CSocket;
sockClient->Create();
// if socket error
if( sockClient->m_hSocket==INVALID_SOCKET)
{
return 0;
}
sockClient->Connect( pThis->m_strName, pThis->m_port+10 );
// if error
if( sockClient->m_hSocket==INVALID_SOCKET)
{
return 0;
}
CSocketFile *m_socketFile=new CSocketFile(sockClient);
CArchive *m_Write=new CArchive(m_socketFile,CArchive::store); // write the data into the string
CString sendContent;
// send the command that capture the image from the screen.
pThis->m_pMainWnd->SendMessage(WM_COMMAND, ID_CAPTURE_FROM_SCREEN, 0);
// Process the original image with none white area
// pThis->pD1->CutWhiteArea();
// if zoom
if(pThis->Ratio!=1) pThis->pD1->RatioZoomin( pThis->Ratio );
//
int nWidth = pThis->pD1->Img_ori[0].size(); //pThis->bm.bmWidth;
int nHeight = pThis->pD1->Img_ori.size(); //pThis->bm.bmHeight;
fprintf(pThis->fin,"nWidth:%d nHeight:%d\n",nWidth,nHeight);
CString str;
str.Format("宽:%d,高:%d",nWidth,nHeight);
// AfxMessageBox(str);
sendContent = "";
sendContent += '1';
sendContent += 'I';
sendContent += (char)(nWidth/256-128);
sendContent += (char)(nWidth%256-128);
sendContent += (char)(nHeight/256-128);
sendContent += (char)(nHeight%256-128);
// if error
if( sockClient->m_hSocket==INVALID_SOCKET)
{
pThis->lock = false;
return 0;
}
*m_Write<<sendContent;
m_Write->Flush();
if(0 == sizeofdata)
{
delete [] lpvBits;
lpvBits = NULL;
}
*/
for(int i=0;(i+0)<nHeight;i+=1)
{
sendContent = "";
sendContent += '1';
sendContent += 'P';
sendContent += (char)(i/256-128);
sendContent += (char)(i%256-128);
sendContent += (char)1;
int j;
for( j=0;j<nWidth;j++)
{
sendContent += (char)(pThis->pD1->Img_ori[i][j]-128); //(char)(pD1->lpvBits[(i*nWidth+j)*4+0]-128);
}
/
// if error
if( sockClient->m_hSocket==INVALID_SOCKET)
{
pThis->lock = false;
return 0;
}
*m_Write<<sendContent;
m_Write->Flush();
}
pThis->Iscompute = 0;
pThis->pD1->PreImage = pThis->pD1->Img_ori;
switch(pThis->Iscompute)
{
case 0:
sendContent = "1E0";
break;
case 1:
sendContent = "1E1";
break;
case 2:
sendContent = "1E2";
break;
default:
break;
}
// if error
// AfxMessageBox(sendContent);
if( sockClient->m_hSocket==INVALID_SOCKET)
{
pThis->lock = false;
return 0;
}
*m_Write<<sendContent;
m_Write->Flush();
//sockClient->AsyncSelect(0);
//sockClient->Close();
m_Write->Close();
m_socketFile->Close();
sockClient->Close();
delete m_Write;
delete m_socketFile;
delete sockClient;
delete pRecvData;
return 0;
}
接收端:
UINT SendRecvProc1(LPVOID lparam)
{
RECVPARAM *pRecvData=(RECVPARAM *)lparam;
CServerApp* pThis = (CServerApp*)pRecvData->pVoid;
pThis->lock = true;
CSocket sockSrvr;
sockSrvr.Create(pThis->m_port+10);
sockSrvr.Listen();
CSocket *sockConnection = new CSocket;
sockSrvr.Accept(*sockConnection);
// 判断异常
if( sockConnection->m_hSocket==INVALID_SOCKET)
{
pThis->lock = false;
return 0;
}
CString data;
CSocketFile *m_socketFile=new CSocketFile(sockConnection);
CArchive *m_Read=new CArchive(m_socketFile,CArchive::load);//从套接字中读数据
// 第一部分,接收图像信息头
do
{
*m_Read>>data;//从套接字中接收内容写入到content中
if(data.GetLength()>=2 && data[0]=='1' && data[1]=='I')
{
break;
}
}while(1);
// 获取宽和高
pThis->pD->nWidth = (data[2]+128)*256L+data[3]+128;
pThis->pD->nHeight = (data[4]+128)*256L+data[5]+128;
//fprintf(fout,"[%d %d]\n",pThis->pD1->nWidth,pThis->pD1->nHeight);
// BGR
//BYTE* tmpBits = new BYTE[pThis->pD1->nWidth*pThis->pD1->nHeight*4];
//pThis->pD1->Bits = new BYTE[pThis->pD1->nWidth*pThis->pD1->nHeight*4];
// 第二部分,接收图像矩阵信息
for(int i=0;i<pThis->pD->nHeight;i++)
{
data = "";
do
{
*m_Read>>data;
if(data.GetLength()>=2 && data[0]=='1' && data[1]=='P')
{
break;
}
}while(1);
int row = (data[2]+128)*256+data[3]+128;
data = "";
// 第三部分,收尾
do{
*m_Read>>data;
if(data.GetLength()>= 2&& data[0]=='1' && data[1]=='E')
{
break;
}
}while(1);
if (data[2]=='0')
{
pThis->pD->stat = "有效图像";
pThis->pD->Iscompute = true;
}
else if(data[2]=='1')
{
pThis->pD->stat = "重复图像,检测";
// if (true==pThis->pD->Iscompute)
// {
pThis->pD->Iscompute = true;
// }
}
else if(data[2]=='2')
{
pThis->pD->stat = "无效图像,不检测";
// if (true==pThis->pD->Iscompute)
// {
pThis->pD->Iscompute = false;
// }
}
//sockConnection->AsyncSelect(0);
//sockConnection->Close();
m_Read->Close();
m_socketFile->Close();
sockConnection->Close();
sockSrvr.Close();
delete m_Read;
delete m_socketFile;
delete sockConnection;
delete pRecvData;
pThis->m_pMainWnd->SendMessage(WM_COMMAND, WM_MY_MESSAGE1, 0);//Doc->Resultis()
// pThis->lock = false;
// AfxEndThread(0);
return 0;
}