I'm trying to write a webserver in C. I know I could use many different libraries for that, but I want it to run only with winsock. The server and a client can send data back and forth but loading the webpage is impossible. Every time I get a 'server unreachable' message, but on the server I get the message that the body was sent. What am I doing wrong?
edit I'm using chrome and microsoft edge
#include <stdio.h>
#include <conio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
void error(const char* err){
printf("ERROR: %s\n", err);
exit(1);
}
char* readline(SOCKET s){
FILE * f = tmpfile();
char rec;
int i=0;
while(recv(s, &rec, 1, 0)>0){
i++;
if(rec!='\r' && rec!='\n')
fwrite(&rec, 1, 1, f);
if(rec=='\r' || rec=='\n')
break;
}
if(i==0){
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_END);
long long size = ftell(f);
fseek(f, 0, SEEK_SET);
char * d = (char*) malloc(size);
memset (d,0,size);
fread(d, 1, size, f);
fclose(f);
return d;
}
void flush(SOCKET s){
while(recv(s, NULL, 1, 0)>0){
}
}
void main(){
// CREATE SERVER
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
error("WSAStartup");
SOCKET s;
if((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
error("socket");
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(80);
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
error("bind");
// SERVER CREATED
while(1){
listen(s, 1);
int c = sizeof(struct sockaddr_in);
SOCKET csock;
struct sockaddr_in client;
if((csock = accept(s, (struct sockaddr*)&client, &c))==INVALID_SOCKET)
continue;
char *client_ip = inet_ntoa(client.sin_addr);
printf("Incomming connection: %s\n", client_ip);
char * head = readline(csock);
if(strncmp(head, "GET", 3)==0){
flush(csock);
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
"<html><head><title>test</title>"
"</head><body>Test123</body></html>";
send(csock, response, strlen(response), 0);
printf("%s\n", "HTML body sended");
}else if(strncmp(head, "HEAD", 4)==0){
flush(csock);
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n";
send(csock, response, strlen(response), 0);
printf("%s\n", "HTML head sended");
}
closesocket(csock);
}
closesocket(s);
WSACleanup();
}