#include "stdafx.h"
#include "Surveillant_Server.h"
#include "process.h"
#include "Surveillant_ServerDoc.h"
#include "Surveillant_ServerView.h"
#include <odbcinst.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerViewCDC *pdc;
SOCKET  socks,newskt_s;
      sockaddr_in sockin_s;
      int  PORT;
     CString str;
 char szHostName[128];
 DWORD dwThreadID;
 HANDLE hThread;
 int nSockErr;
 char recvBuff[DataBufeSize];
 CString str1;
/////////////
SOCKET tempsockets;
DWORD WINAPI Serverthread(LPVOID lpVoid);
IMPLEMENT_DYNCREATE(CSurveillant_ServerView, CView)BEGIN_MESSAGE_MAP(CSurveillant_ServerView, CView)
//{{AFX_MSG_MAP(CSurveillant_ServerView)
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_MESSAGE(WM_MSG,OnEvent)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerView construction/destructionCSurveillant_ServerView::CSurveillant_ServerView()
{    
// TODO: add construction code here
struct hostent *phost;
/////////////////////////////////////////

int i; if( gethostname(szHostName, 128) == 0 ) 
{   
phost=gethostbyname(szHostName);
//m_hostname=szhostname;
i=0;
int j;
int h_length=4;
for(j=0;j<h_length;j++)
{
CString addr;
if(j>0)
str+=".";
addr.Format("%u",(unsigned int)((unsigned char*)phost->h_addr_list[i])[j]);
str+=addr;
}
}
      /////////////////////////////////////////
WSADATA wsd; 
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
AfxMessageBox("错误:WSAStartup failed with error " ); }
////////////////////////////////////////
        PORT=5512;
socks=socket(AF_INET,SOCK_STREAM,0);
sockin_s.sin_family=AF_INET;
sockin_s.sin_addr.s_addr=inet_addr("127.0.0.1");
sockin_s.sin_port=htons(PORT);
if(bind(socks,(struct sockaddr*)&sockin_s,sizeof(sockin_s))<0)
{AfxMessageBox("绑定失败了");
} //WSAAsyncSelect(socks,GetSafeHwnd(),WM_MSG1,FD_ACCEPT);

listen(socks,5);
hThread=CreateThread(NULL,0,Serverthread,this->m_hWnd,0,&dwThreadID);//创建一个新线程用来接受连接
}
CSurveillant_ServerView::~CSurveillant_ServerView()
{
}BOOL CSurveillant_ServerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerView drawingvoid CSurveillant_ServerView::OnDraw(CDC* pDC)
{
CSurveillant_ServerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(10,200,szHostName+str+":");//测试//输出机器名称和IP
    pDC->TextOut(20,300,str1="123");
pdc=pDC;
////////////////////////////////////////////////////
}/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerView printingBOOL CSurveillant_ServerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}void CSurveillant_ServerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}void CSurveillant_ServerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerView diagnostics#ifdef _DEBUG
void CSurveillant_ServerView::AssertValid() const
{
CView::AssertValid();
}void CSurveillant_ServerView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CSurveillant_ServerDoc* CSurveillant_ServerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSurveillant_ServerDoc)));
return (CSurveillant_ServerDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CSurveillant_ServerView message handlers
DWORD WINAPI Serverthread(LPVOID lpVoid) //DWORD WINAPI
{   
HWND hwnd=(HWND)lpVoid;
/*CSurveillant_ServerView *pView=((CSurveillant_ServerView*)((CFrameWnd*)
AfxGetApp()->m_pMainWnd)->GetActiveView());
pView->pDc->TextOut(200,300,"线程成功运行!");*/
int nLen=sizeof(sockin_s);
newskt_s=accept(socks,(struct sockaddr*)&sockin_s,&nLen);
AfxMessageBox("xiancheng yunxing ");
if(newskt_s!=INVALID_SOCKET)
str1="有情况了 !";
//::InvalidateRect(hwnd,NULL,NULL);
pdc->TextOut(20,400,"有情况了 !");
WSAAsyncSelect(socks,GetParent(hwnd),WM_MSG1,FD_CLOSE|FD_READ|FD_ACCEPT);
pView->SetTimer(0,2500,NULL);
return 1;
}void CSurveillant_ServerView::OnEvent(WPARAM wParam, LPARAM lParam)  
{
switch(wParam)
{
/*case FD_ACCEPT:

     newskt_s=accept(socks,(LPSOCKADDR)&sockin_s,(int *)sizeof(sockin_s));
 AfxMessageBox("aaa");
 break;*/
case FD_CLOSE:
closesocket(tempsockets);
AfxMessageBox("客户端关闭了!");
     break;
case FD_READ:
BOOL SendResult=FALSE;// 判断发送是否成功
CString Bufe;
int result;
result=recv(tempsockets,recvBuff,DataBufeSize,0);
if(result==0)
{
AfxMessageBox("连接出问题!不过为正常错误!");
break;
}
recvBuff[result]='\0';
Bufe.Format("%c",recvBuff);
if(Bufe=="SendScreen!")//判断是不是要求发送屏幕信息
{  
AfxMessageBox("SendScreen");
         send(newskt_s,"OK",strlen("OK"),0);
       //SendResult=SendScreenMessage();
}
      break;
}
}void CSurveillant_ServerView::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default

CView::OnTimer(nIDEvent);
}BOOL CSurveillant_ServerView::SendScreenMessage()
{
//char dot[1572864];//1024*768*2
/////////////////////////////static CWindowDC ddc(GetDesktopWindow());//引用桌面窗口指针定义对象ddc
pDc=&ddc;//将指针pDc指向ddc
wdc.CreateCompatibleDC(pDc);//建立与ddc兼容的设备环境
bmp.CreateCompatibleBitmap(pDc,1024,768);//建立与ddc兼容的位图
wdc.SelectObject(&bmp);//选择位图对象
wdc.BitBlt(0,0,1024,768,pDc,0,0,SRCCOPY);//把桌面图象复制到wdc的bmp中
//bmp.GetBitmapBits(
pDc->StretchBlt(0,0,768,768,&wdc,0,0,1024,768,SRCCOPY);
// 将位图信息复制到缓存lpData中
bmp.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
bmp.GetBitmapBits(size,lpData);///
send(newskt_s,lpData,size,0);
return TRUE;
}

解决方案 »

  1.   

    你是怎么判定线程停止了,难道仅仅依靠 Thread 中的 pdc->TextOut 来判断?
      

  2.   

    你的程序结构上存在问题
    1.你在线程里accept,如果没有连接,当然看起来好像停了
    2.你发送信息时无法断定newskt_s已经在线程中创建了,而且你的服务器和客户端公用一个socket,这是没有道理的
      

  3.   

    在工作线程中处理重绘本身就是一个常识性错误。将重绘的工作交由UI线程来完成,线程间交互可以使用PostMessage(不用SendMessage)/PostThreadMessage