为什么sin_addr 在in_addr 结构中? [英] Why is sin_addr inside the structure in_addr?

查看:29
本文介绍了为什么sin_addr 在in_addr 结构中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的疑问与 UNIX 中套接字的以下结构有关:

My doubt is related to the following structure of sockets in UNIX :

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

这里的成员 sin_addrstruct in_addr 类型.

Here the member sin_addr is of type struct in_addr.

但我不明白为什么有人想要这样做,因为所有 struct inaddr 都有:

But I don't get why someone would like to do that as all struct inaddr has is :

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};

所有in_addr 都只有一个成员s_addr.为什么我们不能有这样的东西:

All in_addr has is just one member s_addr. Why cannot we have something like this :

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    unsigned long    s_addr ; 
    char             sin_zero[8];  // zero this if you want to
};

推荐答案

struct in_addr 有时与此非常不同,这取决于您使用的系统.例如,在 Windows 上:

struct in_addr is sometimes very different than that, depending on what system you're on. On Windows for example:

typedef 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;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

唯一的要求是它包含一个成员s_addr.

The only requirement is that it contain a member s_addr.

这篇关于为什么sin_addr 在in_addr 结构中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆