我在OnAccept和OnReceive各得到一个CSocket指针请问我现在怎样根据这个指针得到客户端的IP这里主要是CSocket可能有很多个,是个链
解决方案 »
- 组合框combobox中type为drop list,如何一开始使其就有数据显示
- 请教:如何实现桌面级别的窗口平铺、层叠
- 树有多个根,怎么返回一个指定的根
- 紧急求教,如何实现类似visio一样的图标拖动效果?
- 再一次CreateFile临时文件时出现创建失败,原因是被另一进程使用,如何才能CreateFile同名文件。多谢。
- 急:如何改计算机名?SetComputerName?SetComputerNameEx
- IStream问题
- 搞定了一个问题:链接静态库死活链不上
- 关闭别的程序
- 各位大侠,请问学习VC++需要看什么类型的原代码?
- VS2003无法启动
- 牛顿发现万有引力是因为当苹果炸到他头上的时候,他去思考。计算机高手是,当遇到问题时候,勇于思考 黑群!!!
int len = sizeof(addr);
Accept(*m_pClientSocket,(SOCKADDR*)&addr,&len);
TRACE(IP: %s; PORT: %d\n, inet_ntoa(addr.sin_addr), addr.sin_port);
请问OnReceive下呢?
|___CAsyncSocket
|__CSocket.可以看出,CSocket是从CAsyncSocket继承得到的。CAsyncSocket有一个方法GetPeerName可以得到对方的IP信息。
因此,答案就明了。void CMySocket::OnReceive(int nErrorCode)
{
CString sIP;
UINT nPort;
if(GetPeerName(sIp, nPort) == 0)
{
CString sInfo;
sInfo.Format("GetPeerName() failed! last error: %d", GetLastError());
AfxMessageBox(sInfo);
return;
} CString sInfo;
sInfo.Format("The peer IP:%s, port:%d", sInfo.operator LPCTSTR(), nPort);
AfxMessageBox(sInfo);
}
sInfo.Format("The peer IP:%s, port:%d", sIP.operator LPCTSTR(), nPort);
AfxMessageBox(sInfo);
{
char buff[1024];
int bufflen=1024; int nLen = CSocket::Receive(buff,bufflen);
m_pParentWnd->PostMessage (WM_UPDATEMSG,(WPARAM)buff,(LPARAM)nLen); SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
GetPeerName((SOCKADDR*)&ClientAddr,&addr_length);
CString strAddr = inet_ntoa(ClientAddr.sin_addr);//********Client IP
UINT IntProt = ClientAddr.sin_port; //*****************Client Port
CString strmp;
strmp.Format(":%d",IntProt);
AfxMessageBox(strAddr+strmp); CSocket::OnReceive(nErrorCode);
m_pWnd->ProcessPendingRead(this);
}
void CListenSocket::OnAccept(int nErrorCode)
{
CServerSocket* pSocket=new CServerSocket; SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
CSocket::Accept (*pSocket,(SOCKADDR*)&ClientAddr,&addr_length);
lpIp = inet_ntoa(ClientAddr.sin_addr);//********Client IP
nPort = ClientAddr.sin_port; //*****************Client Port
CString strmp;
strmp.Format(":%d",nPort);
AfxMessageBox(lpIp+strmp);
// m_list_pSocket.AddTail(pSocket); pSocket->m_pParentWnd=m_pParentWnd;
m_pParentWnd->PostMessage (WM_UPDATECONNECT,0,0);
CSocket::OnAccept(nErrorCode);
m_pWnd->ProcessPendingAccept();
}
以前,我用这两个函数得到的IP PROT值是不一样的,不知道为什么