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
|
|
|
|
|
|
|
|
2012-03-12 02:10:37 +08:00
|
|
|
void runTcpServer(unsigned short port);
|
|
|
|
void runTcpClient(unsigned short port);
|
|
|
|
void runUdpServer(unsigned short port);
|
|
|
|
void runUdpClient(unsigned short port);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
2010-03-23 17:39:43 +08:00
|
|
|
// Choose an arbitrary port for opening sockets
|
|
|
|
const unsigned short port = 50001;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-23 17:39:43 +08:00
|
|
|
// TCP, UDP or connected UDP ?
|
2009-07-12 06:17:24 +08:00
|
|
|
char protocol;
|
2010-03-23 17:39:43 +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;
|
2010-03-23 17:39:43 +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
|
|
|
|
2010-03-23 17:39:43 +08:00
|
|
|
if (protocol == 't')
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-03-23 17:39:43 +08:00
|
|
|
// Test the TCP protocol
|
|
|
|
if (who == 's')
|
2012-03-12 02:10:37 +08:00
|
|
|
runTcpServer(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2012-03-12 02:10:37 +08:00
|
|
|
runTcpClient(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-23 17:39:43 +08:00
|
|
|
// Test the unconnected UDP protocol
|
|
|
|
if (who == 's')
|
2012-03-12 02:10:37 +08:00
|
|
|
runUdpServer(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2012-03-12 02:10:37 +08:00
|
|
|
runUdpClient(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;
|
|
|
|
}
|