Intentionally easy!
- The full program file is right here
- The protocol—informally, the set of rules both client and server must follow if they're to speak with one another—is simple.
- The protocol here is...
- The client connects (e.g. "rings" the service's phone at a particular "extension", and waits for the server to "pick up")
- The client says nothing.
- The server speaks by publishing the current time into its own end of the connection and then hangs up.
- The client ingests the published text (understood, by protocol, to be just one line), publishes it to the console, and then itself hangs up.
int main(int argc, char *argv[]) {
int clientSocket = createClientSocket("myth7.stanford.edu", 12345);
if (clientSocket == kClientSocketError) {
cerr << "Time server could not be reached" << endl;
cerr << "Aborting" << endl;
return 1;
}
sockbuf sb(clientSocket);
iosockstream ss(&sb);
string timeline;
getline(ss, timeline);
cout << timeline << endl;
return 0;
}
- We'll discuss the implementation of createClientSocket on Monday, but it's okay to just view it as a built-in that sets up a bidirectional pipe between the client and the server running on the specified host and port number.