}
4.在析构函数中完成必需的清除操作:
CServerView::~CServerView()
{
if(m_bInitialized)本文来自辣,文~论^文·网原文请找腾讯752018766
WSACleanup();
closesocket(m_sock);
if(m_hListenThread)
::TerminateThread(m_hListenThread,0);
}
5.定义接收和处理消息的线程:
long WINAPI Listen(CServerView *pView)
{
char msg[2000]="";
int nchar;
SOCKADDR_IN saClnt;
int saClntLen;
while(1)
{
saClntLen=sizeof(saClnt);
nchar=recvfrom(pView->m_sock,msg,1024,0,
(PSOCKADDR)&saClnt,&saClntLen);
if(nchar<0) { pView->m_data[pView->count++]+
="Error in recvfrom\n";
pView->InvalidateRect(NULL);
}
else
{
switch(msg[0])
{
case'A':
wsprintf(msg,"A: Client from %s
attached\n",inet_ntoa(saClnt.sin_addr));
pView->m_data[pView->count++]=msg;
pView->flag=TRUE;
pView->InvalidateRect(NULL);
pView->Isconnect=TRUE;
pView->saClnt=saClnt;
pView->saClntLen=saClntLen;
sendto(pView->m_sock,msg,1024,0,
(PSOCKADDR)&saClnt,saClntLen);
上一页 [1] [2] [3] [4] [5] [6] 下一页