关于Accept的返回值问题??

zhoumeifang 2006-03-31 12:41:46
Accept返回的是客户端的还是服务器的 SOCKET?
我想在服务器端取得客户端的IP地址和端口号,应该怎么写啊?

在服务器端:

SOCKADDR_IN conAddr;
int len = sizeof(conAddr);
SOCKET conSock;
CString strIP;
CString strFinal;
int iPort;
CString strPort;
while(1)
{
conSock = accept(sockListen,(SOCKADDR*)&conAddr,&len);
PARAMRECV* paramRecv = new PARAMRECV;
paramRecv ->sock = conSock;
paramRecv ->ptr = pServer;
strIP = inet_ntoa(conAddr.sin_addr);
iPort = ntohl(conAddr.sin_port);
strPort.Format("%s",iPort);

strFinal += "IP地址为: " + strIP + strPort + " 的客户端连接到服务器"+ "\r\n" ;
//strFinal += "\n";
pServer ->m_pDlg ->SetDlgItemTex(IDC_EDIT1,strFinal);

}


我这样取得客户端的端口号,正确吗?
运行是总是有错啊??
...全文
394 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
soaroc 2006-04-01
  • 打赏
  • 举报
回复
iPort = ntohl(conAddr.sin_port);
改为
iPort = ntohs(conAddr.sin_port);
试一试,由于段口号是短字节,因该使用ntohs
cnwolf 2006-03-31
  • 打赏
  • 举报
回复
accept返回的就是客户端的地址和端口
wanglei051 2006-03-31
  • 打赏
  • 举报
回复
struct sockaddr_in client;
CString meg;
int salen = sizeof(client);
pSocket->GetPeerName((struct sockaddr FAR *)&client, &salen);
printf("Connect from %s:%d\n",inet_ntoa( client.sin_addr ),client.sin_port)
ouyh12345 2006-03-31
  • 打赏
  • 举报
回复
返回的是客户端的socket。
什么错?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧