首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

recvfrom提取ip的有关问题

2013-12-26 
recvfrom提取ip的问题这么做,能正确打印ip信息_cprintf(the Client recv ip is:%s\n,inet_ntoa(m_BroCli

recvfrom提取ip的问题
这么做,能正确打印ip信息

_cprintf("the Client recv ip is:%s  \n",inet_ntoa(m_BroClientAddr.sin_addr));


但是这么做,弹出框是乱码。是怎么回事?
CString str;
str.Format(_T("%s"),inet_ntoa(m_BroClientAddr.sin_addr));
AfxMessageBox(str);

[解决办法]
inet_ntoa
The Windows Sockets inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format.

char FAR * inet_ntoa (
  struct in_addr in  
);
 
Parameters
in 
[in] A structure that represents an Internet host address. 
Remarks
The inet_ntoa function takes an Internet address structure specified by the in parameter and returns an ASCII string representing the address in ".'' (dot) notation as in "a.b.c.d''. The string returned by inet_ntoa resides in memory that is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The data is guaranteed to be valid until the next Windows Sockets function call within the same thread, but no longer. Therefore, the data should be copied before another Windows Sockets call is made.

Return Values
If no error occurs, inet_ntoa returns a char pointer to a static buffer containing the text address in standard ".'' notation. Otherwise, it returns NULL. 

QuickInfo
  Windows NT: Yes
  Windows: Yes
  Windows CE: Use version 1.0 and later.
  Header: Declared in winsock2.h.
  Import Library: Link with ws2_32.lib.

See Also
inet_addr

 

热点排行