0

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.");
    }
}
Starfighter911
  • 197
  • 2
  • 3
  • 9
ChrisB92
  • 45
  • 1
  • 7
  • 1
    If you really mean to print an arbitrary string to test output, use some innocuous text like the word "Test". It's not nice to teach your computer to swear, and it may offend some viewers here too. – BoltClock Nov 29 '11 at 23:40
  • @BoltClock: The INTERCAL compiler will refuse to compile your program if you're not polite enough in your code (with sufficiently many PLEASEs), although I don't know if it responds negatively to obscenities. – Adam Rosenfield Nov 29 '11 at 23:47

1 Answers1

5

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.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 1
    Thanks man, fflushing stdout worked perfectly, now I can test my program finally =D. And to those complaining about my clear fustration, apologies =P – ChrisB92 Nov 29 '11 at 23:59