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

时间:2021-05-19

仅仅实现了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;
}

演示图

以上就是本文分享的全部内容了,希望大家能够喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章