服务器之家

服务器之家 > 正文

使用C++制作简单的web服务器(续)

时间:2021-02-22 12:09     来源/作者:C++教程网

增加功能:
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服务器(续)

以上所述就是本文对于使用C++实现简单web服务器的全部代码了,希望大家能够喜欢。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部