Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
After getting my local IP address, I want set my IP to be the gateway. But there's something wrong the code, as follow:

C
#include<stdio.h>
#include<conio.h>
#include<winsock2.h>
#include<windows.h>
#include<stdlib.h>
#pragma comment(lib"ws2_32.lib")

// this function is to get local ip address
void CheckIP(void) //define CheckIP() function to get IP

    WSADATA wsaData;
    char name[255];
    char *ip;
    PHOSTENT hostinfo;

    //instruct MAKEWORD() get Winsock vision
    if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )
    {

        if( gethostname ( name, sizeof(name)) == 0) {

            if((hostinfo = gethostbyname(name)) != NULL) {

                ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);

                printf("\n\n    Your IP IS:%s\n\n",ip); 
            }
        }
        WSACleanup( );     
    }
}
int main(void)
{
    CheckIP();
    system("route -p add 0.0.0.0 mask 0.0.0.0 ip");
    // Maybe it's wrong, but how can correct it ? help!
    /* for example: if the ip that I get is 192.168.0.1. system   ("route -p add 0.0.0.0 mask 0.0.0.0 192.168.0.1 ")*/

    getch();

    return 0;
}


Could you help me correct it ? Show your code after you corrected it.
Many thanks!
Posted
Updated 10-Jun-10 19:43pm
v6

1 solution

From your code you're executing:

route -p add 0.0.0.0 mask 0.0.0.0 ip

through a command prompt. From the comments it looks like to expect to see:

route -p add 0.0.0.0 mask 0.0.0.0 192.168.0.1

executed.

Return the IP address of the computer from CheckIP and then format the string you send to system to include the IP address, not the letters i and p and it might start working.

Cheers,

Ash
 
Share this answer
 
Comments
eagle_chen 11-Jun-10 1:51am    
Thanks. But When I add "return ip;", it's stil.Could you please show me your code after it is corrected? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900