#include "stdafx.h"
#include <Winsock2.h>
#include <stdio.h>
#pragma comment (lib,"WS2_32.lib")int main(int argc, char* argv[])
{
struct sockaddr_in src;
src.sin_addr.S_un.S_addr=3232235737; 
printf("%s\n",inet_ntoa(src.sin_addr));
return 0;}
最后我得到的怎么是217.0.168.192,正确的应该是193.168.0.217
请大家帮忙!

解决方案 »

  1.   

    呵呵,使网络地址字节顺序的问题。
    有函数可以提供转换,不过忘了,你可以自己写:
    union {
        unsigned long l;
        unsigned char c[4];
    }temp
    temp=src.sin_addr.S_un.S_addr;
    unsigned char ctemp;
    ctemp=temp.c[0];
    temp.c[0]=temp.c[3];
    temp.c[3]=ctemp;
    ctemp=temp.c[1];
    temp.c[1]=temp.c[2];
    temp.c[2]=ctemp;
      

  2.   

    错了
    第五行temp.l=src.sin_addr.S_un.S_addr;
      

  3.   

    // fi.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include <Winsock2.h>
    #include <stdio.h>
    #pragma comment (lib,"WS2_32.lib")int main(int argc, char* argv[])
    {
    int a,b,c,d;
    struct sockaddr_in src;
    src.sin_addr.S_un.S_addr=3232235737; 
    printf("%s\n",inet_ntoa(src.sin_addr));
    d=src.sin_addr.S_un.S_un_b.s_b1;
    c=src.sin_addr.S_un.S_un_b.s_b2;
    b=src.sin_addr.S_un.S_un_b.s_b3;
    a=src.sin_addr.S_un.S_un_b.s_b4;
    printf("%d.%d.%d.%d\n",a,b,c,d);
    return 0;}
    我是这样改的
      

  4.   

    src.sin_addr.S_un.S_addr=3232235737; 
    改为:
    src.sin_addr.S_un.S_addr=htonl(3232235737);