大侠请请详细地介绍一下实现的主要步骤好吗?
BOOL CNetMonitorApp::FoundListen()
{
if(m_pSocket!=NULL)
delete m_pSocket;
m_pSocket =new CListenSocket();
if (m_pSocket->Create(622))
{
if (m_pSocket->Listen())
{
// MessageBox(NULL,"in listen","in",MB_OK);
return TRUE;
}
}
AfxMessageBox("Create socket or Listen failed!");
return FALSE;
}
BOOL CNetClientView::ConnectServer()
{
if(pClientSock->m_hSocket==INVALID_SOCKET)
{
if(!pClientSock->Create())//创建连接套接字
{
delete pClientSock;
pClientSock=NULL;
return FALSE;
}
}
// CPtrList NetIPList;
if(NetIPList.IsEmpty())
GetNetIPAddress(NetIPList);
if(NetIPList.IsEmpty()) return FALSE;
//对于面向连接的用户,客户端调用Connect函数即可连接到服务器。
CString *ServerIP;
POSITION pos=NetIPList.GetHeadPosition();
BOOL IsConnectServer=FALSE;
while(pos!=NULL)
{
ServerIP=(CString*)NetIPList.GetNext(pos);
if(pClientSock->Connect((*ServerIP),622))//"192.168.1.37"LPCSTR(*ServerIP)
{
if(NetIPList.GetCount()>1)
{
CString* str=new CString(LPCSTR(*ServerIP));
while(!NetIPList.IsEmpty())
delete NetIPList.RemoveHead();
NetIPList.AddTail((void*)str);
SaveServerIP(LPCSTR(*str));
}
IsConnectServer=TRUE;
break;
}
}
if(!IsConnectServer)
{
SetTimer(3,30000,0);
return FALSE;
}
pClientSock->Initialize();//初始化套接字
char lpname[200];
DWORD len=200;
GetComputerName(lpname,&len);
CMsg msg;
msg.m_strText=lpname;
msg.n_Bytes=strlen(lpname); //strName.GetLength();
msg.InfoFlag="HostName";
pClientSock->SendMsg(&msg);
SetTimer(1,1500,0);
return TRUE;
}
///服务器接受connect
CReceiveSocket* pSocket = new CReceiveSocket(this);
pp_Socket=pSocket;
lpSockAddr=new SOCKADDR;
int n_Byte=sizeof(SOCKADDR);
CListenSocket* pListenSocket=((CNetMonitorApp*)AfxGetApp())->m_pSocket;
if (!(pListenSocket->Accept(*pSocket,lpSockAddr,&n_Byte)))
{
MessageBox(NULL,"Accept socket error!",TEXT("Error"),MB_OK);
delete pSocket;
pp_Socket=NULL;
return FALSE;
}