Skip to content

Commit 22604da

Browse files
authored
Maint/refactor (#12)
* Move dual stack setup to netex.c * make netex a static library * Spefify C99 * Convert cpp stuff to c * Update README * stagger windows, cleaning up... * don't thread accept * Multipeer shapes * sdl cleanup and bug fixes * Remove unneeded includes * more cleanup * CI: Add windows * CI: Add MacOS * Use win32 Platform header guards * Remove sleep suffix (fix test on MacOS) * Fix clang warnings * Windowize * Remove global struct conn_inf * Rename main struct and vars * Make recv_args more human friendly * Rename the sdl example and update docs
1 parent e8b98fb commit 22604da

32 files changed

+895
-893
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ BreakFunctionDefinitionParameters: false
116116
BreakInheritanceList: BeforeColon
117117
BreakStringLiterals: true
118118
BreakTemplateDeclarations: MultiLine
119-
ColumnLimit: 80
119+
ColumnLimit: 100
120120
CommentPragmas: '^ IWYU pragma:'
121121
CompactNamespaces: false
122122
ConstructorInitializerIndentWidth: 4

.github/workflows/c-cpp.yml renamed to .github/workflows/linux.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
name: C/C++ CI
1+
name: Linux
22
concurrency:
33
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
44
cancel-in-progress: true
55

66
on:
77
push:
88
branches: [ trunk ]
9+
paths:
10+
- '**'
11+
- '!**.yml'
12+
- '!**.md'
13+
- '**/linux.yml'
14+
915
pull_request:
1016
branches: [ trunk ]
17+
paths:
18+
- '**'
19+
- '!**.yml'
20+
- '!**.md'
21+
- '**/linux.yml'
1122

1223
jobs:
1324
build:

.github/workflows/macos.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: MacOS
2+
concurrency:
3+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
4+
cancel-in-progress: true
5+
6+
on:
7+
push:
8+
branches: [ trunk ]
9+
paths:
10+
- '**'
11+
- '!**.yml'
12+
- '!**.md'
13+
- '**/macos.yml'
14+
15+
pull_request:
16+
branches: [ trunk ]
17+
paths:
18+
- '**'
19+
- '!**.yml'
20+
- '!**.md'
21+
- '**/macos.yml'
22+
23+
jobs:
24+
macos:
25+
runs-on: macos-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install dependencies
30+
run: |
31+
brew install \
32+
meson \
33+
sdl2
34+
35+
- name: Meson setup
36+
run: |
37+
meson setup _build -Db_sanitize=none
38+
39+
- name: Build
40+
run: |
41+
meson compile -C _build
42+
43+
- name: Test
44+
run: |
45+
meson test -v -C _build

.github/workflows/windows.yml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Windows
2+
concurrency:
3+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
4+
cancel-in-progress: true
5+
6+
on:
7+
push:
8+
branches: [ trunk ]
9+
paths:
10+
- '**'
11+
- '!**.yml'
12+
- '!**.md'
13+
- '**/windows.yml'
14+
15+
pull_request:
16+
branches: [ trunk ]
17+
paths:
18+
- '**'
19+
- '!**.yml'
20+
- '!**.md'
21+
- '**/windows.yml'
22+
23+
jobs:
24+
MSYS2:
25+
runs-on: windows-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
- uses: msys2/setup-msys2@v2
29+
with:
30+
msystem: mingw64
31+
pacboy: >-
32+
gcc:p
33+
ninja:p
34+
pkgconf:p
35+
update: true
36+
37+
- name: Install dependencies
38+
shell: msys2 {0}
39+
run: |
40+
pacman --noconfirm --needed -S \
41+
mingw-w64-x86_64-meson \
42+
mingw-w64-x86_64-SDL2 \
43+
mingw-w64-x86_64-winpthreads
44+
45+
- name: Meson setup
46+
shell: msys2 {0}
47+
run: |
48+
meson setup _build
49+
50+
- name: Build
51+
shell: msys2 {0}
52+
run: |
53+
meson compile -C _build
54+
55+
- name: Test
56+
shell: msys2 {0}
57+
run: |
58+
meson test -v -C _build

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ TCP and UDP examples of networking in C
66

77
Website: https://github.com/andy5995/c_networking_examples
88

9-
## tcp_dual_stack_sdl2_window server and client
9+
## TCP SDL Window Updater server and client
1010

11-
When the server is run, an SDL window is created with a red circle. Connect
12-
with the client. When you click in the server window, the red circle will move
13-
in the client window.
11+
When you connect to the server with the client, a window rendered with sdl
12+
will be created. Clicking in the client or server window will move a geometric
13+
shape to the coordinates where you clicked.
1414

1515
## Dual Stack Echo Server
1616

@@ -59,15 +59,11 @@ to change that yet.
5959

6060
## Compiling
6161

62-
You can compile the c files individually:
63-
64-
cc -Wall 'example.c' netex.c -o 'example'
65-
66-
or use [meson](https://mesonbuild.com/) to build them all at once:
67-
68-
meson builddir
69-
cd builddir
70-
ninja
62+
```sh
63+
meson builddir
64+
cd builddir
65+
ninja
66+
```
7167

7268
## See Also
7369

dual_stack_echo_client.c

+7-51
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,30 @@
11
/*
22
* dual_stack_echo_client.c
33
*
4-
* This is adapted from code generated by ChatGPT and examples provided
5-
* in the C library documentation
6-
*
74
*/
85

9-
#include <arpa/inet.h>
10-
#include <netdb.h>
116
#include <stdio.h>
127
#include <stdlib.h>
138
#include <string.h>
14-
#include <sys/socket.h>
15-
#include <sys/types.h>
169
#include <unistd.h>
1710

18-
#define PORT "12345" // Must match the server's port
11+
#include "netex.h"
1912

2013
int main(int argc, char *argv[]) {
21-
char *server_addr = NULL;
22-
if (argc == 2)
23-
server_addr = argv[1];
24-
else {
25-
fputs("You must provide the server address.\n", stderr);
26-
return 1;
27-
}
14+
struct socket_info_t socket_info;
15+
parse_client_opts(argc, argv, &socket_info);
16+
assign_tcp_dual_stack_client_fd(&socket_info);
2817

29-
int client_fd;
30-
struct addrinfo hints, *res, *p;
3118
char buffer[1024];
32-
33-
// Set up hints for getaddrinfo()
34-
memset(&hints, 0, sizeof(hints));
35-
hints.ai_family = AF_UNSPEC; // Allow both IPv4 and IPv6
36-
hints.ai_socktype = SOCK_STREAM;
37-
38-
// Get address info
39-
if (getaddrinfo(server_addr, PORT, &hints, &res) != 0) {
40-
perror("getaddrinfo");
41-
return 1;
42-
}
43-
44-
// Try to connect to one of the results
45-
for (p = res; p != NULL; p = p->ai_next) {
46-
client_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
47-
if (client_fd == -1)
48-
continue;
49-
50-
if (connect(client_fd, p->ai_addr, p->ai_addrlen) == 0)
51-
break; // Connected successfully
52-
53-
close(client_fd);
54-
}
55-
56-
freeaddrinfo(res);
57-
58-
if (!p) {
59-
perror("Failed to connect");
60-
return 1;
61-
}
62-
6319
// Read response from server
64-
ssize_t bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
20+
ssize_t bytes_received = recv(socket_info.sockfd, buffer, sizeof(buffer) - 1, 0);
6521
if (bytes_received > 0) {
66-
buffer[bytes_received] = '\0'; // Null-terminate received data
22+
buffer[bytes_received] = '\0';
6723
printf("Server response: %s", buffer);
6824
} else {
6925
perror("recv");
7026
}
7127

72-
close(client_fd);
28+
close_socket_checked(socket_info.sockfd);
7329
return 0;
7430
}

dual_stack_echo_client_cpp_base.cpp

-73
This file was deleted.

0 commit comments

Comments
 (0)