使用C++製作簡單的web伺服器_C 語言

來源:互聯網
上載者:User

僅僅實現了web的功能,局限性比較大,由於只是想要理解web伺服器的原理,所以地址和連接埠是固定的,也不支援多線程,每次訪問的頁面也是一致的,後面自己會慢慢改進這些

複製代碼 代碼如下:

//*****************************************************************************
//@ProjectName      ZYhttpd
//@Description      my http server
//@Author           NicoleRobin
//@Date             2015/02/09
//*****************************************************************************
#include <cstdio>
#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: %d\r\n\r\n%s\
"
#define HTML "\
<html>\
    <head>\
        <title>ZYhttp</title>\
    <head>\
    <body>\
        <h1>Hello ZYhttp!</h1>\
    </body>\
</html>\
"
#pragma comment(lib, "WS2_32")
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);
        sprintf_s(buffer, HEADER, strlen(HTML), HTML);
        if (send(socketClient, buffer, strlen(buffer), 0) < 0)
        {
            printf("Send data failed!");
            break;
        }
        printf("Send data : \n%s", buffer);
        closesocket(socketClient);
    }
    closesocket(socketServer);
    WSACleanup();
    return 0;
}

示範圖

以上就是本文分享的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.