Skip to content

Instantly share code, notes, and snippets.

@passcod
Created Mar 1, 2019
Embed
What would you like to do?
// reduced further from
// https://github.com/mafintosh/echo-servers.c/blob/master/tcp-echo-server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
#define on_error(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); exit(1); }
int main (int argc, char *argv[]) {
if (argc < 2) on_error("0");
int port = atoi(argv[1]);
int server_fd, client_fd, err;
struct sockaddr_in server, client;
char buf[BUFFER_SIZE];
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) on_error("1");
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
int opt_val = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);
err = bind(server_fd, (struct sockaddr *) &server, sizeof(server));
if (err < 0) on_error("2");
err = listen(server_fd, 128);
if (err < 0) on_error("3");
printf("OK");
while (1) {
socklen_t client_len = sizeof(client);
client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
if (client_fd < 0) on_error("4");
while (1) {
int read = recv(client_fd, buf, BUFFER_SIZE, 0);
if (!read) break; // done reading
if (read < 0) on_error("5");
printf("%s", buf);
//err = send(client_fd, buf, read, 0);
//if (err < 0) on_error("6");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment