#include "tnplib.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
inline void D(const char *errormessage) {
#ifdef _DEBUG
cerr << errormessage << endl;
#endif
}
static int counter = 0;
void SimpleHTTPServer(int port) {
char buffer[256];
int status;
SOCKET serve = TCPStartServer(port, 10);
for (;;) {
SOCKET visitor = TCPWaitForConnection(serve);
status = TCPRecvLine(visitor, buffer, sizeof(buffer) );
if (status<0) continue;
D(buffer);
do {
status = TCPRecvDumpLine(visitor);
} while (status>0);
TCPSendLine(visitor, "HTTP/1.1 200 Ok\r\n");
TCPSendLine(visitor, "Server: TNP Simple Demo Server\r\n");
TCPSendLine(visitor, "Connection: close\r\n");
TCPSendLine(visitor, "Cache-Control: no-cache\r\nExpires: 0\r\nETag: \"\"\r\n");
TCPSendLine(visitor, "Content-Type: text/html; charset=ISO-8859-1\r\n");
TCPSendLine(visitor, "\r\n");
TCPSendLine(visitor, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\r\n");
TCPSendLine(visitor, "<html>\r\n<head>\r\n");
TCPSendLine(visitor, "<meta http-equiv=\"Content-type\" content=\"text/html; charset=iso-8859-1\">\r\n");
TCPSendLine(visitor, "<title>A simple dynamic Web page</title>\r\n</head>\r\n<body>\r\n");
TCPSendLine(visitor, "<p>");
counter++; itoa(counter, buffer, 10); TCPSendLine(visitor, buffer);
TCPSendLine(visitor, "</p>\r\n");
TCPSendLine(visitor, "</body>\r\n</html>\r\n");
TCPPrepClose(visitor);
TCPStopClient(visitor);
D("Done");
}
}
int main()
{
SocketLibStart();
SimpleHTTPServer(8088);
SocketLibEnd();
return(0);
}