Knowledge Base Nr: 00239 webserver.cpp - http://www.swe-kaiser.de
Downloads:
MFC: einfacher Webserver
SocketServer mit MFC-Sockets für schnellen Zugriff und Befehlsabarbeitung in C++
z.b.: http://localhost:4711/testaufruf
#include "../cpp_classes/SocketSupport.h" //CSocketSupport : public CAsyncSocket
class CMySocketServer;
const UINT LISTENERPORT = 4711;
CMySocketServer* g_serversock = 0;
class CMySocketServer : public CSocketSupport
{
public:
CRemotewebDlg* m_pDlg;
CMySocketServer(CRemotewebDlg* pDlg) : CSocketSupport(true)
{
m_pDlg = pDlg;
}
virtual ~CMySocketServer() {}
virtual void OnAccept(int nErrorCode)
{
CMySocketServer* pClientsock = new CMySocketServer(m_pDlg);
BOOL bSucc = Accept(*pClientsock);
}
virtual void OnSocketReceive(const char* lpszData, int nDataLen)
{
DoCmd(lpszData);
}
void DoCmd(const char* lpszCmd);
};
void CMySocketServer::DoCmd(const char* lpszCmd)
{
CString strAnswer;
bool bNoContent = true;
if (bNoContent)
{
strAnswer.Format(
"HTTP/1.1 204 No Content\n\r"
"Content-type: text/html\n\r"
"Server: RemoteWeb 1.0\n\r"
"Pragma: no-cache\n\r"
"Cache-Control: no-cache, must-revalidate\n\r"
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\n\r"
"\n\r");
}
else
{
strAnswer.Format(
"HTTP/1.1 200 OK\n\r"
"Content-type: text/html\n\r"
"Server: RemoteWeb 1.0\n\r"
"Pragma: no-cache\n\r"
"Cache-Control: no-cache, must-revalidate\n\r"
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\n\r"
"\n\r"
""
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\r"
"\n\r"
"<html>\n\r"
"<head>\n\r"
" <title>Test RemoteWeb</title>\n\r"
"</head>\n\r"
"<body>\n\r"
"<h1>Test RemoteWeb: %s</h1>\n\r"
"</body>\n\r"
"</html>\n\r"
, lpszCmd);
}
int nSend = Send(strAnswer, strAnswer.GetLength());
//wichtig - der browser wartet sonst ewig
Close();
}
void CRemotewebDlg::OnButtonStart()
{
OnStop();
g_serversock = new CMySocketServer(this);
BOOL bSucc = TRUE;
bSucc = g_serversock->Create(LISTENERPORT);
//zulassen: socket sofort wiederbenutzbar
int i = 1;
bSucc = g_serversock->SetSockOpt(SO_REUSEADDR, &i, sizeof(i));
int i2 = 1;
bSucc = g_serversock->SetSockOpt(SO_KEEPALIVE, &i2, sizeof(i2));
bSucc = g_serversock->Listen(5);
}
void CRemotewebDlg::OnStop()
{
if (g_serversock)
{
g_serversock->Close();
delete g_serversock;
g_serversock = 0;
}
}
////////// bild schicken:
if (bSendPic)
{
int nSizeBytes = 0;
const int MAXPICSIZE = 800*600*3;
unsigned char* pFileContent = new unsigned char[MAXPICSIZE];
FILE* fp = fopen("e:/projects/internet/my_home/kaiserreich/images/k_images/dummy.jpg", "rb");
if (fp)
{
nSizeBytes = fread(pFileContent, 1, MAXPICSIZE, fp);
fclose(fp);
}
strAnswer.Format(
"HTTP/1.1 200 OK\n"
"Server: VideoWebServer 1.0\n"
"Last-Modified: Tue, 22 Jun 2004 21:07:02 GMT\n"
"Pragma: no-cache\n"
"Cache-Control: no-cache, must-revalidate\n"
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\n"
"Content-Length: %d\n"
"Content-Type: image/jpeg\n"
"\n"
, nSizeBytes
);
nSend = Send(strAnswer, strAnswer.GetLength()); //header
nSend = Send(pFileContent, nSizeBytes); //bilddaten
delete pFileContent;
}
###### seite umlenken
else if (bLocationHeader)
{
strAnswer.Format(
"HTTP/1.1 302 Found\n"
"Server: VideoWebServer 1.0\n"
"Location: http://krserver/pda/p_weblivecam.php3\n"
"Connection: close\n"
"\n"
);
nSend = Send(strAnswer, strAnswer.GetLength());
}