C编程 - 命令行Arg - 尝试获取IP地址

Chr*_*B92 0 c ip argv argc

我们的任务是拥有服务器 - 客户端模型....

我们应该检查命令行arg,如果没有(argc = 1),我们设置为服务器.否则我们使用argv [1]来设置客户端套接字......

如果我只是为参数使用随机性似乎运行正常,但是当我尝试将地址放入1.2.3.4时,它不会给出任何输出,只是启动程序而什么都不做.

我想我的问题是以IP地址的形式处理命令行参数的最佳方法.

这是我的主要功能.

int main( int argc, char *argv[] )
{

    printf("%i",argc);
    if(argc==1)
    {
            printf("Welcome to the ZOG Virtual Probe Game!  You have choosen to take the role of a server.");
            printf("\nPlease wait for an opponent to connect.");
            runServer();
    }else if(argc==2)
    {
            printf("Welcome to the ZOG Virtual Probe Game!  You are now connecting to specified IP.");
            runClient(argv[1]);
    }else
    {
            printf("You used an invalid command line argument. You can input an IP address or leave no command arg to host a game.");
    }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*ell 5

在printf之后添加fflush.您的欢迎消息正在缓冲.将换行符放在要打印的字符串末尾可能就足够了.