Question

Our assignment is to have a server-client model....

We're supposed to check for a command line arg, if there is not one(argc = 1), we set up as a server. otherwise we use the argv[1] to setup the client socket...

It seems to run fine if I just use randomness for the argument, but when I try to put an address in, 1.2.3.4 for example, it doesnt give any output, just starts the program and does nothing.

I guess my question is what's the best way to handle a command line argument in the form of an IP address.

Here is my main function.

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.");
    }
}
Was it helpful?

Solution

Add an fflush after the printf. Your welcome message is being buffered. It may be sufficient to just put a newline at the end of the strings being printed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top