0
votes

Maybe I haven't fully woken up, but I'm going through Beej's network programming guide on datagram sockets

https://beej.us/guide/bgnet/html/multi/clientserver.html#simpleserver

and I'm confused by an initial piece in the client side. No command line arguments are specified in the demonstration, however, it looks like there are multiple calls in the code to check for the argument count. For instance, in the first if statement there's a check for the count to be 3, if not the program exits.

 if (argc != 3) {
    fprintf(stderr,"usage: talker hostname message\n");
    exit(1);
}

Could someone explain what's going on here?

Thanks for any help in advance.

2

2 Answers

2
votes

He's expecting the program to be invoked with two arguments, "hostname" and "message", which would be argv[1], and argv[2] respectively, with argv[0] reserved for the name of the executable, ./talker or the like.

The "talker" program sends a message to the "listener" program, and it needs to know what to send (message) and where to send it (hostname).

1
votes

I think you're reading the wrong code. The talker program does indeed take the usual argc and argv arguments to main().