Created
January 27, 2015 00:59
-
-
Save thentenaar/f9318b38e79300cb5617 to your computer and use it in GitHub Desktop.
Simple echo server: Refactor #6: Move the work loop back out into its own function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple echo server | |
* | |
* This program listens for connections on the | |
* loopback interface, on port 9999. Upon connecting, | |
* The first line we read, of up to BUF_LEN bytes, is | |
* then sent back to the sender, and the connection | |
* is closed. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#define BUF_LEN 25 | |
static char buf[BUF_LEN + 1]; | |
/** | |
* Read data from the given file descriptor, | |
* and echo it back out. | |
*/ | |
static void handle_connection(int fd) | |
{ | |
int i; | |
unsigned int bytes_read = 0; | |
/** | |
* Read data until the buffer is full, we get an EOF, or | |
* we've read a '\n'. | |
*/ | |
readit: | |
if ((i = recv(fd, buf + bytes_read, BUF_LEN - bytes_read, 0)) > 0) { | |
bytes_read += i; | |
if (bytes_read < BUF_LEN && !strchr(buf, '\n')) | |
goto readit; | |
} | |
if (i < 0) { | |
if (errno == EINTR) | |
goto readit; | |
perror("recv"); | |
return; | |
} | |
/* Echo data if we got it */ | |
if (bytes_read) { | |
buf[bytes_read + 1] = 0; | |
printf("Read: %s\n", buf); | |
if (send(fd, buf, bytes_read, 0) < 0) | |
perror("send"); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int fd, c_fd = 0, i = 1; | |
struct sockaddr_in sa; | |
/* Open a TCP socket */ | |
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { | |
perror("socket"); | |
goto done; | |
} | |
/* Let's ensure we can reuse our address */ | |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(int)); | |
/* This will bind to localhost:9999 */ | |
memset(&sa, 0, sizeof(sa)); | |
sa.sin_family = AF_INET; | |
sa.sin_port = htons(9999); | |
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) { | |
perror("bind"); | |
goto done; | |
} | |
/* Listen for one connection at a time */ | |
if (listen(fd, 1) < 0) { | |
perror("listen"); | |
goto done; | |
} | |
loop: | |
/* Clear the buffer */ | |
memset(buf, 0, BUF_LEN+1); | |
if ((c_fd = accept(fd, NULL, NULL)) < 0) { | |
perror("accept"); | |
goto loop; | |
} | |
/* We've got a live one. */ | |
printf("Got connection\n"); | |
handle_connection(c_fd); | |
close(c_fd); | |
goto loop; | |
done: | |
if (c_fd) close(c_fd); | |
close(fd); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment