qqjianyue代码工

砌C/C++代码
posts - 6, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Socket地址结构

Posted on 2011-01-09 16:42 Qzi 阅读(324) 评论(0)  编辑 收藏 引用
/* Description of data base entry for a single host.  */
struct hostent
{
  char *h_name;            /* Official name of host.  */
  char **h_aliases;        /* Alias list.  */
  int h_addrtype;        /* Host address type.  */
  int h_length;            /* Length of address.  */
  char **h_addr_list;        /* List of addresses from name server.  */
#if defined __USE_MISC || defined __USE_GNU
# define    h_addr    h_addr_list[0] /* Address, for backward compatibility.*/
#endif
};

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;            /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };

Get ip string:
char* getIpStr(const char* domainName){
    struct hostent *hp;
    struct in_addr in;
    hp=gethostbyname(domainName);
    memcpy(&in.s_addr, hp->h_addr,4);
    return inet_ntoa(in);
}




只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理