2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-05-04 15:17:04 +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...)
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void DoClientTCP(unsigned short Port);
|
|
|
|
void DoClientUDP(unsigned short Port);
|
|
|
|
void DoServerTCP(unsigned short Port);
|
|
|
|
void DoServerUDP(unsigned short Port);
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// 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
|
|
|
|
|
|
|
// TCP or UDP ?
|
2009-07-12 06:17:24 +08:00
|
|
|
char protocol;
|
2009-01-29 00:18:34 +08:00
|
|
|
std::cout << "Do you want to use TCP ('t') or UDP ('u') ? ";
|
2009-07-12 06:17:24 +08:00
|
|
|
std::cin >> protocol;
|
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
|
2009-07-12 06:17:24 +08:00
|
|
|
if (protocol == 't')
|
|
|
|
DoServerTCP(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2009-07-12 06:17:24 +08:00
|
|
|
DoServerUDP(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Run as a client
|
2009-07-12 06:17:24 +08:00
|
|
|
if (protocol == 't')
|
|
|
|
DoClientTCP(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2009-07-12 06:17:24 +08:00
|
|
|
DoClientUDP(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');
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|