将IP地址从int更改为字符串的问题

时间:2016-06-27 01:17:10

标签: c string network-programming ipv6 ipv4

我正在尝试将int数组格式的IP地址(当前为IPv4,但识别IPv6)转换为格式化字符串。我正在研究Ubuntu Linux。

我区分IPv4和IPv6地址,然后尝试将int数组转换为字符串。

这是我正在使用的代码:

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>

int main(void)
{
    int i;
    unsigned int val32;
    int ma_ip[4];
    char m_ip[4];
    char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
    int no = 0;
    char *token;
    char s3[] = "10.1.35.1";
    /* just example. test only one ip address.. */
    for(i = 0; i < 1; i++)
    {
        char *mm = strstr(s3, ":");
        if( *mm != NULL)
        {
             token = strtok(s3, ":");
             while(token != NULL)
             {
                 token = strtok(NULL, ":");
                 no++;
             }
             if(no >= 2 && no <= 7)
                 printf("\nthis is ipv6\n");
             else
                 printf("\nwrong ipv6\n");
         }
         else
         {
             token = strtok(s3, ".");
             while(token != NULL)
             {
                 token = strtok(NULL, ".");
                 no++;
             }
             if(no == 4)
             {
                 printf("\nthis is ipv4.\n");
                 val32 = inet_addr(s3)
                 ma_ip[i] = val32;
             }
             else
                 printf("\nwrong ipv4.\n")
         }
         inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
         printf("\nipaddr = %s\n", ipaddr);
         strcpy(&m_ip[0], ipaddr);
         printf("\nafter strcpy = %s\n", m_ip[0]);
    }
}

此输出错误:

ipaddr = 0.0.0.10

实际应该是:

ipaddr = 10.1.35.1

我在printf

之后的最后strcpy内也收到此错误
  

格式'%s'需要类型为'char *'的参数,但参数2的类型为'int'错误!**

为什么会发生这种情况,我该如何解决?

3 个答案:

答案 0 :(得分:0)

m_ip[0]指向第一个元素的位置。如果你想打印字符串,请尝试使用m_ip,即数组名称或&m_ip[0],它基本上传递了第一个元素的地址,可以解释为pointer并且基本上导致分段故障。使用m_ip打印(但仍然只有0.0.0.10)。

您的计划中的另一点是,您应该在mm操作后将NULLstrstr进行比较。

对于网络地址操作,我建议您查看this question

答案 1 :(得分:0)

我对你的节目有一些疑问。 你正在转换一个IP字符串

 char s3[] = "10.1.35.1";

到一个整数并再次回到字符串??

我认为这就是你需要的

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));

// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);

printf("%s\n", str); // prints "192.0.2.33"

关于strcpy之后的最后一个printf,它应该是

printf("\nafter strcpy = %s\n", m_ip);

答案 2 :(得分:-1)

这是一个可行的版本。你的代码的问题是,strtok()正在修剪字符串's3'。在时间n == 4时,字符串只剩下“10”。

 #include <stdio.h>
 #include <string.h>
 #include <arpa/inet.h>
 #include <malloc.h>

int main(void)
{
    int i;
    unsigned int val32;
    int ma_ip[4];
    char m_ip[4];
    char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
    int no = 0;
    char *token;
    char s3[] = "10.1.35.1";
    char *s4 = malloc(strlen(s3)+1);   
    strcpy(s4, s3);               //You can save the original string.
    /* just example. test only one ip address.. */
    for(i = 0; i < 1; i++)
    {
        char *mm = strstr(s3, ":");
        if( mm != NULL)
        {
             token = strtok(s3, ":");
             while(token != NULL)
             {
                 token = strtok(NULL, ":");
                 no++;
             }
             if(no >= 2 && no <= 7)
                 printf("\nthis is ipv6\n");
             else
                 printf("\nwrong ipv6\n");
         }
         else
         {
             token = strtok(s3, ".");
             while(token != NULL)
             {
                 token = strtok(NULL, ".");
                 no++;
             }
             if(no == 4)
             {
                 printf("\nthis is ipv4.\n");
                 val32 = inet_addr(s4);  //use the intact string s4, instead of s3.
                 ma_ip[i] = val32;
             }
             else
                 printf("\nwrong ipv4.\n");
         }
         inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
         printf("\nipaddr = %s\n", ipaddr);
         strcpy(&m_ip[0], ipaddr);
         printf("\nafter strcpy = %s\n", m_ip);
    }
}