应用程序添加静态IP路由,默认网关? [英] Application to add a static IP route, default gateway?

查看:124
本文介绍了应用程序添加静态IP路由,默认网关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取本地IP地址后,我想将IP设置为网关.但是代码有问题,如下所示:

After getting my local IP address, I want set my IP to be the gateway. But there''s something wrong the code, as follow:

#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!

推荐答案

在您的代码中,您正在执行:

路由-p添加0.0.0.0掩码0.0.0.0 ip

通过命令提示符.从评论看来,我们希望看到它:

路由-p添加0.0.0.0掩码0.0.0.0 192.168.0.1

已执行.

从CheckIP返回计算机的IP地址,然后格式化发送到系统的字符串以包括IP地址,而不是字母i和p,它可能会开始工作.

干杯,

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


这篇关于应用程序添加静态IP路由,默认网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆