diff options
author | Oxore <oxore@protonmail.com> | 2023-12-07 00:08:57 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2023-12-07 00:08:57 +0300 |
commit | 0b86520b9ff166cd285d077d3d8192653e7d95e9 (patch) | |
tree | a61177320e53d6c81d55a62d965340459859344f |
Initial commit
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | CMakeLists.txt | 47 | ||||
-rw-r--r-- | Readme.md | 19 | ||||
-rw-r--r-- | client.c | 31 | ||||
-rw-r--r-- | server.c | 36 |
5 files changed, 140 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a1c77f --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +compile_commands.json +*.o +*.d +cmake[-_]build*/ +build/ +client +server diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ec05a85 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: Unlicense + +cmake_minimum_required(VERSION 3.16) + +set(CMAKE_EXPORT_COMPILE_COMMANDS True) + +project(udp-tinkering) + +set(common_debug_flags + -g3 + -fsanitize=address,undefined + -fno-omit-frame-pointer + -O1 + ) +set(compile_flags + $<$<CONFIG:Debug>:${common_debug_flags}> + $<$<COMPILE_LANGUAGE:C>:-Wno-nested-anon-types> + $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions> + $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti> + $<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast> + $<$<COMPILE_LANGUAGE:CXX>:-Wsuggest-override> + $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wsuggest-final-types> + $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wsuggest-final-methods> + $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fstrict-volatile-bitfields> + $<$<COMPILE_LANG_AND_ID:C,GNU>:-fstrict-volatile-bitfields> + $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-Wlogical-op> + $<$<COMPILE_LANG_AND_ID:C,GNU>:-Wlogical-op> + $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}/=> + $<$<COMPILE_LANG_AND_ID:C,GNU>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}/=> + -Wall + -Wextra + -pedantic + -Wcast-align + -Wshadow + ) + +add_executable(server server.c) +target_compile_options(server PRIVATE ${compile_flags}) +target_compile_definitions(server PRIVATE _FORTIFY_SOURCE=2) +target_link_options(server PRIVATE $<$<CONFIG:Debug>:${common_debug_flags}>) +target_include_directories(server PRIVATE .) + +add_executable(client client.c) +target_compile_options(client PRIVATE ${compile_flags}) +target_compile_definitions(client PRIVATE _FORTIFY_SOURCE=2) +target_link_options(client PRIVATE $<$<CONFIG:Debug>:${common_debug_flags}>) +target_include_directories(client PRIVATE .) diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..5f8e8b4 --- /dev/null +++ b/Readme.md @@ -0,0 +1,19 @@ +# UDP tinkering + +I want to build server and client for researching how I can do certain things +with UDP and what limits are. + +UDP related thoughts: + +- What packet loss looks like? +- What packet reordering looks like? Is it even a thing? +- Is packet loss really lower when traffic is paced uniformly instead of + sporadic bursts of messages? +- Does message length impact packet loss or packet reordering? +- Can UDP packet be corrupted? +- Can I make UDP hole punching work just off of a custom UDP server? + +And not UDP related, but rather related to my intended usage of UDP things: +- Implement some simulation synchronization by time based on ping roundtrip time + or something (how does NTP even work?). +- Implement file transferring. diff --git a/client.c b/client.c new file mode 100644 index 0000000..524a0a8 --- /dev/null +++ b/client.c @@ -0,0 +1,31 @@ +// Initially based on https://stackoverflow.com/a/35570418 + +#include <netinet/in.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <unistd.h> + +int main(void) +{ + int const fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket failed"); + return 1; + } + struct sockaddr_in const serveraddr = { + .sin_family = AF_INET, + .sin_port = htons(50037), + .sin_addr.s_addr = htonl(0x7f000001), + }; + for (int i = 0; i < 4; i++) { + ssize_t const ret = sendto( + fd, "hello", 5, 0, (struct sockaddr const *)&serveraddr, sizeof(serveraddr)); + if (ret < 0) { + perror("sendto failed"); + break; + } + printf("message sent\n"); + } + close(fd); +} diff --git a/server.c b/server.c new file mode 100644 index 0000000..e35a9b5 --- /dev/null +++ b/server.c @@ -0,0 +1,36 @@ +// Initially based on https://stackoverflow.com/a/35570418 + +#include <netinet/in.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <unistd.h> + +int main(void) +{ + int const fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket failed"); + return 1; + } + struct sockaddr_in const serveraddr = { + .sin_family = AF_INET, + .sin_port = htons(50037), + .sin_addr.s_addr = htonl(INADDR_ANY), + }; + if (bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { + perror("bind failed"); + return 1; + } + for (;;) { + char buffer[200]; + int const length = recvfrom(fd, buffer, sizeof(buffer) - 1, 0, NULL, 0); + if (length < 0) { + perror("recvfrom failed"); + break; + } + buffer[length] = '\0'; + printf("%d bytes: '%s'\n", length, buffer); + } + close(fd); +} |