htons(???)怎么用啊?有 什么作用呢?我msdn没有装!

解决方案 »

  1.   

    htons
    The Windows Sockets htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).u_short htons(
      u_short hostshort  
    );
    Parameters
    hostshort 
    [in] 16-bit number in host byte order. 
    Res
    The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/IP networks.Return Values
    The htons function returns the value in TCP/IP network byte order.For example:
    int m_iPort;
    m_iPort=4001;
    struct sockaddr_in server;
             server.sin_family=AF_INET;
    server.sin_port=htons(m_iPort);
    server.sin_addr.s_addr=inet_addr(ip);
      

  2.   

    htons()  //host to net short
    htonl() // host to net longntohs()
    ntohl()网络字节序转换,简单的说如果在本地是一高位在前低位在后表示int,
    如果网络上的表示格式是相反的就要进行网络字节序转换