C ++ - a simple server that sends simple HTML to clients
Now I'm just cheating on this and I'm not sure why this isn't working.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <cassert>
const char html[] = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-type: text/html\r\n"
"\r\n"
"<html>\r\n"
"<head>\r\n"
"<title>Hello, world!</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h1>Hello, world!</h1>\r\n"
"</body>\r\n"
"</html>\r\n\r\n";
int main() {
WSADATA wsa;
assert( WSAStartup( MAKEWORD( 2, 2 ), &wsa ) == 0 );
addrinfo *res = NULL;
addrinfo hints;
ZeroMemory( &hints, sizeof( hints ) );
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
assert( getaddrinfo( NULL, "80", &hints, &res ) == 0 );
SOCKET s = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
assert( s != INVALID_SOCKET );
assert( bind( s, res->ai_addr, (int)res->ai_addrlen ) != SOCKET_ERROR );
assert( listen( s, SOMAXCONN ) != SOCKET_ERROR );
SOCKET client = accept( s, NULL, NULL );
assert( client != INVALID_SOCKET );
char buffer[512];
int bytes;
bytes = recv( client, buffer, 512, 0 );
for ( int i = 0; i < bytes; ++i ) {
std::cout << buffer[i];
}
assert( send( client, html, strlen( html ) - 1, 0 ) > 0 );
assert( shutdown( client, SD_BOTH ) != SOCKET_ERROR );
closesocket( client );
WSACleanup();
return 0;
}
When I compile and run this and then navigate to 127.0.0.1 in my browser, I get this in my console:
GET / HTTP / 1.1
Host: 127.0.0.1
Connection: keep-alive
User-Agent: Mozilla / 5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit / 530.5 (K HTML, like Gecko) Chrome / 2.0.172.8 Safari / 530.5
Cache-Control: max-age = 0
Accept: application / xml, application / xhtml + xml, text / html; q = 0.9, text / plain; q = 0.8, image / png, /; q = 0.5
Accept-Encoding: gzip, deflate, bzip2, sdch
Accept-Language: en-US, en; q = 0.8
Accept-Charset: ISO-8859-1, utf-8; q = 0.7, *; q = 0.3
EDIT . I have updated the HTML I am posting. I just tested this with Mozilla Firefox and Google Chrome and it works in Firefox but not Chrome!
EDIT 2 . It looks like the reason it worked in Firefox but not Chrome was because Firefox renders the HTML as received while Chrome waits for the connection to close before doing any rendering action. I added some code to close the socket and it worked. I have updated my code with a working source.
a source to share
You need to send the status bar:
HTTP / 1.1 200 OK
preceding your response headers.
See Fiddler (www.fiddler2.com) for a better understanding of what valid HTTP responses look like.
As for your later editing, all browsers wait for a certain amount of data before starting rendering; Chrome's limitation is different from Firefox. If you set Content-Length or HTTP Chunked encoding, you would see the correct behavior.
Have a look at Mongoose http://code.google.com/p/mongoose/ This is a standalone library that has a multithreaded http webserver and has a super simple api (but complete). After a few minutes, I was able to link it to an already existing application.
I had the same question today (to give my C ++ app the end of the web) Providing a C ++ app with HTTP server functionality
a source to share