From b565be4e53376beb977aa660390f563f80c91d71 Mon Sep 17 00:00:00 2001 From: "masesm5@yahoo.com" Date: Fri, 4 Dec 2020 15:20:59 -0800 Subject: [PATCH 1/9] v0.3 --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9015411..e4f8fe7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5) project(easy-socket VERSION 0.2.0) -option(EASY_SOCKET_BUILD_TESTS "Turn on to build tests." ON) +option(BUILD_TESTS "Turn on to build tests." ON) set(PROJECT_COMPATIBILITY AnyNewerVersion) add_library(${PROJECT_NAME} INTERFACE) @@ -58,8 +58,10 @@ install( set(${PROJECT_NAME}_VERSION ${PROJECT_VERSION} CACHE INTERNAL "") -if(EASY_SOCKET_BUILD_TESTS) +if(BUILD_TESTS) # Add sub directories - add_subdirectory(test/test-server) - add_subdirectory(test/test-client) + add_subdirectory(test/test-tcp-server) + add_subdirectory(test/test-tcp-client) + add_subdirectory(test/test-udp-sender) + add_subdirectory(test/test-udp-receiver) endif() From c9f23f7b052c3e8dd18641a1a421e97c7ab365bb Mon Sep 17 00:00:00 2001 From: "masesm5@yahoo.com" Date: Fri, 4 Dec 2020 15:22:42 -0800 Subject: [PATCH 2/9] v0.3 initial --- README.md | 107 ++++++++++++-- easy-socket.sln | 24 +++- include/masesk/EasySocket.hpp | 87 ++++++++++- test/test-client/CMakeLists.txt | 5 - test/test-server/CMakeLists.txt | 5 - test/test-tcp-client/CMakeLists.txt | 5 + test/test-tcp-client/tcp_client.cpp | 21 +++ .../test-client.vcxproj | 3 +- .../test-client.vcxproj.filters | 2 +- test/test-tcp-server/CMakeLists.txt | 5 + test/test-tcp-server/RecieverUDP.cpp | 0 test/test-tcp-server/tcp_server.cpp | 14 ++ .../test-server.vcxproj | 3 +- .../test-server.vcxproj.filters | 2 +- test/test-udp-receiver/CMakeLists.txt | 5 + .../test-udp-receiver.vcxproj | 135 ++++++++++++++++++ .../test-udp-receiver.vcxproj.filters | 22 +++ .../udp_receiver.cpp} | 2 +- test/test-udp-sender/CMakeLists.txt | 5 + test/test-udp-sender/test-udp-sender.vcxproj | 135 ++++++++++++++++++ .../test-udp-sender.vcxproj.filters | 22 +++ .../udp_sender.cpp} | 3 +- 22 files changed, 574 insertions(+), 38 deletions(-) delete mode 100644 test/test-client/CMakeLists.txt delete mode 100644 test/test-server/CMakeLists.txt create mode 100644 test/test-tcp-client/CMakeLists.txt create mode 100644 test/test-tcp-client/tcp_client.cpp rename test/{test-client => test-tcp-client}/test-client.vcxproj (98%) rename test/{test-client => test-tcp-client}/test-client.vcxproj.filters (95%) create mode 100644 test/test-tcp-server/CMakeLists.txt create mode 100644 test/test-tcp-server/RecieverUDP.cpp create mode 100644 test/test-tcp-server/tcp_server.cpp rename test/{test-server => test-tcp-server}/test-server.vcxproj (98%) rename test/{test-server => test-tcp-server}/test-server.vcxproj.filters (95%) create mode 100644 test/test-udp-receiver/CMakeLists.txt create mode 100644 test/test-udp-receiver/test-udp-receiver.vcxproj create mode 100644 test/test-udp-receiver/test-udp-receiver.vcxproj.filters rename test/{test-server/Server.cpp => test-udp-receiver/udp_receiver.cpp} (80%) create mode 100644 test/test-udp-sender/CMakeLists.txt create mode 100644 test/test-udp-sender/test-udp-sender.vcxproj create mode 100644 test/test-udp-sender/test-udp-sender.vcxproj.filters rename test/{test-client/Client.cpp => test-udp-sender/udp_sender.cpp} (76%) diff --git a/README.md b/README.md index b651ff0..3253afe 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,9 @@ masesk::EasySocket easySocket; //or using namespace masesk; ``` ### Usecase - * EasySocket wraps the code for the server and client, so that a single header can be used to initialize and start a server or a client. +* EasySocket supports both UDP and TCP. [Refer to this StackOverflow answer for difference between the two and when to use each one.](https://stackoverflow.com/questions/1099672/when-is-it-appropriate-to-use-udp-instead-of-tcp#answer-1099734) +#### TCP ```cpp //server example @@ -43,22 +44,76 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListen("test", 8080, &handleData); + socketManager.socketListenTCP("test", 8080, &handleData); return 0; } ``` ```cpp // client example + #include #include #include using namespace std; using namespace masesk; + int main() { EasySocket socketManager; - socketManager.socketConnect("test", "127.0.0.1", 8080); - socketManager.socketSend("test", "Hello from client!"); + socketManager.socketConnectTCP("test", "127.0.0.1", 8080); + string userInput; + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + socketManager.socketSendTCP("test", userInput); + } + socketManager.closeConnection("test"); + return 0; +} +``` + +#### UDP + +```cpp +// receiver example +#include +#include +using namespace std; +using namespace masesk; + +void handleData(const std::string &data) { + cout << "Client sent: " + data << endl; +} + +int main() { + EasySocket socketManager; + socketManager.socketListenUDP("test", 9090, handleData); + return 0; +} +``` + +```cpp +// send example +#include +#include +#include +using namespace std; +using namespace masesk; + +int main() { + EasySocket socketManager; + string userInput; + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + socketManager.socketSendUDP("127.0.0.1", 9090, userInput); + } socketManager.closeConnection("test"); return 0; } @@ -66,35 +121,59 @@ int main() { ### Functions +### TCP + #### Server Functions -* `void socketListen(std::string channelName, int port, std::function callback);` +* `void socketListenTCP(std::string channelName, std::uint16_t port, std::function callback);` * channelName: string identifier of channel * port: integer value of port used on server side (eg. 8080) * function: pointer of function that will be called to handle the data when the server recieves data from the client +* `void closeConnection(std::string channelName)` - close socket port on server + * channelName: string identifier of channel + #### Client Functions -* `void socketConnect(std::string channelName, std::string ip, int port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. +* `void socketConnectTCP(std::string channelName, std::string ip, std::uint16_t port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. * channelName - string identifier of channel * ip - string for where the server resides (eg. 127.0.0.1 for local) * port - integer value of port used on server side (eg. 8080) -* `void socketSend(std::string channelName, std::string data)` - send data to server based on channel name +* `void socketSendTCP(std::string channelName, std::string data)` - send data to server based on channel name * channelName: string identifier of channel * data: data to be sent through to the server on given channel * `void closeConnection(std::string channelName)` - close connection with server using channel name * channelName: string identifier of channel + +### UDP + +#### Receiver Functions + +* `void socketListenUDP(std::string channelName, std::uint16_t port, std::function callback);` + * channelName: string identifier of channel + * port: integer value of port used on server side (eg. 9090) + * function: pointer of function that will be called to handle the data when the server recieves data from the client + +#### Sender Functions +* `void socketSendUDP(std::string ip, std::uint16_t port, std::string data)` - send data to server based on channel name + * ip - string for where the server resides (eg. 127.0.0.1 for local) + * port - integer value of port used on server side (eg. 9090) + * data: data to be sent through to the server on given channel + ## Example Check `test/test-server` and `test/test-client` for a working client and server example running locally. ### Build Tests on Windows 1. Open `easy-socket.sln` in Visual Studio 2017 -2. Right click on `test-client`, select `properites`, and change `Windows SDK Version` to your installed 10.x -3. Right click on `test-server`, select `properites`, and change `Windows SDK Version` to your installed 10.x -4. Right click on ```Solution 'easy-socket' ``` and select `Rebuild entire solution`. -5. Select desired `Configuration` (Debug/Release) and `Platform` (x64/Win32) from the top-bar dropdowns next to the start button. -6. Executables will be available at `[x64 or Win32]/[Debug or Release]` +2. Right click on `test-tcp-client`, select `properites`, and change `Windows SDK Version` to your installed 10.x +3. Right click on `test-tcp-server`, select `properites`, and change `Windows SDK Version` to your installed 10.x +4. Right click on `test-udp-receiver`, select `properites`, and change `Windows SDK Version` to your installed 10.x +5. Right click on `test-udp-sender`, select `properites`, and change `Windows SDK Version` to your installed 10.x +6. Right click on ```Solution 'easy-socket' ``` and select `Rebuild entire solution`. +7. Select desired `Configuration` (Debug/Release) and `Platform` (x64/Win32) from the top-bar dropdowns next to the start button. +8. Right click on `Solution easy-socket` and select two projects to start: either `test-tcp-server` and `test-tcp-client` OR `test-udp-receiver` and `test-udp-sender` +9. Executables will be available at `[x64 or Win32]/[Debug or Release]` ### Build Tests on Linux @@ -116,4 +195,6 @@ cmake .. make ``` -Executables will be available at `build/tests/test-client/client` and `build/tests/test-client/server` +Executables will be available at `build/tests/test-tcp-client/tcp_client` and `build/tests/test-tcp-client/tcp_server` and `build/tests/test-udp-receiver/udp_receiver` and `build/tests/test-udp-sender/udp_sender` + +Disable building tests: `cmake -D BUILD_TESTS=OFF ..` diff --git a/easy-socket.sln b/easy-socket.sln index 0d3dca9..4c6ab85 100644 --- a/easy-socket.sln +++ b/easy-socket.sln @@ -5,9 +5,13 @@ VisualStudioVersion = 15.0.28010.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "easy-socket", "easy-socket.vcxproj", "{EF42A18B-14CB-46AD-A34C-30B35EF0586C}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-client", "test\test-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-client", "test\test-tcp-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-server", "test\test-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-server", "test\test-tcp-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-receiver", "test\test-udp-receiver\test-udp-receiver.vcxproj", "{EE13023D-664D-4CEA-988D-35BBA91F49C4}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-sender", "test\test-udp-sender\test-udp-sender.vcxproj", "{49EC5DE8-B8C7-4081-A79C-A13E9224C175}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -41,6 +45,22 @@ Global {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x64.Build.0 = Release|x64 {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x86.ActiveCfg = Release|Win32 {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x86.Build.0 = Release|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x64.ActiveCfg = Debug|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x64.Build.0 = Debug|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x86.ActiveCfg = Debug|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x86.Build.0 = Debug|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x64.ActiveCfg = Release|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x64.Build.0 = Release|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x86.ActiveCfg = Release|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x86.Build.0 = Release|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x64.ActiveCfg = Debug|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x64.Build.0 = Debug|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x86.ActiveCfg = Debug|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x86.Build.0 = Debug|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x64.ActiveCfg = Release|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x64.Build.0 = Release|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x86.ActiveCfg = Release|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index e610934..413871b 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -47,7 +47,7 @@ namespace masesk { }; class EasySocket { public: - void socketListen(const std::string &channelName, int port, std::function callback) { + void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) { if (sockInit() != 0) { throw masesk::socket_error_exception(); @@ -82,13 +82,12 @@ namespace masesk { memset(service, 0, NI_MAXSERV); if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { - std::cout << host << " connected on port " << service << std::endl; + //std::cout << host << " connected on port " << service << std::endl; } else { inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); - std::cout << host << " connected on port " << - ntohs(client.sin_port) << std::endl; + //std::cout << host << " connected on port " << ntohs(client.sin_port) << std::endl; } sockClose(listening); char buff[BUFF_SIZE]; @@ -117,7 +116,7 @@ namespace masesk { sockQuit(); } - void socketSend(const std::string &channelName, const std::string &data) { + void socketSendTCP(const std::string &channelName, const std::string &data) { if (data.size() > BUFF_SIZE) { throw masesk::data_size_exception(); } @@ -132,7 +131,7 @@ namespace masesk { } } - void socketConnect(const std::string &channelName, const std::string &ip, std::uint16_t port) { + void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) { if (sockInit() != 0) { throw masesk::socket_error_exception(); return; @@ -163,7 +162,83 @@ namespace masesk { sockClose(s); sockQuit(); } + } + void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data) { + if (sockInit() != 0) { + throw masesk::socket_error_exception(); + return; + } + if (data.size() > BUFF_SIZE) { + throw masesk::data_size_exception(); + } + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); + inet_pton(AF_INET, ip.c_str(), &(servaddr.sin_addr)); + int result = sendto(sock, data.c_str(), data.length(), + 0, (const struct sockaddr *) &servaddr, + sizeof(servaddr)); + if (result == SOCKET_ERROR) { + throw masesk::socket_error_exception(); + } + + } + void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) { + if (sockInit() != 0) { + throw masesk::socket_error_exception(); + return; + } + sockaddr_in servaddr; + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); +#ifdef _WIN32 + int serverSize = sizeof(servaddr); +#else + unsigned int serverSize = sizeof(client); +#endif + inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr)); + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + bind(sock, (sockaddr*)&servaddr, serverSize); + + server_sockets[channelName] = sock; + char buff[BUFF_SIZE]; + sockaddr_in clientAddr; + int len = sizeof(clientAddr); + + while (1) { + memset(buff, 0, BUFF_SIZE); + int bytesRecieved = recvfrom(sock, (char *)buff, BUFF_SIZE, + 0, (struct sockaddr *) &clientAddr, + &len); + if (bytesRecieved > 0) { + callback(std::string(buff, 0, bytesRecieved)); + } + } + + } + + void socketAcceptUDP(const std::string &channelName, const std::uint16_t &port) { + sockaddr_in servaddr; + servaddr.sin_family = AF_INET; + + +#ifdef _WIN32 + servaddr.sin_addr.S_un.S_addr = INADDR_ANY; +#else + servaddr.sin_addr.s_addr = INADDR_ANY; +#endif + servaddr.sin_port = htons(port); +#ifdef _WIN32 + int serverSize = sizeof(servaddr); +#else + unsigned int serverSize = sizeof(client); +#endif + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + bind(sock, (sockaddr*)&servaddr, serverSize); + server_sockets[channelName] = sock; } private: diff --git a/test/test-client/CMakeLists.txt b/test/test-client/CMakeLists.txt deleted file mode 100644 index 8a58e5d..0000000 --- a/test/test-client/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable( client - Client.cpp -) - -target_link_libraries(client easy-socket) diff --git a/test/test-server/CMakeLists.txt b/test/test-server/CMakeLists.txt deleted file mode 100644 index d76c4ae..0000000 --- a/test/test-server/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable( server - Server.cpp -) - -target_link_libraries(server easy-socket) diff --git a/test/test-tcp-client/CMakeLists.txt b/test/test-tcp-client/CMakeLists.txt new file mode 100644 index 0000000..76edf4a --- /dev/null +++ b/test/test-tcp-client/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( tcp_client + tcp_client.cpp +) + +target_link_libraries(tcp_client easy-socket) diff --git a/test/test-tcp-client/tcp_client.cpp b/test/test-tcp-client/tcp_client.cpp new file mode 100644 index 0000000..2382551 --- /dev/null +++ b/test/test-tcp-client/tcp_client.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +using namespace std; +using namespace masesk; + +int main() { + EasySocket socketManager; + socketManager.socketConnectTCP("test", "127.0.0.1", 8080); + string userInput; + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + socketManager.socketSendTCP("test", userInput); + } + socketManager.closeConnection("test"); + return 0; +} \ No newline at end of file diff --git a/test/test-client/test-client.vcxproj b/test/test-tcp-client/test-client.vcxproj similarity index 98% rename from test/test-client/test-client.vcxproj rename to test/test-tcp-client/test-client.vcxproj index d23b746..6ece84d 100644 --- a/test/test-client/test-client.vcxproj +++ b/test/test-tcp-client/test-client.vcxproj @@ -23,6 +23,7 @@ {4FFDB01A-3314-4B28-841A-1FE8CE3516CE} testclient 10.0.17134.0 + test-tcp-client @@ -127,7 +128,7 @@ - + diff --git a/test/test-client/test-client.vcxproj.filters b/test/test-tcp-client/test-client.vcxproj.filters similarity index 95% rename from test/test-client/test-client.vcxproj.filters rename to test/test-tcp-client/test-client.vcxproj.filters index 9b8a0e2..64b391e 100644 --- a/test/test-client/test-client.vcxproj.filters +++ b/test/test-tcp-client/test-client.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/test/test-tcp-server/CMakeLists.txt b/test/test-tcp-server/CMakeLists.txt new file mode 100644 index 0000000..c597257 --- /dev/null +++ b/test/test-tcp-server/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( tcp_server + tcp_server.cpp +) + +target_link_libraries(tcp_server easy-socket) diff --git a/test/test-tcp-server/RecieverUDP.cpp b/test/test-tcp-server/RecieverUDP.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/test-tcp-server/tcp_server.cpp b/test/test-tcp-server/tcp_server.cpp new file mode 100644 index 0000000..8784958 --- /dev/null +++ b/test/test-tcp-server/tcp_server.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; +using namespace masesk; + +void handleData(const std::string &data) { + cout << "Client sent: " + data << endl; +} + +int main() { + EasySocket socketManager; + socketManager.socketListenTCP("test", 8080, &handleData); + return 0; +} \ No newline at end of file diff --git a/test/test-server/test-server.vcxproj b/test/test-tcp-server/test-server.vcxproj similarity index 98% rename from test/test-server/test-server.vcxproj rename to test/test-tcp-server/test-server.vcxproj index 8f74a8d..9217638 100644 --- a/test/test-server/test-server.vcxproj +++ b/test/test-tcp-server/test-server.vcxproj @@ -23,6 +23,7 @@ {1CAD60FA-43FF-4595-84B3-B72ED7F3330F} testserver 10.0.17134.0 + test-tcp-server @@ -127,7 +128,7 @@ - + diff --git a/test/test-server/test-server.vcxproj.filters b/test/test-tcp-server/test-server.vcxproj.filters similarity index 95% rename from test/test-server/test-server.vcxproj.filters rename to test/test-tcp-server/test-server.vcxproj.filters index f0325e0..06bbbe6 100644 --- a/test/test-server/test-server.vcxproj.filters +++ b/test/test-tcp-server/test-server.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/test/test-udp-receiver/CMakeLists.txt b/test/test-udp-receiver/CMakeLists.txt new file mode 100644 index 0000000..19112d7 --- /dev/null +++ b/test/test-udp-receiver/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( udp_receiver + udp_receiver.cpp +) + +target_link_libraries(udp_receiver easy-socket) diff --git a/test/test-udp-receiver/test-udp-receiver.vcxproj b/test/test-udp-receiver/test-udp-receiver.vcxproj new file mode 100644 index 0000000..b6d5fe0 --- /dev/null +++ b/test/test-udp-receiver/test-udp-receiver.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {EE13023D-664D-4CEA-988D-35BBA91F49C4} + testudpreceiver + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/test/test-udp-receiver/test-udp-receiver.vcxproj.filters b/test/test-udp-receiver/test-udp-receiver.vcxproj.filters new file mode 100644 index 0000000..a642548 --- /dev/null +++ b/test/test-udp-receiver/test-udp-receiver.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/test/test-server/Server.cpp b/test/test-udp-receiver/udp_receiver.cpp similarity index 80% rename from test/test-server/Server.cpp rename to test/test-udp-receiver/udp_receiver.cpp index 6db749e..e9e9196 100644 --- a/test/test-server/Server.cpp +++ b/test/test-udp-receiver/udp_receiver.cpp @@ -9,6 +9,6 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListen("test", 8080, &handleData); + socketManager.socketListenUDP("test", 9090, handleData); return 0; } \ No newline at end of file diff --git a/test/test-udp-sender/CMakeLists.txt b/test/test-udp-sender/CMakeLists.txt new file mode 100644 index 0000000..f33ae11 --- /dev/null +++ b/test/test-udp-sender/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( udp_sender + udp_sender.cpp +) + +target_link_libraries(udp_sender easy-socket) diff --git a/test/test-udp-sender/test-udp-sender.vcxproj b/test/test-udp-sender/test-udp-sender.vcxproj new file mode 100644 index 0000000..24f7df2 --- /dev/null +++ b/test/test-udp-sender/test-udp-sender.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175} + testudpsender + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/test/test-udp-sender/test-udp-sender.vcxproj.filters b/test/test-udp-sender/test-udp-sender.vcxproj.filters new file mode 100644 index 0000000..e076d82 --- /dev/null +++ b/test/test-udp-sender/test-udp-sender.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/test/test-client/Client.cpp b/test/test-udp-sender/udp_sender.cpp similarity index 76% rename from test/test-client/Client.cpp rename to test/test-udp-sender/udp_sender.cpp index 57d9771..aa39e05 100644 --- a/test/test-client/Client.cpp +++ b/test/test-udp-sender/udp_sender.cpp @@ -6,7 +6,6 @@ using namespace masesk; int main() { EasySocket socketManager; - socketManager.socketConnect("test", "127.0.0.1", 8080); string userInput; while (true) { cout << "> "; @@ -14,7 +13,7 @@ int main() { if (userInput.size() <= 0) { break; } - socketManager.socketSend("test", userInput); + socketManager.socketSendUDP("127.0.0.1", 9090, userInput); } socketManager.closeConnection("test"); return 0; From b3e2ea9bb8aa65577e4ce577b5e611381787b06e Mon Sep 17 00:00:00 2001 From: masesk Date: Fri, 4 Dec 2020 15:41:52 -0800 Subject: [PATCH 3/9] fix linux version, add build to gitignore --- .gitignore | 4 + include/masesk/EasySocket.hpp | 146 ++++++++++++++++++++-------------- 2 files changed, 90 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 628c535..21a1d72 100644 --- a/.gitignore +++ b/.gitignore @@ -393,3 +393,7 @@ FodyWeavers.xsd *.exe *.out *.app + + +# Build +/build \ No newline at end of file diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index 413871b..fb980cd 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -21,35 +21,38 @@ const int SOCKET_ERROR = -1; #include #include - -namespace masesk { +namespace masesk +{ const int BUFF_SIZE = 4096; struct socket_error_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Can't start socket!"; } }; struct invalid_socket_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Can't create a socket!"; } }; struct data_size_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Data size is above the maximum allowed by the buffer"; } }; - class EasySocket { + class EasySocket + { public: - void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) { + void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) + { - if (sockInit() != 0) { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); } SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); @@ -65,7 +68,7 @@ namespace masesk { #else hint.sin_addr.s_addr = INADDR_ANY; #endif - bind(listening, (sockaddr*)&hint, sizeof(hint)); + bind(listening, (sockaddr *)&hint, sizeof(hint)); listen(listening, SOMAXCONN); sockaddr_in client; #ifdef _WIN32 @@ -73,14 +76,14 @@ namespace masesk { #else unsigned int clientSize = sizeof(client); #endif - SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); + SOCKET clientSocket = accept(listening, (sockaddr *)&client, &clientSize); server_sockets[channelName] = clientSocket; - char host[NI_MAXHOST]; - char service[NI_MAXSERV]; + char host[NI_MAXHOST]; + char service[NI_MAXSERV]; memset(host, 0, NI_MAXHOST); memset(service, 0, NI_MAXSERV); - if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) + if (getnameinfo((sockaddr *)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { //std::cout << host << " connected on port " << service << std::endl; } @@ -100,28 +103,32 @@ namespace masesk { { throw socket_error_exception(); } - if (bytesReceived > BUFF_SIZE) { + if (bytesReceived > BUFF_SIZE) + { throw masesk::data_size_exception(); } - if (bytesReceived > 0) { + if (bytesReceived > 0) + { callback(std::string(buff, 0, bytesReceived)); } - else { + else + { break; } - - } sockClose(clientSocket); sockQuit(); } - void socketSendTCP(const std::string &channelName, const std::string &data) { - if (data.size() > BUFF_SIZE) { + void socketSendTCP(const std::string &channelName, const std::string &data) + { + if (data.size() > BUFF_SIZE) + { throw masesk::data_size_exception(); } - if (client_sockets.find(channelName) != client_sockets.end()) { + if (client_sockets.find(channelName) != client_sockets.end()) + { SOCKET sock = client_sockets.at(channelName); int sendResult = send(sock, data.c_str(), data.size() + 1, 0); if (sendResult == SOCKET_ERROR) @@ -131,8 +138,10 @@ namespace masesk { } } - void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) { - if (sockInit() != 0) { + void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) + { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); return; } @@ -146,7 +155,7 @@ namespace masesk { hint.sin_family = AF_INET; hint.sin_port = htons(port); inet_pton(AF_INET, ip.c_str(), &hint.sin_addr); - int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint)); + int connResult = connect(sock, (sockaddr *)&hint, sizeof(hint)); if (connResult == SOCKET_ERROR) { sockClose(sock); @@ -154,21 +163,25 @@ namespace masesk { throw socket_error_exception(); } client_sockets[channelName] = sock; - } - void closeConnection(const std::string &channelName) { - if (client_sockets.find(channelName) != client_sockets.end()) { + void closeConnection(const std::string &channelName) + { + if (client_sockets.find(channelName) != client_sockets.end()) + { SOCKET s = client_sockets.at(channelName); sockClose(s); sockQuit(); } } - void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data) { - if (sockInit() != 0) { + void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data) + { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); return; } - if (data.size() > BUFF_SIZE) { + if (data.size() > BUFF_SIZE) + { throw masesk::data_size_exception(); } SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -178,16 +191,18 @@ namespace masesk { servaddr.sin_port = htons(port); inet_pton(AF_INET, ip.c_str(), &(servaddr.sin_addr)); int result = sendto(sock, data.c_str(), data.length(), - 0, (const struct sockaddr *) &servaddr, - sizeof(servaddr)); - if (result == SOCKET_ERROR) { + 0, (const struct sockaddr *)&servaddr, + sizeof(servaddr)); + if (result == SOCKET_ERROR) + { throw masesk::socket_error_exception(); } - } - void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) { - if (sockInit() != 0) { + void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) + { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); return; } @@ -197,34 +212,40 @@ namespace masesk { #ifdef _WIN32 int serverSize = sizeof(servaddr); #else - unsigned int serverSize = sizeof(client); + unsigned int serverSize = sizeof(servaddr); #endif inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr)); SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - bind(sock, (sockaddr*)&servaddr, serverSize); + bind(sock, (sockaddr *)&servaddr, serverSize); server_sockets[channelName] = sock; - char buff[BUFF_SIZE]; - sockaddr_in clientAddr; - int len = sizeof(clientAddr); - - while (1) { - memset(buff, 0, BUFF_SIZE); - int bytesRecieved = recvfrom(sock, (char *)buff, BUFF_SIZE, - 0, (struct sockaddr *) &clientAddr, - &len); - if (bytesRecieved > 0) { - callback(std::string(buff, 0, bytesRecieved)); - } - } + char buff[BUFF_SIZE]; + sockaddr_in clientAddr; + servaddr.sin_port = htons(port); +#ifdef _WIN32 + int clientSize = sizeof(servaddr); +#else + unsigned int clientSize = sizeof(servaddr); +#endif + while (1) + { + memset(buff, 0, BUFF_SIZE); + int bytesRecieved = recvfrom(sock, (char *)buff, BUFF_SIZE, + 0, (struct sockaddr *)&clientAddr, + &clientSize); + if (bytesRecieved > 0) + { + callback(std::string(buff, 0, bytesRecieved)); + } + } } - void socketAcceptUDP(const std::string &channelName, const std::uint16_t &port) { + void socketAcceptUDP(const std::string &channelName, const std::uint16_t &port) + { sockaddr_in servaddr; servaddr.sin_family = AF_INET; - #ifdef _WIN32 servaddr.sin_addr.S_un.S_addr = INADDR_ANY; #else @@ -234,14 +255,14 @@ namespace masesk { #ifdef _WIN32 int serverSize = sizeof(servaddr); #else - unsigned int serverSize = sizeof(client); + unsigned int serverSize = sizeof(servaddr); #endif SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - bind(sock, (sockaddr*)&servaddr, serverSize); + bind(sock, (sockaddr *)&servaddr, serverSize); server_sockets[channelName] = sock; } - private: + private: std::unordered_map client_sockets; std::unordered_map server_sockets; int sockInit(void) @@ -269,15 +290,20 @@ namespace masesk { #ifdef _WIN32 status = shutdown(sock, SD_BOTH); - if (status == 0) { status = closesocket(sock); } + if (status == 0) + { + status = closesocket(sock); + } #else status = shutdown(sock, SHUT_RDWR); - if (status == 0) { status = close(sock); } + if (status == 0) + { + status = close(sock); + } #endif return status; - } }; -} +} // namespace masesk #endif \ No newline at end of file From cc1a777c94bde3f5720992ca26865c43d0e00601 Mon Sep 17 00:00:00 2001 From: "masesm5@yahoo.com" Date: Fri, 4 Dec 2020 15:56:06 -0800 Subject: [PATCH 4/9] fix readme --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3253afe..8ad5e21 100644 --- a/README.md +++ b/README.md @@ -125,24 +125,24 @@ int main() { #### Server Functions -* `void socketListenTCP(std::string channelName, std::uint16_t port, std::function callback);` +* `void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback);` * channelName: string identifier of channel * port: integer value of port used on server side (eg. 8080) * function: pointer of function that will be called to handle the data when the server recieves data from the client -* `void closeConnection(std::string channelName)` - close socket port on server +* `void closeConnection(const std::string &channelName)` - close socket port on server * channelName: string identifier of channel #### Client Functions -* `void socketConnectTCP(std::string channelName, std::string ip, std::uint16_t port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. +* `void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. * channelName - string identifier of channel * ip - string for where the server resides (eg. 127.0.0.1 for local) * port - integer value of port used on server side (eg. 8080) -* `void socketSendTCP(std::string channelName, std::string data)` - send data to server based on channel name +* `void socketSendTCP(const std::string &channelName, const std::string &data)` - send data to server based on channel name * channelName: string identifier of channel * data: data to be sent through to the server on given channel -* `void closeConnection(std::string channelName)` - close connection with server using channel name +* `void closeConnection(const std::string &channelName)` - close connection with server using channel name * channelName: string identifier of channel @@ -150,19 +150,21 @@ int main() { #### Receiver Functions -* `void socketListenUDP(std::string channelName, std::uint16_t port, std::function callback);` +* `void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback);` * channelName: string identifier of channel * port: integer value of port used on server side (eg. 9090) * function: pointer of function that will be called to handle the data when the server recieves data from the client +* `void closeConnection(const std::string &channelName)` - close UDP socket port + * channelName: string identifier of channel #### Sender Functions -* `void socketSendUDP(std::string ip, std::uint16_t port, std::string data)` - send data to server based on channel name +* `void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data)` - send data to server based on channel name * ip - string for where the server resides (eg. 127.0.0.1 for local) * port - integer value of port used on server side (eg. 9090) * data: data to be sent through to the server on given channel ## Example -Check `test/test-server` and `test/test-client` for a working client and server example running locally. +Check `test/test-tcp-server` / `test/test-tcp-client` / `test/test-udp-receiver` / `test/test-udp-sender` for a working client and server example running locally. ### Build Tests on Windows 1. Open `easy-socket.sln` in Visual Studio 2017 From 3a9748a23ea21f2b577d72cbd3f97f9f6de4ded8 Mon Sep 17 00:00:00 2001 From: masesk Date: Fri, 4 Dec 2020 16:06:22 -0800 Subject: [PATCH 5/9] remove version numbers --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e4f8fe7..f645213 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,12 +35,12 @@ install( configure_package_config_file( ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + INSTALL_DESTINATION lib/cmake/${PROJECT_NAME} ) install( EXPORT ${PROJECT_NAME}Targets - DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + DESTINATION lib/cmake/${PROJECT_NAME} ) install( @@ -48,7 +48,7 @@ install( "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION - lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + lib/cmake/${PROJECT_NAME} ) install( From 81c5c5d57afb90cb961a9fb82acc39b586b77a53 Mon Sep 17 00:00:00 2001 From: masesk <36123825+masesk@users.noreply.github.com> Date: Fri, 4 Dec 2020 21:55:15 -0800 Subject: [PATCH 6/9] Remove unused accept function --- include/masesk/EasySocket.hpp | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index fb980cd..145ba19 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -241,27 +241,6 @@ namespace masesk } } - void socketAcceptUDP(const std::string &channelName, const std::uint16_t &port) - { - sockaddr_in servaddr; - servaddr.sin_family = AF_INET; - -#ifdef _WIN32 - servaddr.sin_addr.S_un.S_addr = INADDR_ANY; -#else - servaddr.sin_addr.s_addr = INADDR_ANY; -#endif - servaddr.sin_port = htons(port); -#ifdef _WIN32 - int serverSize = sizeof(servaddr); -#else - unsigned int serverSize = sizeof(servaddr); -#endif - SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - bind(sock, (sockaddr *)&servaddr, serverSize); - server_sockets[channelName] = sock; - } - private: std::unordered_map client_sockets; std::unordered_map server_sockets; @@ -306,4 +285,4 @@ namespace masesk } }; } // namespace masesk -#endif \ No newline at end of file +#endif From 947220791100f9b0d792fc2be97547a5429deb27 Mon Sep 17 00:00:00 2001 From: "masesm5@yahoo.com" Date: Mon, 7 Dec 2020 19:53:33 -0800 Subject: [PATCH 7/9] rename test to examples --- CMakeLists.txt | 12 ++++++------ easy-socket.sln | 8 ++++---- {test => examples}/test-tcp-client/CMakeLists.txt | 0 {test => examples}/test-tcp-client/tcp_client.cpp | 0 .../test-tcp-client/test-client.vcxproj | 0 .../test-tcp-client/test-client.vcxproj.filters | 0 {test => examples}/test-tcp-server/CMakeLists.txt | 0 {test => examples}/test-tcp-server/RecieverUDP.cpp | 0 {test => examples}/test-tcp-server/tcp_server.cpp | 0 .../test-tcp-server/test-server.vcxproj | 0 .../test-tcp-server/test-server.vcxproj.filters | 0 {test => examples}/test-udp-receiver/CMakeLists.txt | 0 .../test-udp-receiver/test-udp-receiver.vcxproj | 0 .../test-udp-receiver.vcxproj.filters | 0 .../test-udp-receiver/udp_receiver.cpp | 0 {test => examples}/test-udp-sender/CMakeLists.txt | 0 .../test-udp-sender/test-udp-sender.vcxproj | 0 .../test-udp-sender/test-udp-sender.vcxproj.filters | 0 {test => examples}/test-udp-sender/udp_sender.cpp | 0 19 files changed, 10 insertions(+), 10 deletions(-) rename {test => examples}/test-tcp-client/CMakeLists.txt (100%) rename {test => examples}/test-tcp-client/tcp_client.cpp (100%) rename {test => examples}/test-tcp-client/test-client.vcxproj (100%) rename {test => examples}/test-tcp-client/test-client.vcxproj.filters (100%) rename {test => examples}/test-tcp-server/CMakeLists.txt (100%) rename {test => examples}/test-tcp-server/RecieverUDP.cpp (100%) rename {test => examples}/test-tcp-server/tcp_server.cpp (100%) rename {test => examples}/test-tcp-server/test-server.vcxproj (100%) rename {test => examples}/test-tcp-server/test-server.vcxproj.filters (100%) rename {test => examples}/test-udp-receiver/CMakeLists.txt (100%) rename {test => examples}/test-udp-receiver/test-udp-receiver.vcxproj (100%) rename {test => examples}/test-udp-receiver/test-udp-receiver.vcxproj.filters (100%) rename {test => examples}/test-udp-receiver/udp_receiver.cpp (100%) rename {test => examples}/test-udp-sender/CMakeLists.txt (100%) rename {test => examples}/test-udp-sender/test-udp-sender.vcxproj (100%) rename {test => examples}/test-udp-sender/test-udp-sender.vcxproj.filters (100%) rename {test => examples}/test-udp-sender/udp_sender.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f645213..bf79529 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5) project(easy-socket VERSION 0.2.0) -option(BUILD_TESTS "Turn on to build tests." ON) +option(BUILD_EXAMPLES "Turn on to build tests." ON) set(PROJECT_COMPATIBILITY AnyNewerVersion) add_library(${PROJECT_NAME} INTERFACE) @@ -58,10 +58,10 @@ install( set(${PROJECT_NAME}_VERSION ${PROJECT_VERSION} CACHE INTERNAL "") -if(BUILD_TESTS) +if(BUILD_EXAMPLES) # Add sub directories - add_subdirectory(test/test-tcp-server) - add_subdirectory(test/test-tcp-client) - add_subdirectory(test/test-udp-sender) - add_subdirectory(test/test-udp-receiver) + add_subdirectory(examples/test-tcp-server) + add_subdirectory(examples/test-tcp-client) + add_subdirectory(examples/test-udp-sender) + add_subdirectory(examples/test-udp-receiver) endif() diff --git a/easy-socket.sln b/easy-socket.sln index 4c6ab85..fa5941e 100644 --- a/easy-socket.sln +++ b/easy-socket.sln @@ -5,13 +5,13 @@ VisualStudioVersion = 15.0.28010.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "easy-socket", "easy-socket.vcxproj", "{EF42A18B-14CB-46AD-A34C-30B35EF0586C}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-client", "test\test-tcp-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-client", "examples\test-tcp-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-server", "test\test-tcp-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-server", "examples\test-tcp-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-receiver", "test\test-udp-receiver\test-udp-receiver.vcxproj", "{EE13023D-664D-4CEA-988D-35BBA91F49C4}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-receiver", "examples\test-udp-receiver\test-udp-receiver.vcxproj", "{EE13023D-664D-4CEA-988D-35BBA91F49C4}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-sender", "test\test-udp-sender\test-udp-sender.vcxproj", "{49EC5DE8-B8C7-4081-A79C-A13E9224C175}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-sender", "examples\test-udp-sender\test-udp-sender.vcxproj", "{49EC5DE8-B8C7-4081-A79C-A13E9224C175}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/test/test-tcp-client/CMakeLists.txt b/examples/test-tcp-client/CMakeLists.txt similarity index 100% rename from test/test-tcp-client/CMakeLists.txt rename to examples/test-tcp-client/CMakeLists.txt diff --git a/test/test-tcp-client/tcp_client.cpp b/examples/test-tcp-client/tcp_client.cpp similarity index 100% rename from test/test-tcp-client/tcp_client.cpp rename to examples/test-tcp-client/tcp_client.cpp diff --git a/test/test-tcp-client/test-client.vcxproj b/examples/test-tcp-client/test-client.vcxproj similarity index 100% rename from test/test-tcp-client/test-client.vcxproj rename to examples/test-tcp-client/test-client.vcxproj diff --git a/test/test-tcp-client/test-client.vcxproj.filters b/examples/test-tcp-client/test-client.vcxproj.filters similarity index 100% rename from test/test-tcp-client/test-client.vcxproj.filters rename to examples/test-tcp-client/test-client.vcxproj.filters diff --git a/test/test-tcp-server/CMakeLists.txt b/examples/test-tcp-server/CMakeLists.txt similarity index 100% rename from test/test-tcp-server/CMakeLists.txt rename to examples/test-tcp-server/CMakeLists.txt diff --git a/test/test-tcp-server/RecieverUDP.cpp b/examples/test-tcp-server/RecieverUDP.cpp similarity index 100% rename from test/test-tcp-server/RecieverUDP.cpp rename to examples/test-tcp-server/RecieverUDP.cpp diff --git a/test/test-tcp-server/tcp_server.cpp b/examples/test-tcp-server/tcp_server.cpp similarity index 100% rename from test/test-tcp-server/tcp_server.cpp rename to examples/test-tcp-server/tcp_server.cpp diff --git a/test/test-tcp-server/test-server.vcxproj b/examples/test-tcp-server/test-server.vcxproj similarity index 100% rename from test/test-tcp-server/test-server.vcxproj rename to examples/test-tcp-server/test-server.vcxproj diff --git a/test/test-tcp-server/test-server.vcxproj.filters b/examples/test-tcp-server/test-server.vcxproj.filters similarity index 100% rename from test/test-tcp-server/test-server.vcxproj.filters rename to examples/test-tcp-server/test-server.vcxproj.filters diff --git a/test/test-udp-receiver/CMakeLists.txt b/examples/test-udp-receiver/CMakeLists.txt similarity index 100% rename from test/test-udp-receiver/CMakeLists.txt rename to examples/test-udp-receiver/CMakeLists.txt diff --git a/test/test-udp-receiver/test-udp-receiver.vcxproj b/examples/test-udp-receiver/test-udp-receiver.vcxproj similarity index 100% rename from test/test-udp-receiver/test-udp-receiver.vcxproj rename to examples/test-udp-receiver/test-udp-receiver.vcxproj diff --git a/test/test-udp-receiver/test-udp-receiver.vcxproj.filters b/examples/test-udp-receiver/test-udp-receiver.vcxproj.filters similarity index 100% rename from test/test-udp-receiver/test-udp-receiver.vcxproj.filters rename to examples/test-udp-receiver/test-udp-receiver.vcxproj.filters diff --git a/test/test-udp-receiver/udp_receiver.cpp b/examples/test-udp-receiver/udp_receiver.cpp similarity index 100% rename from test/test-udp-receiver/udp_receiver.cpp rename to examples/test-udp-receiver/udp_receiver.cpp diff --git a/test/test-udp-sender/CMakeLists.txt b/examples/test-udp-sender/CMakeLists.txt similarity index 100% rename from test/test-udp-sender/CMakeLists.txt rename to examples/test-udp-sender/CMakeLists.txt diff --git a/test/test-udp-sender/test-udp-sender.vcxproj b/examples/test-udp-sender/test-udp-sender.vcxproj similarity index 100% rename from test/test-udp-sender/test-udp-sender.vcxproj rename to examples/test-udp-sender/test-udp-sender.vcxproj diff --git a/test/test-udp-sender/test-udp-sender.vcxproj.filters b/examples/test-udp-sender/test-udp-sender.vcxproj.filters similarity index 100% rename from test/test-udp-sender/test-udp-sender.vcxproj.filters rename to examples/test-udp-sender/test-udp-sender.vcxproj.filters diff --git a/test/test-udp-sender/udp_sender.cpp b/examples/test-udp-sender/udp_sender.cpp similarity index 100% rename from test/test-udp-sender/udp_sender.cpp rename to examples/test-udp-sender/udp_sender.cpp From e1adaf397efde283b9c11b5b9bbdc151287b3514 Mon Sep 17 00:00:00 2001 From: "masesm5@yahoo.com" Date: Sun, 28 Feb 2021 21:53:15 -0800 Subject: [PATCH 8/9] init udp function to minimize socket creation --- examples/test-udp-receiver/udp_receiver.cpp | 2 +- examples/test-udp-sender/udp_sender.cpp | 8 ++- include/masesk/EasySocket.hpp | 76 +++++++++++++-------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/examples/test-udp-receiver/udp_receiver.cpp b/examples/test-udp-receiver/udp_receiver.cpp index e9e9196..a82988b 100644 --- a/examples/test-udp-receiver/udp_receiver.cpp +++ b/examples/test-udp-receiver/udp_receiver.cpp @@ -9,6 +9,6 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListenUDP("test", 9090, handleData); + socketManager.socketListenUDP("test_udp", 9090, handleData); return 0; } \ No newline at end of file diff --git a/examples/test-udp-sender/udp_sender.cpp b/examples/test-udp-sender/udp_sender.cpp index aa39e05..9d7ba46 100644 --- a/examples/test-udp-sender/udp_sender.cpp +++ b/examples/test-udp-sender/udp_sender.cpp @@ -7,13 +7,19 @@ using namespace masesk; int main() { EasySocket socketManager; string userInput; + socketManager.socketInitSendUDP("test_udp", "127.0.0.1", 9090); while (true) { cout << "> "; getline(cin, userInput); if (userInput.size() <= 0) { break; } - socketManager.socketSendUDP("127.0.0.1", 9090, userInput); + try { + socketManager.socketSendUDP("test_udp", userInput); + }catch (socket_error_exception &e) { + cout << e.what() << endl; + } + } socketManager.closeConnection("test"); return 0; diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index 145ba19..8082050 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -45,6 +45,10 @@ namespace masesk return "Data size is above the maximum allowed by the buffer"; } }; + struct sock_save { + SOCKET sock; + sockaddr_in addr; + }; class EasySocket { public: @@ -77,7 +81,9 @@ namespace masesk unsigned int clientSize = sizeof(client); #endif SOCKET clientSocket = accept(listening, (sockaddr *)&client, &clientSize); - server_sockets[channelName] = clientSocket; + sock_save tcp_save; + tcp_save.sock = clientSocket; + server_sockets[channelName] = tcp_save; char host[NI_MAXHOST]; char service[NI_MAXSERV]; @@ -129,7 +135,7 @@ namespace masesk if (client_sockets.find(channelName) != client_sockets.end()) { - SOCKET sock = client_sockets.at(channelName); + SOCKET sock = client_sockets.at(channelName).sock; int sendResult = send(sock, data.c_str(), data.size() + 1, 0); if (sendResult == SOCKET_ERROR) { @@ -162,40 +168,54 @@ namespace masesk sockQuit(); throw socket_error_exception(); } - client_sockets[channelName] = sock; + client_sockets[channelName].sock = sock; } void closeConnection(const std::string &channelName) { if (client_sockets.find(channelName) != client_sockets.end()) { - SOCKET s = client_sockets.at(channelName); + SOCKET s = client_sockets.at(channelName).sock; sockClose(s); sockQuit(); } } - void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data) - { - if (sockInit() != 0) - { - throw masesk::socket_error_exception(); - return; - } - if (data.size() > BUFF_SIZE) - { - throw masesk::data_size_exception(); + + + void socketInitSendUDP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) { + + if (client_sockets.find(channelName) == client_sockets.end()) { + if (sockInit() != 0) + { + throw masesk::socket_error_exception(); + return; + } + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); + inet_pton(AF_INET, ip.c_str(), &(servaddr.sin_addr)); + sock_save udp_sock; + udp_sock.sock = sock; + udp_sock.addr = servaddr; + client_sockets[channelName] = udp_sock; } - SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - sockaddr_in servaddr; - memset(&servaddr, 0, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(port); - inet_pton(AF_INET, ip.c_str(), &(servaddr.sin_addr)); - int result = sendto(sock, data.c_str(), data.length(), - 0, (const struct sockaddr *)&servaddr, - sizeof(servaddr)); - if (result == SOCKET_ERROR) + } + + void socketSendUDP(const std::string &channelName, const std::string &data) + { + if (client_sockets.find(channelName) != client_sockets.end()) { - throw masesk::socket_error_exception(); + sock_save udp_sock = client_sockets.at(channelName); + SOCKET sock = udp_sock.sock; + sockaddr_in servaddr = udp_sock.addr; + int result = sendto(sock, data.c_str(), data.length(), + 0, (const struct sockaddr *)&servaddr, + sizeof(servaddr)); + if (result == SOCKET_ERROR) + { + throw masesk::socket_error_exception(); + } } } @@ -218,7 +238,7 @@ namespace masesk SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); bind(sock, (sockaddr *)&servaddr, serverSize); - server_sockets[channelName] = sock; + server_sockets[channelName].sock = sock; char buff[BUFF_SIZE]; sockaddr_in clientAddr; servaddr.sin_port = htons(port); @@ -242,8 +262,8 @@ namespace masesk } private: - std::unordered_map client_sockets; - std::unordered_map server_sockets; + std::unordered_map client_sockets; + std::unordered_map server_sockets; int sockInit(void) { #ifdef _WIN32 From 6c2086e0a3da5fdc37c2d63133be7552d4b6cacb Mon Sep 17 00:00:00 2001 From: masesk Date: Mon, 8 Aug 2022 14:17:31 -0700 Subject: [PATCH 9/9] generic types for return and parameter of callback --- examples/test-tcp-server/tcp_server.cpp | 2 +- examples/test-udp-receiver/udp_receiver.cpp | 2 +- include/masesk/EasySocket.hpp | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/test-tcp-server/tcp_server.cpp b/examples/test-tcp-server/tcp_server.cpp index 8784958..6a53179 100644 --- a/examples/test-tcp-server/tcp_server.cpp +++ b/examples/test-tcp-server/tcp_server.cpp @@ -9,6 +9,6 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListenTCP("test", 8080, &handleData); + socketManager.socketListenTCP("test", 8080, &handleData); return 0; } \ No newline at end of file diff --git a/examples/test-udp-receiver/udp_receiver.cpp b/examples/test-udp-receiver/udp_receiver.cpp index a82988b..bc4d6af 100644 --- a/examples/test-udp-receiver/udp_receiver.cpp +++ b/examples/test-udp-receiver/udp_receiver.cpp @@ -9,6 +9,6 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListenUDP("test_udp", 9090, handleData); + socketManager.socketListenUDP("test_udp", 9090, handleData); return 0; } \ No newline at end of file diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index 8082050..a1b5a48 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -52,7 +52,8 @@ namespace masesk class EasySocket { public: - void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) + template + void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) { if (sockInit() != 0) @@ -136,7 +137,7 @@ namespace masesk if (client_sockets.find(channelName) != client_sockets.end()) { SOCKET sock = client_sockets.at(channelName).sock; - int sendResult = send(sock, data.c_str(), data.size() + 1, 0); + int sendResult = send(sock, data.c_str(), (int)data.size() + 1, 0); if (sendResult == SOCKET_ERROR) { throw masesk::socket_error_exception(); @@ -209,7 +210,7 @@ namespace masesk sock_save udp_sock = client_sockets.at(channelName); SOCKET sock = udp_sock.sock; sockaddr_in servaddr = udp_sock.addr; - int result = sendto(sock, data.c_str(), data.length(), + int result = sendto(sock, data.c_str(), (int)data.length(), 0, (const struct sockaddr *)&servaddr, sizeof(servaddr)); if (result == SOCKET_ERROR) @@ -219,7 +220,8 @@ namespace masesk } } - void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) + template + void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) { if (sockInit() != 0) {