Using C + + to make a simple Web server (cont.) _c Language

Source: Internet
Author: User
Tags socket socket error strlen

Add Features:
1. Read the Web page from the file and return it to the client instead of writing the page code dead in the code.
Limitations:
1, also can not be based on the URL address parameters, access to the specified page
2, still a single thread

Copy Code code as follows:

//*****************************************************************************
//@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\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;
*/
}

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.

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.