Compare commits
No commits in common. "deb1ef87f911fc2d4a9c3371123a4ae0329ba27a" and "27ccd5e5b04f9406724291c4ee2a31700e247f71" have entirely different histories.
deb1ef87f9
...
27ccd5e5b0
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
.idea/
|
.idea/
|
||||||
build/
|
|
||||||
|
|||||||
26
Makefile
26
Makefile
@ -1,26 +0,0 @@
|
|||||||
# Makefile for strict C89/C90 compatibility
|
|
||||||
# Compiles all samples in src/c90/ with -std=c90 -pedantic -Wall -Wextra -Werror
|
|
||||||
|
|
||||||
.PHONY: all clean
|
|
||||||
|
|
||||||
CC := gcc
|
|
||||||
CFLAGS := -std=c90 -pedantic -Wall -Wextra -Werror
|
|
||||||
SRCDIR := src/c90
|
|
||||||
BINDIR := build
|
|
||||||
SOURCES := $(wildcard $(SRCDIR)/*.c)
|
|
||||||
BINS := $(SOURCES:$(SRCDIR)/%.c=$(BINDIR)/%)
|
|
||||||
|
|
||||||
all: $(BINS)
|
|
||||||
|
|
||||||
$(BINDIR)/%: $(SRCDIR)/%.c | $(BINDIR)
|
|
||||||
$(CC) $(CFLAGS) -o $@ $<
|
|
||||||
|
|
||||||
$(BINDIR):
|
|
||||||
mkdir -p $(BINDIR)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf $(BINDIR)
|
|
||||||
|
|
||||||
print:
|
|
||||||
@echo "Strict C90 binaries in bin/:"
|
|
||||||
@ls $(BINDIR)
|
|
||||||
47
REAMDE.md
47
REAMDE.md
@ -1,48 +1,3 @@
|
|||||||
# socket-samples
|
# socket-samples
|
||||||
|
|
||||||
Socket programming examples in strict C90 and modern Python3.
|
A simple repo with examples of python sockets
|
||||||
|
|
||||||
## Build & Run C90 Examples
|
|
||||||
|
|
||||||
```
|
|
||||||
make clean && make all
|
|
||||||
```
|
|
||||||
Builds 9 binaries in `build/` (strict `-std=c90 -pedantic -Wall -Wextra -Werror`).
|
|
||||||
|
|
||||||
Port: **9999**
|
|
||||||
|
|
||||||
All use the same **custom binary protocol**:
|
|
||||||
- Header: `1-byte type + 2-byte BE16 length`
|
|
||||||
- Types: `PING(0x01)`, `PONG(0x02)`, `METRIC_REQ(0x03)`, `METRIC_RESP(0x04)`, `SUBSCRIBE(0x05)`, `PUSH(0x06)`, `UNSUB(0x07)`, `ERROR(0xFF)`
|
|
||||||
- Metrics: `cpu`, `memory`, `disk`, `loadavg`, `uptime` (simulated)
|
|
||||||
|
|
||||||
### C90 Clients
|
|
||||||
- `src/c90/metric_client.c`: Basic interactive TCP client (ping/get/quit).
|
|
||||||
- `src/c90/metric_client_ansi.c`: Advanced TCP client w/ `select()` + unsubscribe.
|
|
||||||
- `src/c90/metric_client_sub.c`: TCP client w/ live subscribe/push via `select()`.
|
|
||||||
- `src/c90/metric_client_udp.c`: Full-featured UDP client (get/sub/unsub).
|
|
||||||
- `src/c90/perf_client_udp.c`: UDP perf tester (`--flood` or subscribe mode, 30s throughput).
|
|
||||||
|
|
||||||
### C90 Servers
|
|
||||||
- `src/c90/metric_server.c`: Iterative TCP server (single client).
|
|
||||||
- `src/c90/metric_server_fork.c`: Forking TCP server (multi-client).
|
|
||||||
- `src/c90/metric_server_fork_sub.c`: Forking TCP server w/ subscribe/push.
|
|
||||||
- `src/c90/metric_server_udp.c`: UDP server w/ subscriber management (`select()`).
|
|
||||||
|
|
||||||
## Python Examples
|
|
||||||
|
|
||||||
Run: `python3 src/python/<file>.py [host [port]]`
|
|
||||||
|
|
||||||
Port: **9999** (metrics), **65432** (echo)
|
|
||||||
|
|
||||||
### Echo (text protocol)
|
|
||||||
- `src/python/echo_client.py`: Interactive TCP echo client.
|
|
||||||
- `src/python/echo_server.py`: Iterative TCP echo server.
|
|
||||||
|
|
||||||
### Metrics (same binary protocol as C90)
|
|
||||||
- `src/python/metric_client.py`: Threaded TCP client (ping/get).
|
|
||||||
- `src/python/metric_server.py`: Threaded multi-client TCP server.
|
|
||||||
|
|
||||||
### Monitor (advanced w/ subscribe/push)
|
|
||||||
- `src/python/mon_client.py`: Full TCP client (ping/get/sub).
|
|
||||||
- `src/python/mon_server.py`: Threaded TCP server w/ subscribe/push support.
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/* metric_client.c - STRICT C89/C90 custom binary metric client */
|
/* metric_client.c - C89/C90 custom binary metric client */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#define PORT 9999
|
#define PORT 9999
|
||||||
#define BUF_SIZE 1024
|
#define BUF_SIZE 1024
|
||||||
|
|
||||||
/* Protocol constants (must match server) */
|
/* same constants */
|
||||||
#define MSG_PING 0x01
|
#define MSG_PING 0x01
|
||||||
#define MSG_PONG 0x02
|
#define MSG_PONG 0x02
|
||||||
#define MSG_METRIC_REQ 0x03
|
#define MSG_METRIC_REQ 0x03
|
||||||
@ -19,119 +19,89 @@
|
|||||||
|
|
||||||
#define HEADER_SIZE 3
|
#define HEADER_SIZE 3
|
||||||
|
|
||||||
static void send_message(int sock, unsigned char type,
|
static void send_message(int sock, unsigned char type, const void *payload, unsigned short len) {
|
||||||
const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
unsigned char header[HEADER_SIZE];
|
||||||
header[0] = type;
|
header[0] = type;
|
||||||
header[1] = (len >> 8) & 0xFF; /* big-endian */
|
header[1] = (len >> 8) & 0xFF;
|
||||||
header[2] = len & 0xFF;
|
header[2] = len & 0xFF;
|
||||||
write(sock, header, HEADER_SIZE);
|
write(sock, header, HEADER_SIZE);
|
||||||
if (len > 0)
|
if (len > 0) write(sock, payload, len);
|
||||||
write(sock, payload, len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int recv_exact(int sock, void *buf, int n)
|
static int recv_exact(int sock, void *buf, int n) {
|
||||||
{
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
while (total < n) {
|
while (total < n) {
|
||||||
int r = read(sock, (char *)buf + total, n - total);
|
int r = read(sock, (char*)buf + total, n - total);
|
||||||
if (r <= 0) return -1;
|
if (r <= 0) return -1;
|
||||||
total += r;
|
total += r;
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_wire_comparison(void)
|
static void print_wire_comparison(void) {
|
||||||
{
|
|
||||||
printf("\n--- Wire overhead comparison ---\n");
|
printf("\n--- Wire overhead comparison ---\n");
|
||||||
printf("Our protocol (METRIC_REQ 'cpu'): %d bytes\n", HEADER_SIZE + 3);
|
printf("Our protocol (METRIC_REQ 'cpu'): %d bytes\n", HEADER_SIZE + 3);
|
||||||
printf("Equivalent HTTP GET: 77 bytes\n");
|
printf("Equivalent HTTP GET: 77 bytes\n");
|
||||||
printf("HTTP is ~13x larger\n\n");
|
printf("HTTP is ~13x larger\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void) {
|
||||||
{
|
|
||||||
int sock;
|
int sock;
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
unsigned char header[HEADER_SIZE];
|
unsigned char header[HEADER_SIZE];
|
||||||
unsigned char payload[BUF_SIZE];
|
unsigned char payload[BUF_SIZE];
|
||||||
double val;
|
double val;
|
||||||
char line[256];
|
char line[256];
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
const char *metric;
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sock < 0) {
|
|
||||||
perror("socket");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(PORT);
|
addr.sin_port = htons(PORT);
|
||||||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||||
perror("connect");
|
perror("connect");
|
||||||
close(sock);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Connected to 127.0.0.1:%d (STRICT C89 client)\n", PORT);
|
printf("Connected to 127.0.0.1:%d\n", PORT);
|
||||||
print_wire_comparison();
|
print_wire_comparison();
|
||||||
|
|
||||||
printf("Commands: ping | get <metric> | quit\n");
|
printf("Commands: ping | get <metric> | quit\nMetrics: cpu, memory, disk, loadavg, uptime\n\n");
|
||||||
printf("Metrics: cpu, memory, disk, loadavg, uptime\n\n");
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("> ");
|
printf("> ");
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL)
|
if (fgets(line, sizeof(line), stdin) == NULL) break;
|
||||||
break;
|
|
||||||
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
line[strcspn(line, "\n")] = '\0';
|
||||||
|
|
||||||
if (strcmp(line, "quit") == 0) {
|
if (strcmp(line, "quit") == 0) break;
|
||||||
break;
|
if (strcmp(line, "ping") == 0) {
|
||||||
}
|
|
||||||
else if (strcmp(line, "ping") == 0) {
|
|
||||||
send_message(sock, MSG_PING, NULL, 0);
|
send_message(sock, MSG_PING, NULL, 0);
|
||||||
}
|
} else if (strncmp(line, "get ", 4) == 0) {
|
||||||
else if (strncmp(line, "get ", 4) == 0) {
|
const char *metric = line + 4;
|
||||||
metric = line + 4;
|
|
||||||
send_message(sock, MSG_METRIC_REQ, metric, strlen(metric));
|
send_message(sock, MSG_METRIC_REQ, metric, strlen(metric));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
printf(" Unknown command.\n");
|
printf(" Unknown command.\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* blocking receive */
|
/* blocking receive */
|
||||||
if (recv_exact(sock, header, HEADER_SIZE) != HEADER_SIZE)
|
if (recv_exact(sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
||||||
break;
|
unsigned char type = header[0];
|
||||||
|
unsigned short len = (header[1] << 8) | header[2];
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
if (recv_exact(sock, payload, len) != len)
|
if (recv_exact(sock, payload, len) != len) break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == MSG_PONG) {
|
if (type == MSG_PONG) {
|
||||||
printf(" [PONG] server alive\n");
|
printf(" [PONG] server alive\n");
|
||||||
}
|
} else if (type == MSG_METRIC_RESP) {
|
||||||
else if (type == MSG_METRIC_RESP) {
|
|
||||||
memcpy(&val, payload, sizeof(double));
|
memcpy(&val, payload, sizeof(double));
|
||||||
printf(" [METRIC] %.2f\n", val);
|
printf(" [METRIC] %.2f\n", val);
|
||||||
}
|
} else if (type == MSG_ERROR) {
|
||||||
else if (type == MSG_ERROR) {
|
|
||||||
payload[len] = '\0';
|
payload[len] = '\0';
|
||||||
printf(" [ERROR] %s\n", payload);
|
printf(" [ERROR] %s\n", payload);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
printf(" [???] Unknown message type 0x%02X\n", type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sock);
|
close(sock);
|
||||||
|
|||||||
@ -1,174 +0,0 @@
|
|||||||
/* metric_client.c - FINAL STRICT C89/C90 clean live client with unsub */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_UNSUB 0x07
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
static void send_message(int sock, unsigned char type,
|
|
||||||
const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
write(sock, header, HEADER_SIZE);
|
|
||||||
if (len > 0)
|
|
||||||
write(sock, payload, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_exact(int sock, void *buf, int n)
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = read(sock, (char *)buf + total, n - total);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_wire_comparison(void)
|
|
||||||
{
|
|
||||||
printf("\n--- Wire overhead comparison ---\n");
|
|
||||||
printf("Our protocol (METRIC_REQ 'cpu'): %d bytes\n", HEADER_SIZE + 3);
|
|
||||||
printf("Equivalent HTTP GET: 77 bytes\n");
|
|
||||||
printf("HTTP is ~13x larger\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int sock, maxfd;
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
double val;
|
|
||||||
char line[256];
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
fd_set readfds;
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = htons(PORT);
|
|
||||||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
||||||
perror("connect"); close(sock); exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Connected to 127.0.0.1:%d (FINAL clean C89 demo client)\n", PORT);
|
|
||||||
print_wire_comparison();
|
|
||||||
|
|
||||||
printf("Commands: ping | get <metric> | sub <metric> <ms> | unsub | quit\n");
|
|
||||||
printf("Metrics: cpu, memory, disk, loadavg, uptime\n\n");
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
|
|
||||||
maxfd = sock + 1;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(0, &readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
if (select(maxfd, &readfds, NULL, NULL, NULL) < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if (recv_exact(sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_exact(sock, payload, len) != len) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\r\x1b[2K");
|
|
||||||
|
|
||||||
if (type == MSG_PONG) {
|
|
||||||
printf(" [PONG] server alive\n");
|
|
||||||
} else if (type == MSG_METRIC_RESP) {
|
|
||||||
memcpy(&val, payload, sizeof(double));
|
|
||||||
printf(" [METRIC] %.2f\n", val);
|
|
||||||
} else if (type == MSG_PUSH) {
|
|
||||||
unsigned char name_len = payload[0];
|
|
||||||
payload[1 + name_len] = '\0';
|
|
||||||
memcpy(&val, payload + 1 + name_len, sizeof(double));
|
|
||||||
printf(" [PUSH] %s: %.2f\n", (char *)payload + 1, val);
|
|
||||||
} else if (type == MSG_ERROR) {
|
|
||||||
payload[len] = '\0';
|
|
||||||
printf(" [ERROR] %s\n", payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(0, &readfds)) {
|
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) break;
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(line, "quit") == 0) break;
|
|
||||||
else if (strcmp(line, "ping") == 0) {
|
|
||||||
send_message(sock, MSG_PING, NULL, 0);
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "get ", 4) == 0) {
|
|
||||||
const char *metric = line + 4;
|
|
||||||
send_message(sock, MSG_METRIC_REQ, metric, strlen(metric));
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "sub ", 4) == 0) {
|
|
||||||
char *metric = strtok(line + 4, " ");
|
|
||||||
if (metric) {
|
|
||||||
unsigned int ms = atoi(strtok(NULL, " "));
|
|
||||||
unsigned char name_len = strlen(metric);
|
|
||||||
unsigned char p[BUF_SIZE];
|
|
||||||
p[0] = name_len;
|
|
||||||
memcpy(p + 1, metric, name_len);
|
|
||||||
p[1 + name_len] = (ms >> 24) & 0xFF;
|
|
||||||
p[1 + name_len + 1] = (ms >> 16) & 0xFF;
|
|
||||||
p[1 + name_len + 2] = (ms >> 8) & 0xFF;
|
|
||||||
p[1 + name_len + 3] = ms & 0xFF;
|
|
||||||
send_message(sock, MSG_SUBSCRIBE, p, 1 + name_len + 4);
|
|
||||||
printf(" Subscribed to '%s' every %u ms\n", metric, ms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (strcmp(line, "unsub") == 0) {
|
|
||||||
send_message(sock, MSG_UNSUB, NULL, 0);
|
|
||||||
printf(" Unsubscribed (push stopped)\n");
|
|
||||||
}
|
|
||||||
else if (line[0] != '\0') {
|
|
||||||
printf(" Unknown command: '%s'\n", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
|
||||||
printf("\nDisconnected.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,173 +0,0 @@
|
|||||||
/* metric_client.c - STRICT C89/C90 client with live SUBSCRIBE/PUSH using select() */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
static void send_message(int sock, unsigned char type,
|
|
||||||
const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
write(sock, header, HEADER_SIZE);
|
|
||||||
if (len > 0)
|
|
||||||
write(sock, payload, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_exact(int sock, void *buf, int n)
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = read(sock, (char *)buf + total, n - total);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_wire_comparison(void)
|
|
||||||
{
|
|
||||||
printf("\n--- Wire overhead comparison ---\n");
|
|
||||||
printf("Our protocol (METRIC_REQ 'cpu'): %d bytes\n", HEADER_SIZE + 3);
|
|
||||||
printf("Equivalent HTTP GET: 77 bytes\n");
|
|
||||||
printf("HTTP is ~13x larger\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int sock, maxfd;
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
double val;
|
|
||||||
char line[256];
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
const char *metric;
|
|
||||||
unsigned int interval_ms;
|
|
||||||
unsigned char name_len;
|
|
||||||
fd_set readfds;
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = htons(PORT);
|
|
||||||
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
||||||
perror("connect"); close(sock); exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Connected to 127.0.0.1:%d (STRICT C89 live client)\n", PORT);
|
|
||||||
print_wire_comparison();
|
|
||||||
|
|
||||||
printf("Commands: ping | get <metric> | sub <metric> <ms> | quit\n");
|
|
||||||
printf("Metrics: cpu, memory, disk, loadavg, uptime\n\n");
|
|
||||||
|
|
||||||
maxfd = sock > 0 ? sock : 0;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(0, &readfds); /* stdin */
|
|
||||||
FD_SET(sock, &readfds); /* socket */
|
|
||||||
|
|
||||||
if (select(maxfd + 1, &readfds, NULL, NULL, NULL) < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 1. Data ready on the socket → handle PUSH / response immediately */
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if (recv_exact(sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_exact(sock, payload, len) != len) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == MSG_PONG) {
|
|
||||||
printf("\n [PONG] server alive\n");
|
|
||||||
}
|
|
||||||
else if (type == MSG_METRIC_RESP) {
|
|
||||||
memcpy(&val, payload, sizeof(double));
|
|
||||||
printf("\n [METRIC] %.2f\n", val);
|
|
||||||
}
|
|
||||||
else if (type == MSG_PUSH) {
|
|
||||||
name_len = payload[0];
|
|
||||||
payload[1 + name_len] = '\0';
|
|
||||||
memcpy(&val, payload + 1 + name_len, sizeof(double));
|
|
||||||
printf("\n [PUSH] %s: %.2f\n", (char *)payload + 1, val);
|
|
||||||
}
|
|
||||||
else if (type == MSG_ERROR) {
|
|
||||||
payload[len] = '\0';
|
|
||||||
printf("\n [ERROR] %s\n", payload);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("\n [???] Unknown type 0x%02X\n", type);
|
|
||||||
}
|
|
||||||
printf("> "); fflush(stdout); /* re-print prompt after async message */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 2. User typed something on stdin */
|
|
||||||
if (FD_ISSET(0, &readfds)) {
|
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) break;
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(line, "quit") == 0) break;
|
|
||||||
else if (strcmp(line, "ping") == 0) {
|
|
||||||
send_message(sock, MSG_PING, NULL, 0);
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "get ", 4) == 0) {
|
|
||||||
metric = line + 4;
|
|
||||||
send_message(sock, MSG_METRIC_REQ, metric, strlen(metric));
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "sub ", 4) == 0) {
|
|
||||||
metric = strtok(line + 4, " ");
|
|
||||||
if (metric) {
|
|
||||||
interval_ms = atoi(strtok(NULL, " "));
|
|
||||||
name_len = strlen(metric);
|
|
||||||
payload[0] = name_len;
|
|
||||||
memcpy(payload + 1, metric, name_len);
|
|
||||||
payload[1 + name_len] = (interval_ms >> 24) & 0xFF;
|
|
||||||
payload[1 + name_len + 1] = (interval_ms >> 16) & 0xFF;
|
|
||||||
payload[1 + name_len + 2] = (interval_ms >> 8) & 0xFF;
|
|
||||||
payload[1 + name_len + 3] = interval_ms & 0xFF;
|
|
||||||
send_message(sock, MSG_SUBSCRIBE, payload, 1 + name_len + 4);
|
|
||||||
printf(" Subscribed to '%s' every %u ms\n", metric, interval_ms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf(" Unknown command.\n");
|
|
||||||
}
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
|
||||||
printf("\nDisconnected.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,170 +0,0 @@
|
|||||||
/* metric_client_udp.c - STRICT C89/C90 UDP client with clean UI */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_UNSUB 0x07
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
static void send_to(int sock, struct sockaddr_in *to,
|
|
||||||
unsigned char type, const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
sendto(sock, header, HEADER_SIZE, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
if (len > 0)
|
|
||||||
sendto(sock, payload, len, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_from_exact(int sock, void *buf, int n)
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = recv(sock, (char *)buf + total, n - total, 0);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_wire_comparison(void)
|
|
||||||
{
|
|
||||||
printf("\n--- Wire overhead comparison ---\n");
|
|
||||||
printf("Our protocol (METRIC_REQ 'cpu'): %d bytes\n", HEADER_SIZE + 3);
|
|
||||||
printf("Equivalent HTTP GET: 77 bytes\n");
|
|
||||||
printf("HTTP is ~13x larger\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int sock, maxfd;
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
double val;
|
|
||||||
char line[256];
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
fd_set readfds;
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(PORT);
|
|
||||||
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
|
|
||||||
|
|
||||||
printf("Connected to UDP 127.0.0.1:%d (FINAL clean C89 demo client)\n", PORT);
|
|
||||||
print_wire_comparison();
|
|
||||||
|
|
||||||
printf("Commands: ping | get <metric> | sub <metric> <ms> | unsub | quit\n");
|
|
||||||
printf("Metrics: cpu, memory, disk, loadavg, uptime\n\n");
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
|
|
||||||
maxfd = sock + 1;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(0, &readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
if (select(maxfd, &readfds, NULL, NULL, NULL) < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if (recv_from_exact(sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_from_exact(sock, payload, len) != len) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\r\x1b[2K");
|
|
||||||
|
|
||||||
if (type == MSG_PONG) {
|
|
||||||
printf(" [PONG] server alive\n");
|
|
||||||
} else if (type == MSG_METRIC_RESP) {
|
|
||||||
memcpy(&val, payload, sizeof(double));
|
|
||||||
printf(" [METRIC] %.2f\n", val);
|
|
||||||
} else if (type == MSG_PUSH) {
|
|
||||||
unsigned char name_len = payload[0];
|
|
||||||
payload[1 + name_len] = '\0';
|
|
||||||
memcpy(&val, payload + 1 + name_len, sizeof(double));
|
|
||||||
printf(" [PUSH] %s: %.2f\n", (char *)payload + 1, val);
|
|
||||||
} else if (type == MSG_ERROR) {
|
|
||||||
payload[len] = '\0';
|
|
||||||
printf(" [ERROR] %s\n", payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(0, &readfds)) {
|
|
||||||
if (fgets(line, sizeof(line), stdin) == NULL) break;
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(line, "quit") == 0) break;
|
|
||||||
else if (strcmp(line, "ping") == 0) {
|
|
||||||
send_to(sock, &server_addr, MSG_PING, NULL, 0);
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "get ", 4) == 0) {
|
|
||||||
const char *metric = line + 4;
|
|
||||||
send_to(sock, &server_addr, MSG_METRIC_REQ, metric, strlen(metric));
|
|
||||||
}
|
|
||||||
else if (strncmp(line, "sub ", 4) == 0) {
|
|
||||||
char *metric = strtok(line + 4, " ");
|
|
||||||
if (metric) {
|
|
||||||
unsigned int ms = atoi(strtok(NULL, " "));
|
|
||||||
unsigned char name_len = strlen(metric);
|
|
||||||
unsigned char p[BUF_SIZE];
|
|
||||||
p[0] = name_len;
|
|
||||||
memcpy(p + 1, metric, name_len);
|
|
||||||
p[1 + name_len] = (ms >> 24) & 0xFF;
|
|
||||||
p[1 + name_len + 1] = (ms >> 16) & 0xFF;
|
|
||||||
p[1 + name_len + 2] = (ms >> 8) & 0xFF;
|
|
||||||
p[1 + name_len + 3] = ms & 0xFF;
|
|
||||||
send_to(sock, &server_addr, MSG_SUBSCRIBE, p, 1 + name_len + 4);
|
|
||||||
printf(" Subscribed to '%s' every %u ms\n", metric, ms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (strcmp(line, "unsub") == 0) {
|
|
||||||
send_to(sock, &server_addr, MSG_UNSUB, NULL, 0);
|
|
||||||
printf(" Unsubscribed (push stopped)\n");
|
|
||||||
}
|
|
||||||
else if (line[0] != '\0') {
|
|
||||||
printf(" Unknown command: '%s'\n", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("> "); fflush(stdout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
|
||||||
printf("\nDisconnected.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -55,10 +55,11 @@ static void handle_client(int client_sock) {
|
|||||||
unsigned char payload[BUF_SIZE];
|
unsigned char payload[BUF_SIZE];
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned char type = 0;
|
|
||||||
unsigned short len = 0;
|
|
||||||
if (recv_exact(client_sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
if (recv_exact(client_sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
||||||
|
|
||||||
|
unsigned char type = header[0];
|
||||||
|
unsigned short len = (header[1] << 8) | header[2];
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
if (recv_exact(client_sock, payload, len) != len) break;
|
if (recv_exact(client_sock, payload, len) != len) break;
|
||||||
}
|
}
|
||||||
@ -66,9 +67,8 @@ static void handle_client(int client_sock) {
|
|||||||
if (type == MSG_PING) {
|
if (type == MSG_PING) {
|
||||||
send_message(client_sock, MSG_PONG, NULL, 0);
|
send_message(client_sock, MSG_PONG, NULL, 0);
|
||||||
} else if (type == MSG_METRIC_REQ) {
|
} else if (type == MSG_METRIC_REQ) {
|
||||||
double val;
|
|
||||||
payload[len] = '\0'; /* null-terminate */
|
payload[len] = '\0'; /* null-terminate */
|
||||||
val = get_metric((char*)payload);
|
double val = get_metric((char*)payload);
|
||||||
if (val < 0) {
|
if (val < 0) {
|
||||||
const char *err = "Unknown metric";
|
const char *err = "Unknown metric";
|
||||||
send_message(client_sock, MSG_ERROR, err, strlen(err));
|
send_message(client_sock, MSG_ERROR, err, strlen(err));
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/* metric_server_fork.c - STRICT C89/C90 multi-client metric server using fork() */
|
/* metric_server_fork.c - C89/C90 multi-client metric server using fork() */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -58,12 +58,9 @@ static int recv_exact(int sock, void *buf, int n)
|
|||||||
|
|
||||||
static void handle_client(int client_sock, struct sockaddr_in *addr)
|
static void handle_client(int client_sock, struct sockaddr_in *addr)
|
||||||
{
|
{
|
||||||
unsigned char header[HEADER_SIZE]; /* all declarations first */
|
unsigned char header[HEADER_SIZE];
|
||||||
unsigned char payload[BUF_SIZE];
|
unsigned char payload[BUF_SIZE];
|
||||||
char client_ip[INET_ADDRSTRLEN];
|
char client_ip[INET_ADDRSTRLEN];
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
double val;
|
|
||||||
|
|
||||||
inet_ntop(AF_INET, &addr->sin_addr, client_ip, sizeof(client_ip));
|
inet_ntop(AF_INET, &addr->sin_addr, client_ip, sizeof(client_ip));
|
||||||
printf("[+] Connected from %s:%d\n", client_ip, ntohs(addr->sin_port));
|
printf("[+] Connected from %s:%d\n", client_ip, ntohs(addr->sin_port));
|
||||||
@ -72,8 +69,8 @@ static void handle_client(int client_sock, struct sockaddr_in *addr)
|
|||||||
if (recv_exact(client_sock, header, HEADER_SIZE) != HEADER_SIZE)
|
if (recv_exact(client_sock, header, HEADER_SIZE) != HEADER_SIZE)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
type = header[0];
|
unsigned char type = header[0];
|
||||||
len = (header[1] << 8) | header[2];
|
unsigned short len = (header[1] << 8) | header[2];
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
if (recv_exact(client_sock, payload, len) != len)
|
if (recv_exact(client_sock, payload, len) != len)
|
||||||
@ -86,7 +83,7 @@ static void handle_client(int client_sock, struct sockaddr_in *addr)
|
|||||||
}
|
}
|
||||||
else if (type == MSG_METRIC_REQ) {
|
else if (type == MSG_METRIC_REQ) {
|
||||||
payload[len] = '\0';
|
payload[len] = '\0';
|
||||||
val = get_metric((char *)payload);
|
double val = get_metric((char *)payload);
|
||||||
printf(" <- METRIC_REQ '%s' from %s\n", (char *)payload, client_ip);
|
printf(" <- METRIC_REQ '%s' from %s\n", (char *)payload, client_ip);
|
||||||
if (val < 0.0) {
|
if (val < 0.0) {
|
||||||
const char *err = "Unknown metric";
|
const char *err = "Unknown metric";
|
||||||
@ -114,6 +111,7 @@ int main(void)
|
|||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
|
/* Classic Unix trick to reap children automatically (no zombies) */
|
||||||
signal(SIGCHLD, SIG_IGN);
|
signal(SIGCHLD, SIG_IGN);
|
||||||
|
|
||||||
srv_sock = socket(AF_INET, SOCK_STREAM, 0);
|
srv_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
@ -149,17 +147,17 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) { /* fork failed */
|
||||||
perror("fork");
|
perror("fork");
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
}
|
}
|
||||||
else if (pid == 0) { /* CHILD */
|
else if (pid == 0) { /* CHILD process */
|
||||||
close(srv_sock);
|
close(srv_sock); /* child does not need the listening socket */
|
||||||
handle_client(client_sock, &client_addr);
|
handle_client(client_sock, &client_addr);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
else { /* PARENT */
|
else { /* PARENT process */
|
||||||
close(client_sock);
|
close(client_sock); /* parent does not need the client socket */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,221 +0,0 @@
|
|||||||
/* metric_server_fork.c - STRICT C89/C90 multi-client metric server with SUBSCRIBE/PUSH (fixed) */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define BACKLOG 10
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_UNSUB 0x07
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
static double get_metric(const char *name)
|
|
||||||
{
|
|
||||||
if (strcmp(name, "cpu") == 0) return 5.0 + (rand() % 9000)/100.0;
|
|
||||||
if (strcmp(name, "memory") == 0) return 30.0 + (rand() % 5000)/100.0;
|
|
||||||
if (strcmp(name, "disk") == 0) return 40.0 + (rand() % 5000)/100.0;
|
|
||||||
if (strcmp(name, "loadavg") == 0) return 0.5 + (rand() % 350)/100.0;
|
|
||||||
if (strcmp(name, "uptime") == 0) return (double)(time(NULL) % 100000);
|
|
||||||
return -1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void send_message(int sock, unsigned char type,
|
|
||||||
const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
if (write(sock, header, HEADER_SIZE) != HEADER_SIZE) return;
|
|
||||||
if (len > 0)
|
|
||||||
write(sock, payload, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_exact(int sock, void *buf, int n)
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = read(sock, (char *)buf + total, n - total);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_client(int client_sock, struct sockaddr_in *addr)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
char client_ip[INET_ADDRSTRLEN];
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
double val;
|
|
||||||
fd_set readfds;
|
|
||||||
struct timeval tv;
|
|
||||||
int retval;
|
|
||||||
|
|
||||||
/* Subscription state (one active subscription per client for this demo) */
|
|
||||||
char subscribed_metric[64] = {0};
|
|
||||||
unsigned int interval_ms = 0;
|
|
||||||
int have_subscription = 0;
|
|
||||||
|
|
||||||
inet_ntop(AF_INET, &addr->sin_addr, client_ip, sizeof(client_ip));
|
|
||||||
printf("[+] Connected from %s:%d\n", client_ip, ntohs(addr->sin_port));
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(client_sock, &readfds);
|
|
||||||
|
|
||||||
/* If we have a subscription, set a timeout for the next PUSH */
|
|
||||||
if (have_subscription && interval_ms > 0) {
|
|
||||||
tv.tv_sec = interval_ms / 1000;
|
|
||||||
tv.tv_usec = (interval_ms % 1000) * 1000;
|
|
||||||
retval = select(client_sock + 1, &readfds, NULL, NULL, &tv);
|
|
||||||
} else {
|
|
||||||
retval = select(client_sock + 1, &readfds, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (retval < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Timeout fired → send PUSH */
|
|
||||||
if (retval == 0 && have_subscription) {
|
|
||||||
val = get_metric(subscribed_metric);
|
|
||||||
if (val >= 0.0) {
|
|
||||||
unsigned char name_len = strlen(subscribed_metric);
|
|
||||||
payload[0] = name_len;
|
|
||||||
memcpy(payload + 1, subscribed_metric, name_len);
|
|
||||||
memcpy(payload + 1 + name_len, &val, sizeof(double));
|
|
||||||
send_message(client_sock, MSG_PUSH, payload, 1 + name_len + sizeof(double));
|
|
||||||
printf(" -> PUSH '%s' to %s\n", subscribed_metric, client_ip);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Data from client */
|
|
||||||
if (recv_exact(client_sock, header, HEADER_SIZE) != HEADER_SIZE)
|
|
||||||
break;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_exact(client_sock, payload, len) != len)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == MSG_PING) {
|
|
||||||
printf(" <- PING from %s\n", client_ip);
|
|
||||||
send_message(client_sock, MSG_PONG, NULL, 0);
|
|
||||||
}
|
|
||||||
else if (type == MSG_METRIC_REQ) {
|
|
||||||
payload[len] = '\0';
|
|
||||||
val = get_metric((char *)payload);
|
|
||||||
printf(" <- METRIC_REQ '%s' from %s\n", (char *)payload, client_ip);
|
|
||||||
if (val < 0.0) {
|
|
||||||
const char *err = "Unknown metric";
|
|
||||||
send_message(client_sock, MSG_ERROR, err, strlen(err));
|
|
||||||
} else {
|
|
||||||
send_message(client_sock, MSG_METRIC_RESP, &val, sizeof(double));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type == MSG_SUBSCRIBE) {
|
|
||||||
unsigned char name_len = payload[0];
|
|
||||||
if (name_len < sizeof(subscribed_metric)-1) {
|
|
||||||
memcpy(subscribed_metric, payload + 1, name_len);
|
|
||||||
subscribed_metric[name_len] = '\0';
|
|
||||||
}
|
|
||||||
interval_ms = (payload[1 + name_len] << 24) |
|
|
||||||
(payload[1 + name_len + 1] << 16) |
|
|
||||||
(payload[1 + name_len + 2] << 8) |
|
|
||||||
payload[1 + name_len + 3];
|
|
||||||
have_subscription = 1;
|
|
||||||
printf(" -> SUBSCRIBE '%s' every %u ms from %s\n", subscribed_metric, interval_ms, client_ip);
|
|
||||||
}
|
|
||||||
else if (type == MSG_UNSUB) {
|
|
||||||
have_subscription = 0;
|
|
||||||
printf(" <- UNSUB from %s\n", client_ip);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const char *err = "Unknown message type";
|
|
||||||
send_message(client_sock, MSG_ERROR, err, strlen(err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(client_sock);
|
|
||||||
printf("[-] Disconnected from %s\n", client_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int srv_sock, client_sock;
|
|
||||||
struct sockaddr_in server_addr, client_addr;
|
|
||||||
socklen_t client_len = sizeof(client_addr);
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
signal(SIGCHLD, SIG_IGN);
|
|
||||||
|
|
||||||
srv_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (srv_sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
server_addr.sin_port = htons(PORT);
|
|
||||||
|
|
||||||
if (bind(srv_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
perror("bind"); exit(1);
|
|
||||||
}
|
|
||||||
if (listen(srv_sock, BACKLOG) < 0) {
|
|
||||||
perror("listen"); exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Metric server (fork() + SUBSCRIBE/PUSH with select()) listening on port %d\n", PORT);
|
|
||||||
printf("Available metrics: cpu, memory, disk, loadavg, uptime\n");
|
|
||||||
printf("Commands: ping, get <metric>, sub <metric> <ms>\n\n");
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
client_sock = accept(srv_sock, (struct sockaddr *)&client_addr, &client_len);
|
|
||||||
if (client_sock < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("accept");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
if (pid < 0) {
|
|
||||||
perror("fork");
|
|
||||||
close(client_sock);
|
|
||||||
}
|
|
||||||
else if (pid == 0) {
|
|
||||||
close(srv_sock);
|
|
||||||
handle_client(client_sock, &client_addr);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
close(client_sock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(srv_sock);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,209 +0,0 @@
|
|||||||
/* metric_server_udp.c - STRICT C89/C90 UDP metric server with SUBSCRIBE/PUSH */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define MAX_SUBS 32
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_UNSUB 0x07
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
struct Subscriber {
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
char metric[64];
|
|
||||||
unsigned int interval_ms;
|
|
||||||
time_t last_push;
|
|
||||||
int active;
|
|
||||||
};
|
|
||||||
|
|
||||||
static double get_metric(const char *name)
|
|
||||||
{
|
|
||||||
if (strcmp(name, "cpu") == 0) return 5.0 + (rand() % 9000)/100.0;
|
|
||||||
if (strcmp(name, "memory") == 0) return 30.0 + (rand() % 5000)/100.0;
|
|
||||||
if (strcmp(name, "disk") == 0) return 40.0 + (rand() % 5000)/100.0;
|
|
||||||
if (strcmp(name, "loadavg") == 0) return 0.5 + (rand() % 350)/100.0;
|
|
||||||
if (strcmp(name, "uptime") == 0) return (double)(time(NULL) % 100000);
|
|
||||||
return -1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void send_to(int sock, const struct sockaddr_in *to,
|
|
||||||
unsigned char type, const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
sendto(sock, header, HEADER_SIZE, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
if (len > 0)
|
|
||||||
sendto(sock, payload, len, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_from_exact(int sock, void *buf, int n, struct sockaddr_in *from)
|
|
||||||
{
|
|
||||||
socklen_t len = sizeof(*from);
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = recvfrom(sock, (char *)buf + total, n - total, 0,
|
|
||||||
(struct sockaddr *)from, &len);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_datagram(int sock, struct Subscriber subs[])
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
struct sockaddr_in from;
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
double val;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (recv_from_exact(sock, header, HEADER_SIZE, &from) != HEADER_SIZE)
|
|
||||||
return;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_from_exact(sock, payload, len, &from) != len)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == MSG_PING) {
|
|
||||||
send_to(sock, &from, MSG_PONG, NULL, 0);
|
|
||||||
}
|
|
||||||
else if (type == MSG_METRIC_REQ) {
|
|
||||||
payload[len] = '\0';
|
|
||||||
val = get_metric((char *)payload);
|
|
||||||
if (val < 0.0) {
|
|
||||||
const char *err = "Unknown metric";
|
|
||||||
send_to(sock, &from, MSG_ERROR, err, strlen(err));
|
|
||||||
} else {
|
|
||||||
send_to(sock, &from, MSG_METRIC_RESP, &val, sizeof(double));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type == MSG_SUBSCRIBE) {
|
|
||||||
unsigned char name_len = payload[0];
|
|
||||||
unsigned int interval_ms = (payload[1 + name_len] << 24) |
|
|
||||||
(payload[1 + name_len + 1] << 16) |
|
|
||||||
(payload[1 + name_len + 2] << 8) |
|
|
||||||
payload[1 + name_len + 3];
|
|
||||||
|
|
||||||
if (name_len < 63) {
|
|
||||||
/* find free slot or overwrite existing for same client */
|
|
||||||
for (i = 0; i < MAX_SUBS; ++i) {
|
|
||||||
if (!subs[i].active || memcmp(&subs[i].addr, &from, sizeof(from)) == 0) {
|
|
||||||
memcpy(&subs[i].addr, &from, sizeof(from));
|
|
||||||
memcpy(subs[i].metric, payload + 1, name_len);
|
|
||||||
subs[i].metric[name_len] = '\0';
|
|
||||||
subs[i].interval_ms = interval_ms;
|
|
||||||
subs[i].last_push = time(NULL);
|
|
||||||
subs[i].active = 1;
|
|
||||||
printf(" -> SUBSCRIBE '%s' every %u ms from %s:%d\n",
|
|
||||||
subs[i].metric, interval_ms,
|
|
||||||
inet_ntoa(from.sin_addr), ntohs(from.sin_port));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type == MSG_UNSUB) {
|
|
||||||
for (i = 0; i < MAX_SUBS; ++i) {
|
|
||||||
if (subs[i].active && memcmp(&subs[i].addr, &from, sizeof(from)) == 0) {
|
|
||||||
subs[i].active = 0;
|
|
||||||
printf(" <- UNSUB from %s:%d\n",
|
|
||||||
inet_ntoa(from.sin_addr), ntohs(from.sin_port));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int sock, i;
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
fd_set readfds;
|
|
||||||
struct timeval tv;
|
|
||||||
struct Subscriber subs[MAX_SUBS];
|
|
||||||
time_t now;
|
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
memset(subs, 0, sizeof(subs));
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
server_addr.sin_port = htons(PORT);
|
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
||||||
perror("bind"); exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("UDP Metric server listening on port %d (fire-and-forget mode)\n", PORT);
|
|
||||||
printf("Available metrics: cpu, memory, disk, loadavg, uptime\n");
|
|
||||||
printf("Commands: ping, get <metric>, sub <metric> <ms>, unsub\n\n");
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
tv.tv_sec = 0;
|
|
||||||
tv.tv_usec = 100000; /* 100 ms tick for pushes */
|
|
||||||
|
|
||||||
if (select(sock + 1, &readfds, NULL, NULL, &tv) < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
handle_datagram(sock, subs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Periodic PUSHes */
|
|
||||||
now = time(NULL);
|
|
||||||
for (i = 0; i < MAX_SUBS; ++i) {
|
|
||||||
if (subs[i].active) {
|
|
||||||
if (now - subs[i].last_push >= subs[i].interval_ms / 1000) {
|
|
||||||
double val = get_metric(subs[i].metric);
|
|
||||||
if (val >= 0.0) {
|
|
||||||
unsigned char name_len = strlen(subs[i].metric);
|
|
||||||
unsigned char p[BUF_SIZE];
|
|
||||||
p[0] = name_len;
|
|
||||||
memcpy(p + 1, subs[i].metric, name_len);
|
|
||||||
memcpy(p + 1 + name_len, &val, sizeof(double));
|
|
||||||
send_to(sock, &subs[i].addr, MSG_PUSH,
|
|
||||||
p, 1 + name_len + sizeof(double));
|
|
||||||
}
|
|
||||||
subs[i].last_push = now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -1,157 +0,0 @@
|
|||||||
/* perf_client_udp.c - STRICT C89/C90 UDP performance tester for the metric protocol */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define PORT 9999
|
|
||||||
#define BUF_SIZE 1024
|
|
||||||
#define TEST_DURATION 30 /* seconds */
|
|
||||||
|
|
||||||
#define MSG_PING 0x01
|
|
||||||
#define MSG_PONG 0x02
|
|
||||||
#define MSG_METRIC_REQ 0x03
|
|
||||||
#define MSG_METRIC_RESP 0x04
|
|
||||||
#define MSG_SUBSCRIBE 0x05
|
|
||||||
#define MSG_PUSH 0x06
|
|
||||||
#define MSG_UNSUB 0x07
|
|
||||||
#define MSG_ERROR 0xFF
|
|
||||||
|
|
||||||
#define HEADER_SIZE 3
|
|
||||||
|
|
||||||
static void send_to(int sock, struct sockaddr_in *to,
|
|
||||||
unsigned char type, const void *payload, unsigned short len)
|
|
||||||
{
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
header[0] = type;
|
|
||||||
header[1] = (len >> 8) & 0xFF;
|
|
||||||
header[2] = len & 0xFF;
|
|
||||||
sendto(sock, header, HEADER_SIZE, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
if (len > 0)
|
|
||||||
sendto(sock, payload, len, 0, (struct sockaddr *)to, sizeof(*to));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int recv_from_exact(int sock, void *buf, int n)
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
while (total < n) {
|
|
||||||
int r = recv(sock, (char *)buf + total, n - total, 0);
|
|
||||||
if (r <= 0) return -1;
|
|
||||||
total += r;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int sock, maxfd, flood_mode = 0, duration = TEST_DURATION;
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
unsigned char header[HEADER_SIZE];
|
|
||||||
unsigned char payload[BUF_SIZE];
|
|
||||||
double val;
|
|
||||||
unsigned char type;
|
|
||||||
unsigned short len;
|
|
||||||
fd_set readfds;
|
|
||||||
time_t start, now;
|
|
||||||
unsigned long count = 0;
|
|
||||||
const char *metric = "cpu";
|
|
||||||
unsigned int interval_ms = 10; /* 10 ms pushes in subscribe mode */
|
|
||||||
|
|
||||||
if (argc > 1 && strcmp(argv[1], "--flood") == 0) flood_mode = 1;
|
|
||||||
|
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (sock < 0) { perror("socket"); exit(1); }
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET;
|
|
||||||
server_addr.sin_port = htons(PORT);
|
|
||||||
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
|
|
||||||
|
|
||||||
printf("=== UDP Metric Performance Test (C89) ===\n");
|
|
||||||
printf("Server: 127.0.0.1:%d\n", PORT);
|
|
||||||
if (flood_mode) {
|
|
||||||
printf("Mode: FLOOD (rapid get %s)\n", metric);
|
|
||||||
} else {
|
|
||||||
printf("Mode: SUBSCRIBE '%s' every %u ms\n", metric, interval_ms);
|
|
||||||
}
|
|
||||||
printf("Test duration: %d seconds\n\n", duration);
|
|
||||||
|
|
||||||
/* Subscribe or start flooding */
|
|
||||||
if (flood_mode) {
|
|
||||||
/* nothing extra needed */
|
|
||||||
} else {
|
|
||||||
unsigned char p[BUF_SIZE];
|
|
||||||
unsigned char name_len = strlen(metric);
|
|
||||||
p[0] = name_len;
|
|
||||||
memcpy(p + 1, metric, name_len);
|
|
||||||
p[1 + name_len] = (interval_ms >> 24) & 0xFF;
|
|
||||||
p[1 + name_len + 1] = (interval_ms >> 16) & 0xFF;
|
|
||||||
p[1 + name_len + 2] = (interval_ms >> 8) & 0xFF;
|
|
||||||
p[1 + name_len + 3] = interval_ms & 0xFF;
|
|
||||||
send_to(sock, &server_addr, MSG_SUBSCRIBE, p, 1 + name_len + 4);
|
|
||||||
printf("Subscribed... counting PUSHes\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
start = time(NULL);
|
|
||||||
maxfd = sock + 1;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(sock, &readfds);
|
|
||||||
|
|
||||||
if (select(maxfd, &readfds, NULL, NULL, NULL) < 0) {
|
|
||||||
if (errno == EINTR) continue;
|
|
||||||
perror("select");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(sock, &readfds)) {
|
|
||||||
if (recv_from_exact(sock, header, HEADER_SIZE) != HEADER_SIZE) break;
|
|
||||||
|
|
||||||
type = header[0];
|
|
||||||
len = (header[1] << 8) | header[2];
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
if (recv_from_exact(sock, payload, len) != len) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == MSG_PUSH) {
|
|
||||||
unsigned char name_len = payload[0];
|
|
||||||
payload[1 + name_len] = '\0';
|
|
||||||
memcpy(&val, payload + 1 + name_len, sizeof(double));
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
else if (type == MSG_METRIC_RESP && flood_mode) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
else if (type == MSG_ERROR) {
|
|
||||||
printf(" [ERROR] %.*s\n", len, payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
now = time(NULL);
|
|
||||||
if (now - start >= duration) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!flood_mode) {
|
|
||||||
send_to(sock, &server_addr, MSG_UNSUB, NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n=== Results ===\n");
|
|
||||||
printf("Duration: %lu seconds\n", (unsigned long)(now - start));
|
|
||||||
printf("Messages received: %lu\n", count);
|
|
||||||
printf("Throughput: %.1f messages/second\n", (double)count / (now - start));
|
|
||||||
if (flood_mode) {
|
|
||||||
printf("(This is the rate of successful round-trips)\n");
|
|
||||||
} else {
|
|
||||||
printf("(UDP PUSH rate — best-effort)\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user