在数据包中的IP地址是ULONG,他在内存中是怎么排放的???

能动力 2003-08-22 04:26:34
是不是按照:
例如:IP是:218.13.2.8
那么分成四个字节,第一个是218,第二个是13......,我这样理解对不对?????
...全文
71 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kunjun_oy 2003-08-27
  • 打赏
  • 举报
回复
数的表示有两种,一种是网络字节顺序,另一种是主机字节顺序。
在内存中是主机字节顺序,一个long数0x01020304在内存中是0x04,0x03,0x02,0x01
而网络传送的地址要求是网络字节顺序,上面的数对应的是0x01,0x02,0x03,0x04
主机字节顺序到网络字节顺序的转换函数是htons,htonl
windbells 2003-08-23
  • 打赏
  • 举报
回复
在windows下应该是8,2,13,218吧,linux下应该还是218,13,2,8
zhouyong0371 2003-08-23
  • 打赏
  • 举报
回复
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] Structure that represents an Internet host address.
in_addr
The Windows Sockets in_addr structure represents a host by its Internet address.

struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
};
Members
S_un_b
Address of the host formatted as four u_chars.
S_un_w
Address of the host formatted as two u_shorts.
S_addr
Address of the host formatted as a u_long.

zhouyong0371 2003-08-23
  • 打赏
  • 举报
回复
你的理解是不正确的,ULONG的IP只是一个数值,具体怎么存放我也不知道,他和字符串形式的IP需要转换才能得到。
void CGetNameFromIPDlg::OnGet()
{
// TODO: Add your control notification handler code here
char m_strAddr[15];

m_HostAddr.GetWindowText(m_strAddr,15);

WSAData wsaData;
DWORD Addr;
//初始化套接字
WSAStartup(MAKEWORD(2,2),&wsaData);

//inet_addr()函数返回一个指向honstent的结构体地址
Addr = inet_addr(m_strAddr); //将字符串形式转换。

HOSTENT * hos;

hos=gethostbyaddr((char*)&Addr,sizeof(Addr),PF_INET);

if(hos!=NULL)
{
MessageBox(hos->h_name,"The host using this IP");
}
else
{
MessageBox("Can't get the host name using this IP.","Failure");
}
}
VCP4 2003-08-22
  • 打赏
  • 举报
回复
好像是一个累加的值。
mme 2003-08-22
  • 打赏
  • 举报
回复
不是这样的

18,356

社区成员

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

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