Sockets and HTTP

Hi All,

Im coding a small HTTP server for a project im working on (home automation :D) and ive hit a brick wall. I have managed to get the server working and running. I can accept connections from Firefox, but if I try with say google chrome, the page will not load!

Here are the main 3 functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
int init(int port) {
	/**
	 * This function initialises the server
	 * It creates the initial sock, binds 
	 * and then listens on the defined port
	 * Returns: Socket Descriptor (INT)
	 */
	int httpd;
	 
	struct sockaddr_in mySockAddr, remoteAddr;
	socklen_t remoteAddr_len;


	mySockAddr.sin_family = AF_INET;
	mySockAddr.sin_port = htons(port);
	
	cout << "Initialising Server...";
	logEvent(1, "[NOTICE] Initialising Server...", false);
	
	httpd = socket(AF_INET, SOCK_STREAM, 0);

	inet_aton("127.0.0.1", &mySockAddr.sin_addr);

	if(httpd == -1){
		logEvent(1,("[ERROR] Creating Socket failed."));
	}

	if(bind(httpd,(struct sockaddr*) &mySockAddr, sizeof(mySockAddr)) < 0) {
		cout << errno << endl;
		
		logEvent(1,("[ERROR] Binding on port "+convertInt(port)+" failed."));
	}

	if(listen(httpd, MAX_CLIENTS) == -1) {
		logEvent(1,("[ERROR] listening on port "+convertInt(port)+" failed."));
	} else {
		cout << "\t[OK]" << endl;
		logEvent(1, ("[NOTICE] Server started on port "+convertInt(port)), false);
	}
	 return httpd;
}


int handle_connection(int ClientSocket) {
	socklen_t len;
	struct sockaddr_storage addr;
	char ipstr[INET6_ADDRSTRLEN];
	int port;
	const char* buf;
	const char* body;
	
	len = sizeof addr;
	getpeername(ClientSocket, (struct sockaddr*)&addr, &len);

	// deal with both IPv4 and IPv6:
	if (addr.ss_family == AF_INET) {
		struct sockaddr_in *ClientSocket = (struct sockaddr_in *)&addr;
		port = ntohs(ClientSocket->sin_port);
		inet_ntop(AF_INET, &ClientSocket->sin_addr, ipstr, sizeof ipstr);
	}
	string IP;
	IP.assign(ipstr);
	logEvent(1, ("Connection Established with: "+IP));
	
	
	
	buf = "HTTP/1.0 200 OK\r\n";
	send(ClientSocket, buf, strlen(buf), 0);
	
	buf = "Server: eoPanel Server\r\n";
	send(ClientSocket, buf, strlen(buf), 0);
	
	buf = "Content-Type: text/html; charset=UTF-8\r\n";
	send(ClientSocket, buf, strlen(buf), 0);
	
	buf = "Content-Length: 20\r\n";
	send(ClientSocket, buf, strlen(buf), 0);
	
	buf = "\r\n";
	send(ClientSocket, buf, strlen(buf), 0);
	body = "<h2> Welcome </h2>";
	send(ClientSocket, body, strlen(body), 0);
	
	
	close(ClientSocket);
}

string convertInt(int number){
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}



int main(int argc, char* argv[]) {
	
	int port;
	struct sockaddr_in remoteAddr;
		socklen_t remoteAddr_len;
	
	if(argv[1] > 0) {
		port = atoi(argv[1]);
	} else {
		port = DEFAULT_PORT;
	}
	
	ServerSocket = init(port);
	
	if(ServerSocket == -1) {
		logEvent(1,("[ERROR] Main did not receive a correct socketID from init();"));
		exit(1);
	}
	
	while(1) {
		ClientSocket = accept(ServerSocket, (struct sockaddr*) &remoteAddr, &remoteAddr_len);
	
		handle_connection(ClientSocket);
	}
	return 0;
}


Basically init() loads up a listening socket, and handle_connection() is where sending all the HTTP stuff goes down. I have tried working with telnet and it sends all the right data.
1
2
3
4
5
6
7
-- Telnet --
HTTP/1.0 200 OK
Server: eoPanel Server
Content-Type: text/html; charset=UTF-8
Content-Length: 20

<h2> Welcome </h2>

Anyone spot an obvious error?
Sorry, the error message I get in Chrome is: Error 101 (net::ERR_CONNECTION_RESET): Unknown error.
I don't know that it should make much difference but your Content-Length is 20 but your data is only 18. Maybe Chrome is waiting for the other 2?
Ah, its gonna be one of those days. That was exactly the problem. Apparently chrome doesnt like not knowing how much data it is to receive, firefox is slightly more laid back!.

Time to make a function to actually generate a response instead of hardcoding it!.
Topic archived. No new replies allowed.