22
33std::string get_user_input (std::string prompt_message)
44{
5- std::cout << prompt_message << std::endl;
6- std::string user_input;
7- getline (std::cin, user_input);
5+ std::cout << prompt_message << std::endl;
6+ std::string user_input;
7+ getline (std::cin, user_input);
88
9- return user_input;
9+ return user_input;
1010}
1111
1212bool validate_input (int input)
1313{
14- if (input != 1 && input != 2 )
15- {
16- std::cout << " Invalid input" << std::endl;
17- return false ;
18- }
14+ if (input != 1 && input != 2 )
15+ {
16+ std::cout << " Invalid input" << std::endl;
17+ return false ;
18+ }
1919
20- return true ;
20+ return true ;
2121}
2222
2323int main () {
@@ -29,87 +29,86 @@ int main() {
2929 const int client_socket = 1 ;
3030 const int server_socket = 2 ;
3131
32-
33- // Pick the protocol
32+ // Pick protocol
3433 int socket_protocol = std::stoi (get_user_input (" Please pick a protocol. 1 for UDP, 2 for TCP:" ));
3534 if (!validate_input (socket_protocol))
3635 return invalid_protocol_return;
3736
38- // Pick client or server
39- int socket_type = std::stoi (get_user_input (" Please enter 1 for client or 2 for server:" ));
37+ // Pick client or server
38+ int socket_type = std::stoi (get_user_input (" Please enter 1 for client or 2 for server:" ));
4039 if (!validate_input (socket_type))
4140 return invalid_sockettype_return;
4241
43- // Pick port (and destination IP address if client)
44- u_short socket_port;
45- std::string destination_ip;
46-
47- // Client
48- if (socket_type == client_socket) {
49- socket_port = static_cast <u_short>(std::stoi (get_user_input (" Please pick a destination port:" )));
50- destination_ip = get_user_input (" Please enter destination ip address (example 127.0.0.1):" );
51- }
52-
53- // Server
54- else
55- {
56- socket_port = static_cast <u_short>(std::stoi (get_user_input (" Please enter a port on which to listen" )));
57- }
58-
59- // UDP Protocol
60- if (socket_protocol == udp_protocol)
61- {
62- // Client
63- if (socket_type == client_socket)
64- {
65- UDPClient client (socket_port, destination_ip);
66- client.send_message (get_user_input (" Please enter your message to send" ) + " \n " );
67- }
68- // Server
69- else if (socket_type == server_socket)
70- {
71- UDPServer server (socket_port);
72- int bind_status = server.socket_bind ();
73-
74- // Return if there is an issue binding
75- if (bind_status)
76- {
77- return bind_status;
78- }
79- server.listen ();
80- }
81-
82- }
83-
84- // TCP Protocol
85- else if (socket_protocol == tcp_protocol)
86- {
87- // Client
88- if (socket_type == client_socket)
89- {
90- TCPClient client (socket_port, destination_ip);
91- int connection_status = client.make_connection ();
42+ // Pick port (and destination IP address if client)
43+ u_short socket_port;
44+ std::string destination_ip;
45+
46+ // Client
47+ if (socket_type == client_socket) {
48+ socket_port = static_cast <u_short>(std::stoi (get_user_input (" Please pick a destination port:" )));
49+ destination_ip = get_user_input (" Please enter destination ip address (example 127.0.0.1):" );
50+ }
51+
52+ // Server
53+ else
54+ {
55+ socket_port = static_cast <u_short>(std::stoi (get_user_input (" Please enter a port on which to listen" )));
56+ }
57+
58+ // UDP Protocol
59+ if (socket_protocol == udp_protocol)
60+ {
61+ // Client
62+ if (socket_type == client_socket)
63+ {
64+ UDPClient client (socket_port, destination_ip);
65+ client.send_message (get_user_input (" Please enter your message to send" ) + " \n " );
66+ }
67+ // Server
68+ else if (socket_type == server_socket)
69+ {
70+ UDPServer server (socket_port);
71+ int bind_status = server.socket_bind ();
72+
73+ // Return if there is an issue binding
74+ if (bind_status)
75+ {
76+ return bind_status;
77+ }
78+ server.listen ();
79+ }
80+
81+ }
82+
83+ // TCP Protocol
84+ else if (socket_protocol == tcp_protocol)
85+ {
86+ // Client
87+ if (socket_type == client_socket)
88+ {
89+ TCPClient client (socket_port, destination_ip);
90+ int connection_status = client.make_connection ();
9291
9392 // Return if there is an issue binding
94- if (connection_status)
95- {
96- return connection_status;
97- }
98- client.send_message (get_user_input (" Please enter your message to send" ) + " \n " );
99- }
100- // Server
101- else if (socket_type == server_socket)
102- {
103- TCPServer server (socket_port);
104- int bind_status = server.socket_bind ();
93+ if (connection_status)
94+ {
95+ return connection_status;
96+ }
97+ client.send_message (get_user_input (" Please enter your message to send" ) + " \n " );
98+ }
99+ // Server
100+ else if (socket_type == server_socket)
101+ {
102+ TCPServer server (socket_port);
103+ int bind_status = server.socket_bind ();
105104
106105 // Return if there is an issue binding
107- if (bind_status)
108- {
109- return bind_status;
110- }
111- }
112- }
113-
114- return 0 ;
106+ if (bind_status)
107+ {
108+ return bind_status;
109+ }
110+ }
111+ }
112+
113+ return 0 ;
115114}
0 commit comments