From e3985b7398044562dafc10a9a97925fab8bfa667 Mon Sep 17 00:00:00 2001 From: adminPyf <3043130461@qq.com> Date: Mon, 5 Jul 2021 22:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'test=5Fhttp=5Fserver'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_http_server/test_http_server.sln | 31 +++++++++++++ test_http_server/test_server.cpp | 67 +++++++++++++++++++++++++++ test_http_server/test_server.sln | 31 +++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 test_http_server/test_http_server.sln create mode 100644 test_http_server/test_server.cpp create mode 100644 test_http_server/test_server.sln diff --git a/test_http_server/test_http_server.sln b/test_http_server/test_http_server.sln new file mode 100644 index 0000000..5111269 --- /dev/null +++ b/test_http_server/test_http_server.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1525 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "first_libevent", "first_libevent.vcxproj", "{129BF326-BFEE-4F1C-920C-0C302BB84895}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.ActiveCfg = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.Build.0 = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.ActiveCfg = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.Build.0 = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.ActiveCfg = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.Build.0 = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.ActiveCfg = Release|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {191D7F93-7554-493B-92E1-BB302C5A8383} + EndGlobalSection +EndGlobal diff --git a/test_http_server/test_server.cpp b/test_http_server/test_server.cpp new file mode 100644 index 0000000..9a622f8 --- /dev/null +++ b/test_http_server/test_server.cpp @@ -0,0 +1,67 @@ + +#include +#include +#include +#include +#ifndef _WIN32 +#include +#endif // !_WIN32 + + +#define SPORT 5001 + +void listen_cb(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *arg) { + std::cout << "listen to be" << std::endl; +} + +int main() +{ +#ifdef _WIN32 + //初始化socket库 + WSADATA wsa; + WSAStartup(MAKEWORD(2, 2), &wsa); +#else + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉! + return 1; + } +#endif + + //创建libevent上下文 + event_base* base = event_base_new(); + if (base) { + std::cout << "test server" << "\n"; + } + + + sockaddr_in sin; + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = htons(SPORT); + + //socket,bind listen 绑定事件 + evconnlistener * ev = evconnlistener_new_bind(base, //libevent上下文 + listen_cb, //接收到连接的回调函数 + base, //回调函数获取的参数(根据业务来) + LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //地址重用,evconnlistener关闭同时关闭socket + 10, //连接队列大小,对应listen函数参数 + (sockaddr*)&sin,//绑定的地址和端口 + sizeof(sin) + ); + + if (base) + event_base_dispatch(base); //事件分发处理 + if (ev) + evconnlistener_free(ev); //清理 + if (base) + event_base_free(base); + +#if _WIN32 + WSACleanup(); +#endif // _WIN32 + + + + return 0; +} + + diff --git a/test_http_server/test_server.sln b/test_http_server/test_server.sln new file mode 100644 index 0000000..5111269 --- /dev/null +++ b/test_http_server/test_server.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1525 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "first_libevent", "first_libevent.vcxproj", "{129BF326-BFEE-4F1C-920C-0C302BB84895}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.ActiveCfg = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.Build.0 = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.ActiveCfg = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.Build.0 = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.ActiveCfg = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.Build.0 = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.ActiveCfg = Release|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {191D7F93-7554-493B-92E1-BB302C5A8383} + EndGlobalSection +EndGlobal