This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ccs 7 中地址转换函数比如inet_pton,inet_addr,在哪儿可以找到啊



ccs 7 中地址转换函数比如inet_pton,inet_addr,在哪儿可以找到啊

  • 头文件:
    windows下:
    #include <WS2tcpip.h>
    linux下:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include<arpa/inet.h>
  • 如果你用的是CC32xx芯片,请用下面的参考代码:
    sock_fd = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);

    s_addr.sin_family = SL_AF_INET;
    s_addr.sin_addr.s_addr = sl_Htonl(dest_ip);
    s_addr.sin_port = sl_Htons(port);

    ret = sl_Connect(sock_fd, ( SlSockAddr_t *)&s_addr, sizeof(s_addr));

    如上,host-to-network long and host-to-network short

    如果是inet_addr, 用下面的函数转换吧:
    static uint32_t ip_string_to_in(char *addr)
    {
    char *s1, *s2, *s3, *s4;
    uint32_t ip = 0;

    s1 = strtok(addr,".,"); /* first pass */
    s2 = strtok(NULL,".,");
    s3 = strtok(NULL,".,");
    s4 = strtok(NULL,".,");

    ip = SL_IPV4_VAL((const char *)atoi(s1), (const char *)atoi(s2),
    (const char *)atoi(s3), (const char *)atoi(s4));

    return ip;
    }
  • cc3220确实只能用htonl来转换地址,