From 1dc95b4eed7974549ef43a87e0777aa343b30fd4 Mon Sep 17 00:00:00 2001 From: Tobias Frust Date: Wed, 29 Jun 2016 21:44:58 +0200 Subject: added client and server executable for testing simple udp packet transfer --- src/CMakeLists.txt | 10 ++++++++-- src/UDPServer/UDPServer.cpp | 10 ++++++++++ src/UDPServer/UDPServer.h | 8 +++++++- src/main.cpp | 11 ++++++++--- src/main_server.cpp | 28 ++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/main_server.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 746b901..80196d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,11 +31,17 @@ include_directories( ${BOOST_INCLUDE_DIRS} ) -set(SOURCES +set(SOURCES_CLIENT "${CMAKE_SOURCE_DIR}/UDPClient/UDPClient.cpp" "${CMAKE_SOURCE_DIR}/main.cpp" ) -add_executable(onlineDetectorSimulator ${SOURCES}) +set(SOURCES_SERVER + "${CMAKE_SOURCE_DIR}/UDPServer/UDPServer.cpp" + "${CMAKE_SOURCE_DIR}/main_server.cpp" +) + +add_executable(onlineDetectorSimulatorServer ${SOURCES_SERVER}) +add_executable(onlineDetectorSimulatorClient ${SOURCES_CLIENT}) diff --git a/src/UDPServer/UDPServer.cpp b/src/UDPServer/UDPServer.cpp index cd36647..854835a 100644 --- a/src/UDPServer/UDPServer.cpp +++ b/src/UDPServer/UDPServer.cpp @@ -41,6 +41,16 @@ * \param[in] addr The address we receive on. * \param[in] port The port we receive from. */ + +#include "UDPServer.h" + + #include + #include + + #ifndef SOCK_CLOEXEC + #define SOCK_CLOEXEC 0 + #endif + UDPServer::UDPServer(const std::string& addr, int port) : f_port(port) , f_addr(addr) diff --git a/src/UDPServer/UDPServer.h b/src/UDPServer/UDPServer.h index 7eef796..22f33b3 100644 --- a/src/UDPServer/UDPServer.h +++ b/src/UDPServer/UDPServer.h @@ -14,7 +14,13 @@ #include #include #include -#include +#include + +class udp_client_server_runtime_error : public std::runtime_error +{ +public: + udp_client_server_runtime_error(const char *w) : std::runtime_error(w) {} +}; class UDPServer { diff --git a/src/main.cpp b/src/main.cpp index 2ff08b2..6898a09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,10 +12,15 @@ int main (int argc, char *argv[]){ UDPClient client = UDPClient(address, port); - std::string msg = "test"; - std::size_t length{msg.size()}; + std::size_t length{8008}; - client.send(msg.c_str(), length); + char buf[length]; + for(auto i = 0; i < length; i++){ + *(buf+i) = i%128; + } + + + client.send(buf, length); return 0; diff --git a/src/main_server.cpp b/src/main_server.cpp new file mode 100644 index 0000000..b90de52 --- /dev/null +++ b/src/main_server.cpp @@ -0,0 +1,28 @@ +#include "UDPServer/UDPServer.h" + +#include +#include + +int main (int argc, char *argv[]){ + + std::cout << "Receiving UDP packages: " << std::endl; + + std::string address = "localhost"; + int port = 1234; + + UDPServer server = UDPServer(address, port); + + std::size_t length{8000}; + + char buf[length]; + + server.recv(buf, length); + + for(auto i = 0; i < length; i++){ + printf("%c", buf[i]); + } + printf("\n"); + + return 0; + +} -- cgit v1.2.3