Skip to content

Commit bacf1c5

Browse files
Minor formatting update
1 parent c986a8d commit bacf1c5

File tree

3 files changed

+87
-87
lines changed

3 files changed

+87
-87
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ Really this library is not that big of a deal and most sources suggest using a l
2121
- `g++ main.cpp -o main`
2222
- `./main`
2323

24-
- To build and run the test application on Windows, use Visual Studio or install it, open the "Developer Command Prompt" and run:
24+
- To build and run the test application on Windows either use Visual Studio directly or open the `Developer Command Prompt` and run:
2525
- `cd src`
2626
- `cl main.cpp`
27-
- Double click on `main.exe`
27+
- `start main.exe`
2828

2929
## Functionality
3030
`main.cpp` is a simple functioning example (not all errors are handled gracefully).
@@ -49,7 +49,7 @@ If server:
4949

5050

5151

52-
## To Use
52+
## To Use
5353
simple_cpp_sockets.h contains classes for UDP and TCP servers and clients.
5454

5555
An example looks like:

src/main.cpp

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
std::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

1212
bool 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

2323
int 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
}

src/simple_cpp_sockets.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef int SOCKET;
2020
#endif
2121

2222

23+
// Possible errors
2324
const int socket_bind_err = 3;
2425
const int socket_accept_err = 4;
2526
const int connection_err = 5;
@@ -49,7 +50,7 @@ class Socket {
4950
};
5051

5152
#ifdef WIN32
52-
// Initialize s_count here on windows
53+
// Initialize s_count on windows
5354
int Socket::s_count{ 0 };
5455
#endif
5556

@@ -208,6 +209,7 @@ int TCPClient::make_connection()
208209
return connection_err;
209210
}
210211
std::cout << "connected" << std::endl;
212+
211213
return 0;
212214
}
213215

@@ -292,4 +294,3 @@ int TCPServer::socket_bind()
292294
#endif
293295
return 0;
294296
}
295-

0 commit comments

Comments
 (0)