我也不知怎么说,还是把有关代码贴出来吧:
1、发送连接
BOOL CConferenceView::PreCreateWindow(CREATESTRUCT& cs)
{
CLogin *log;
log = new CLogin;
if(log->DoModal() != IDOK)
{
delete m_pSocket;
m_pSocket = NULL;
}
else
{
if(!ConnectSocket(log->Server_ip ,1023))
GetIP();
if(m_pArchiveOut!=NULL)
{
msg.picture = -1;
msg.from = log->m_username;
msg.ip = ip;
user_name = log->m_username;
TRY
{
msg.Serialize (*m_pArchiveOut);
m_pArchiveOut->Flush ();
}
}
void CConferenceView::GetIP()
{
if( gethostname(szHostName, 128) == 0 )
{
struct hostent * pHost;
int i;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
LPCSTR psz=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
ip.Format("%s", psz);
}
}
}
2、接收数据
void CConferenceView::ProcessPendingRead () //在CConferenceDoc中调用此方法
{ if(!self)
{
AfxGetMainWnd()->SendMessage(WM_ADDLIST, (LPARAM)&(msg), 6);
self = true;
}
do
{
ReceiveMsg();
if(m_pSocket == NULL)
return;
}while(!m_pArchiveIn->IsBufferEmpty ());
}
void CConferenceView::ReceiveMsg ()
{
memory *p;
TRY
{
msg.Serialize (*m_pArchiveIn);
if(msg.picture==-1 && msg.from!=user_name)
AfxGetMainWnd()->SendMessage(WM_ADDLIST, (LPARAM)&(msg), 6);
if(msg.picture == -3)
AfxGetMainWnd()->SendMessage(WM_ADDLIST+ 1, (LPARAM)&(msg), 6);
if(msg.picture == -4)
{
CString off = "DUMP";
AfxGetMainWnd()->SendMessage(WM_ADDLIST + 1, (LPARAM)&(off), 6);
AfxGetMainWnd()->SetWindowText("服务器已经关闭!);
}
}
}
在CConferenceView.h中已经声明了:
CMsg msg;
CMsg是CObject类,并声明了
CString from; //用户名
CString ip; //ip地址
3、在CMainFrame中用m_olist列表显示信息
void CMainFrame::OnAddMember(WPARAM wParam, LPARAM lParam)
{
CMsg* pStr = (CMsg*)wParam;
this->dlgBar.m_olist.AddItem(0, pStr->from.GetBuffer(0), pStr->ip.GetBuffer(0));
pStr->from.ReleaseBuffer();
pStr->ip.ReleaseBuffer();
}
运行的结果是:用户名可以完全显示在线用户,但是ip没有显示?请高手指点,谢谢!!
1、发送连接
BOOL CConferenceView::PreCreateWindow(CREATESTRUCT& cs)
{
CLogin *log;
log = new CLogin;
if(log->DoModal() != IDOK)
{
delete m_pSocket;
m_pSocket = NULL;
}
else
{
if(!ConnectSocket(log->Server_ip ,1023))
GetIP();
if(m_pArchiveOut!=NULL)
{
msg.picture = -1;
msg.from = log->m_username;
msg.ip = ip;
user_name = log->m_username;
TRY
{
msg.Serialize (*m_pArchiveOut);
m_pArchiveOut->Flush ();
}
}
void CConferenceView::GetIP()
{
if( gethostname(szHostName, 128) == 0 )
{
struct hostent * pHost;
int i;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
LPCSTR psz=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
ip.Format("%s", psz);
}
}
}
2、接收数据
void CConferenceView::ProcessPendingRead () //在CConferenceDoc中调用此方法
{ if(!self)
{
AfxGetMainWnd()->SendMessage(WM_ADDLIST, (LPARAM)&(msg), 6);
self = true;
}
do
{
ReceiveMsg();
if(m_pSocket == NULL)
return;
}while(!m_pArchiveIn->IsBufferEmpty ());
}
void CConferenceView::ReceiveMsg ()
{
memory *p;
TRY
{
msg.Serialize (*m_pArchiveIn);
if(msg.picture==-1 && msg.from!=user_name)
AfxGetMainWnd()->SendMessage(WM_ADDLIST, (LPARAM)&(msg), 6);
if(msg.picture == -3)
AfxGetMainWnd()->SendMessage(WM_ADDLIST+ 1, (LPARAM)&(msg), 6);
if(msg.picture == -4)
{
CString off = "DUMP";
AfxGetMainWnd()->SendMessage(WM_ADDLIST + 1, (LPARAM)&(off), 6);
AfxGetMainWnd()->SetWindowText("服务器已经关闭!);
}
}
}
在CConferenceView.h中已经声明了:
CMsg msg;
CMsg是CObject类,并声明了
CString from; //用户名
CString ip; //ip地址
3、在CMainFrame中用m_olist列表显示信息
void CMainFrame::OnAddMember(WPARAM wParam, LPARAM lParam)
{
CMsg* pStr = (CMsg*)wParam;
this->dlgBar.m_olist.AddItem(0, pStr->from.GetBuffer(0), pStr->ip.GetBuffer(0));
pStr->from.ReleaseBuffer();
pStr->ip.ReleaseBuffer();
}
运行的结果是:用户名可以完全显示在线用户,但是ip没有显示?请高手指点,谢谢!!
这个函数如何实现,贴下代码看看
{
if(ar.IsStoring ())
{
ar << from;
ar << ip;
}
else
{
ar >> from;
ar >> ip;
}
m_msgList.Serialize (ar);
}
怀疑这个SendMessage有问题。一般情况下,LPARAM用来传递地址的,WPARAM用来传递值的,你把msg改用LPARAM来传递试试。
你可以参考一下MSN的协议,或者自己写msn的客户端,这样理解就比较深刻一些。