Skip to content

Instantly share code, notes, and snippets.

@LevitatingBusinessMan
Last active May 10, 2024 23:48
Show Gist options
  • Save LevitatingBusinessMan/e052634f1ea3b09c58fdff057a781885 to your computer and use it in GitHub Desktop.
Save LevitatingBusinessMan/e052634f1ea3b09c58fdff057a781885 to your computer and use it in GitHub Desktop.
#include <asm-generic/socket.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <netinet/tcp.h>
#define NONAGLE 1
#define TIMEMS() \
({ \
struct timeval _timeval; \
gettimeofday(&_timeval, NULL); \
_timeval.tv_usec; \
})
void main() {
pid_t pid = fork();
struct timeval as;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(1337);
if (pid == 0) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
int nonagle = NONAGLE;
setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, sizeof(nonagle));
sleep(1);
connect(sock_fd, &addr, sizeof(addr));
perror("connect");
for (;;) {
char c = 'A';
write(sock_fd, &c, 1);
perror("write");
write(sock_fd, &c, 1);
perror("write");
printf("written %ld\n", TIMEMS());
sleep(1);
}
} else {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
int reuse = 1;
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
bind(sock_fd, &addr, sizeof(addr));
perror("bind");
listen(sock_fd, 1);
perror("listen");
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(sock_fd, &client_addr, &client_len);
perror("accept");
char buf[256];
for (;;) {
int n = read(client_fd, &buf, sizeof(buf));
perror("read");
printf("read %d %ld\n", n, TIMEMS());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment