Making a simple Web server using C + + (cont.)

Source: Internet
Author: User
Tags socket error

Add Features:
1. Read the Web page from the file and return it to the client, instead of writing the page code to die in the code.
Limitations:
1, can not access the specified page according to the parameters in the URL address

2. Still a single thread

@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 ok\r\ncontent-type:text/html; Charset=utf-8\r\nserver:zyhttp_v1.0.1\r\ncontent-length:%ld\r\n\r\n "const string strpath =" index.html "; #pragma Comment (lib, "ws2_32")//Get File Sizelong getfilelength (string strpath);//thread functionint 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, ten)) {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) &L T                    0) {printf ("Send data failed!");                Break            } memset (buffer, 0, buffer_size); } if (send (socketclient, buffer, strlen (buffer), 0) < 0) {printf ("Send data faile                D! ");            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, 1024x768 * 1000-1)) {lsize + = strlen (szbuf);    memset (szbuf, 0, 1024*1000);    } fin.close ();    Lsize + = strlen (szbuf);    return lsize; */}



Demo diagram

The above is the article for the use of C + + to implement a simple Web server all the code, I hope you can enjoy.

Making a simple Web server using C + + (cont.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.