make it so

This commit is contained in:
2017-09-18 09:06:18 +02:00
commit 465f405cd1
4 changed files with 80 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

15
Makefile Normal file
View File

@@ -0,0 +1,15 @@
TARGET=862
BUILDDIR=bin/
PREFIX=/usr/local/bin/
MAIN=main.c
override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wuninitialized -pedantic -std=gnu11
all: main.c
mkdir -p $(BUILDDIR)
$(CC) $(MAIN) -o $(BUILDDIR)$(TARGET) $(CFLAGS)
install: all
install $(BUILDDIR)$(TARGET) $(PREFIX)$(TARGET)
uninstall:
rm -rf $(PREFIX)$(TARGET)

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# 862
An implementation of [RFC 862](https://tools.ietf.org/html/rfc862), the “echo”
service, for the heck of it.
## Limitations
This only echos back messages up to 256 characters. That really ought to be
enough for everyone. It also doesnt notice when a client dies, which is not
great.

54
main.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
void die(const char *msg) {
perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
int sockfd;
int newsockfd;
int port = 7;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
char msg[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) die("error opening socket.");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) die("error binding socket.");
clilen = sizeof(cli_addr);
while (1) {
listen(sockfd, 5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) die("error accepting");
while (1) {
memset(msg, 0, sizeof(msg));
n = read(newsockfd, msg, 256);
if (n < 0) break;
n = write(newsockfd, msg, strlen(msg));
if (n < 0) break;
}
close(newsockfd);
}
close(sockfd);
return 0;
}