我在程序里是这样做的:
// CMPP120Dlg.cpp : implementation file
//#include "stdafx.h"
#include "CMPP120.h"
#include "CMPP120Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------
UINT WorkerThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
AfxMessageBox("OK");
CWnd *pstaTimer=(CWnd*)pParam;
for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}
return 0;   // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//----------------------------------------
UINT SocketThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
CCMPP120Dlg *mDlg;//=(CCMPP120Dlg*)pParam;
CSocket* mSock=(CSocket*)pParam;
char mBuff[4096];
int len;
while(!(mDlg->mText=="exit"))
{
len=mSock->Receive(mBuff,4096,0) ;
if (len>0)
{
mBuff[len]='\0';
            mDlg->mRecv=mBuff;
mDlg->UpdateData(FALSE);
    }
        Sleep(100);
} /*for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}*/
return 0;   // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//-------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg dialogCCMPP120Dlg::CCMPP120Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCMPP120Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCMPP120Dlg)
mRecv = _T("");
mText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CCMPP120Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCMPP120Dlg)
DDX_Control(pDX, IDC_STATIC1, m_REC);
DDX_Text(pDX, IDC_EDIT1, mRecv);
DDX_Text(pDX, IDC_EDIT2, mText);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CCMPP120Dlg, CDialog)
//{{AFX_MSG_MAP(CCMPP120Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg message handlersBOOL CCMPP120Dlg::OnInitDialog()
{
CDialog::OnInitDialog();    
    //CCMPP120Dlg* Dlg;
//Dlg->OnInitDialog; 
mHost="127.0.0.1";
mPort=8801;
if(!mSocket.Create())
{
AfxMessageBox("套接字建立错误!");
return TRUE;
}
if(!mSocket.Connect(mHost,mPort))
{
AfxMessageBox("连接不到服务器!");
return TRUE;
}
////////////////////////////////////////////////////////////
在这里能传入窗体吗?
AfxBeginThread(SocketThread,&mSocket);
///////////////////////////////////////////////////////////////
//UpdateData(FALSE);
return TRUE;  // return TRUE  unless you set the focus to a control
}// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCMPP120Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CCMPP120Dlg::OnButton1() 
{
UpdateData();
mSocket.Send(mText,mText.GetLength());
}