應用C++制造簡略的web辦事器(續)。本站提示廣大學習愛好者:(應用C++制造簡略的web辦事器(續))文章只能為提供參考,不一定能成為您想要的結果。以下是應用C++制造簡略的web辦事器(續)正文
增長功效:
1、從文件中讀取網頁並前往給客戶端,而不是把網頁代碼寫逝世在代碼中。
局限:
1、還不克不及依據URL地址中的參數,拜訪指定頁面
2、依然是單線程
//*****************************************************************************
//@ProjectName ZYhttpd
//@Description my http server
//@Author NicoleRobin
//@Date 2015/02/09
//*****************************************************************************
#include <cstdio>
#include <string>
#include <fstream>
#include <WinSock2.h>
using namespace std;
#define BUFFER_SIZE 1024
#define HOST "127.0.0.1"
#define PORT 81
#define HEADER "\
HTTP/1.1 200 OK\r\n\
Content-Type: text/html; charset=UTF-8\r\n\
Server: ZYhttp_v1.0.1\r\n\
Content-Length: %ld\r\n\r\n\
"
const string strPath = "index.html";
#pragma comment(lib, "WS2_32")
// get file size
long GetFileLength(string strPath);
// thread function
int main(int argc, char **argv)
{
// define and init an server sockaddr
sockaddr_in addrServer;
addrServer.sin_family = AF_INET;
addrServer.sin_addr.S_un.S_addr = INADDR_ANY;
addrServer.sin_port = htons(PORT);
// init socket dll
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if (WSAStartup(socketVersion, &wsaData) != 0)
{
printf("Init socket dll error!");
exit(1);
}
// create socket
SOCKET socketServer = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == socketServer)
{
printf("Create socket error!");
exit(1);
}
// bind server socket host
if (SOCKET_ERROR == bind(socketServer, (LPSOCKADDR)&addrServer, sizeof(addrServer)))
{
printf("Bind server host failed!");
exit(1);
}
// listen
if (SOCKET_ERROR == listen(socketServer, 10))
{
printf("Listen failed!");
exit(1);
}
while (true)
{
printf("Listening ... \n");
sockaddr_in addrClient;
int nClientAddrLen = sizeof(addrClient);
SOCKET socketClient = accept(socketServer, (sockaddr*)&addrClient, &nClientAddrLen);
if (SOCKET_ERROR == socketClient)
{
printf("Accept failed!");
break;
}
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
if (recv(socketClient, buffer, BUFFER_SIZE, 0) < 0)
{
printf("Recvive data failed!");
break;
}
printf("Recv data : \n%s", buffer);
/*
// response
memset(buffer, 0, BUFFER_SIZE);
ifstream fin(strPath.c_str(), ios::in | ios::binary);
if (fin.is_open())
{
char szTmp[512] = "";
fin.read(szTmp, 511);
sprintf_s(buffer, HEADER, GetFileLength(strPath), szTmp);
if (send(socketClient, buffer, strlen(buffer), 0) < 0)
{
printf("Send data failed!");
break;
}
}
fin.close();
*/
// response
// send header
memset(buffer, 0, BUFFER_SIZE);
sprintf_s(buffer, HEADER, GetFileLength(strPath));
if (send(socketClient, buffer, strlen(buffer), 0) < 0)
{
printf("Send data failed!");
break;
}
ifstream fin(strPath.c_str(), ios::in | ios::binary);
if (fin.is_open())
{
memset(buffer, 0, BUFFER_SIZE);
while (fin.read(buffer, BUFFER_SIZE - 1))
{
if (send(socketClient, buffer, strlen(buffer), 0) < 0)
{
printf("Send data failed!");
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
if (send(socketClient, buffer, strlen(buffer), 0) < 0)
{
printf("Send data failed!");
break;
}
}
fin.close();
closesocket(socketClient);
}
closesocket(socketServer);
WSACleanup();
return 0;
}
long GetFileLength(string strPath)
{
ifstream fin(strPath.c_str(), ios::in | ios::binary);
fin.seekg(0, ios_base::end);
streampos pos = fin.tellg();
long lSize = static_cast<long>(pos);
fin.close();
return lSize;
/*
long lSize = 0;
ifstream fin(strPath.c_str(), ios::in | ios::binary);
char szBuf[1024*1000] = "";
while (fin.read(szBuf, 1024 * 1000 - 1))
{
lSize += strlen(szBuf);
memset(szBuf, 0, 1024*1000);
}
fin.close();
lSize += strlen(szBuf);
return lSize;
*/
}
演示圖
以上所述就是本文關於應用C++完成簡略web辦事器的全體代碼了,願望年夜家可以或許愛好。