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
|
|
|
|
|
|
|
|
2010-03-23 17:39:43 +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')
|
|
|
|
RunTcpServer(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2010-03-23 17:39:43 +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')
|
|
|
|
RunUdpServer(port);
|
2009-01-29 00:18:34 +08:00
|
|
|
else
|
2010-03-23 17:39:43 +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;
|
|
|
|
}
|