2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
2022-07-05 00:20:58 +08:00
|
|
|
#include <iostream>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
void runTcpServer(unsigned short port);
|
|
|
|
void runTcpClient(unsigned short port);
|
|
|
|
void runUdpServer(unsigned short port);
|
|
|
|
void runUdpClient(unsigned short port);
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Choose an arbitrary port for opening sockets
|
|
|
|
const unsigned short port = 50001;
|
|
|
|
|
|
|
|
// TCP, UDP or connected UDP ?
|
|
|
|
char protocol;
|
2014-11-17 16:44:46 +08:00
|
|
|
std::cout << "Do you want to use TCP (t) or UDP (u)? ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> protocol;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Client or server ?
|
|
|
|
char who;
|
2014-11-17 16:44:46 +08:00
|
|
|
std::cout << "Do you want to be a server (s) or a client (c)? ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> who;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
if (protocol == 't')
|
|
|
|
{
|
|
|
|
// Test the TCP protocol
|
|
|
|
if (who == 's')
|
|
|
|
runTcpServer(port);
|
|
|
|
else
|
|
|
|
runTcpClient(port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Test the unconnected UDP protocol
|
|
|
|
if (who == 's')
|
|
|
|
runUdpServer(port);
|
|
|
|
else
|
|
|
|
runUdpClient(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
}
|