0

我们的任务是建立一个服务器-客户端模型......

我们应该检查命令行 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.");
    }
}
4

1 回答 1

5

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

于 2011-11-29T23:33:20.697 回答