2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <iomanip>
|
2009-07-12 06:17:24 +08:00
|
|
|
#include <iostream>
|
2009-04-09 17:24:26 +08:00
|
|
|
#include <cstdlib>
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Function prototypes
|
|
|
|
// (I'm too lazy to put them into separate headers...)
|
|
|
|
////////////////////////////////////////////////////////////
|
2012-03-12 02:10:37 +08:00
|
|
|
void doClient(unsigned short port);
|
|
|
|
void doServer(unsigned short port);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Choose a random port for opening sockets (ports < 1024 are reserved)
|
2009-07-12 06:17:24 +08:00
|
|
|
const unsigned short port = 2435;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Client or server ?
|
2009-07-12 06:17:24 +08:00
|
|
|
char who;
|
2009-01-29 00:18:34 +08:00
|
|
|
std::cout << "Do you want to be a server ('s') or a client ('c') ? ";
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cin >> who;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
if (who == 's')
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Run as a server
|
2012-03-12 02:10:37 +08:00
|
|
|
doServer(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Run as a client
|
2012-03-12 02:10:37 +08:00
|
|
|
doClient(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the user presses 'enter' key
|
|
|
|
std::cout << "Press enter to exit..." << std::endl;
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|