problem in IP address conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dulanjalie
    New Member
    • Feb 2008
    • 3

    problem in IP address conversion

    im implementating a over lay network. This is the routing table given as a .txt file.
    node no IP Port no
    1 987.000.333.444 5000
    2 987.111.333.444 7440
    3 222.222.111.111 7000

    This data is stored in t.txt and all data are separated by Tab space.
    I wrote a program to read the file and store data separately. It reads node no and port no correctly but not the IP. And it reads last IP correctly

    this is the programme
    [code=c]
    #include <stdio.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netdb.h> // gethostbyname()

    int main() {
    FILE *file;
    FILE *file1;
    char c[50]; /* make sure it is large enough to hold all the data! */
    char *d;
    char a,b,e,f;
    int flag=0;
    int m,n=0;
    int i=0;
    int j=0;

    struct Node_Infor
    {
    unsigned short Node_No[10];
    struct in_addr IP[10];
    //unsigned long IP[10];
    unsigned short port[10];
    }Node;



    file = fopen("t.txt", "r");
    if(file==NULL)
    {
    printf("Error: can't open file.\n");
    return 1;
    }
    else
    {
    printf("File opened successfully.\n ");

    while(feof(file )== 0)
    {

    c[i] = getc(file);

    if(c[i] == '\t'||c[i]== '\n')
    {
    c[i] = '\0';
    // printf("%s\n",c ); /* print out the string */
    i=0;
    flag++;

    switch(flag)
    {
    case 1:
    Node.Node_No[n]=atoi(c);
    break;
    case 2:
    inet_aton(c,&No de.IP[n]);
    printf("input c: %s\nNode.IP: %s\n",c,inet_nt oa(Node.IP[n]));
    break;
    case 3:
    Node.port[n]=atoi(c);
    flag=0;
    n++;
    break;
    }

    }
    else
    {
    i++;
    }
    }
    fclose(file1);
    fclose(file);

    for(m=0;m<n;m++ )
    {
    printf("%d\t%s\ t%d\n",Node.Nod e_No[m],inet_ntoa(Node .IP[m]),Node.port[m]);
    }
    return 0;
    }
    }[/code]
    Last edited by sicarie; Feb 24 '08, 11:56 PM. Reason: Code tags
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Read the IP address asa string and then store that to the in_addr structure.


    Raghuram

    Comment

    Working...