I'm trying to make a tiny http server in c but I got CONNRESET errors with httperf, why ?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define SOCKERROR -1
#define SD_RECEIVE 0
#define SD_SEND 1
#define SD_BOTH 2
int server;
int client;
...
int main(int argc, char *argv[])
{
int status;
int accepted;
struct addrinfo hint;
struct addrinfo *info;
struct sockaddr addr;
socklen_t addrsize;
int yes = 1;
...
// client
addrsize = sizeof addr;
while (1)
{
memset(&accepted, 0, sizeof accepted);
memset(&addr, 0, sizeof addr);
accepted = accept(server, &addr, &addrsize);
if (accepted == SOCKERROR) {
warn("Accept", errno);
} else {
shutdown(accepted, SD_SEND);
close(accepted);
}
}
// shutdown
...
return EXIT_SUCCESS;
}
You're closing the socket as soon as you accept
it. So the connection is reset on the other end of it.
If you want to talk to an HTTP client, you're going to have to parse the incoming HTTP requests, and reply with valid HTTP data. (Warning: that's not trivial.)
Please read this article: nweb: a tiny, safe Web server (static pages only) for example, it has a good rundown of what needs to be done for a minimal HTTP server.