Using C + + to make a simple Web server _c language

Source: Internet
Author: User
Tags socket error

Only realized the Web function, the limitation is bigger, because only wants to understand the Web server principle, therefore the address and the port is fixed, also does not support the multithreading, each visit page is also consistent, after oneself will slowly improve these

Copy Code code as follows:

//*****************************************************************************
//@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 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 "\
<title>zyhttp</title>\
<body>\
</body>\
"
#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;
}

Demo diagram

The above is the entire content that this article shares, hoped everybody can like.

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.