赞
踩
From:https://zhuanlan.zhihu.com/p/357075167
java、c#、go、ruby 都有内置的net包,此外各语言都有各自流行的网络库、HTTP请求库、HTTP脚手架和 web 框架。
下面的 HTTP 请求代码均 copy 自 postman,postman 列举了众多语言写法,独独没有C++。
libcurl:https://curl.se/libcurl/
curl
作为命令行工具确实简单好用,比wget
更强大。当被作为库使用,并不也是那么简单,往往需要自己动手再封装一层。当然这也是c语言的通病,为了复用同一个接口,导致接口很难用。
- CURL *hnd = curl_easy_init();
-
- curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(hnd, CURLOPT_URL, "http://127.0.0.1:8080/echo");
-
- struct curl_slist *headers = NULL;
- headers = curl_slist_append(headers, "content-type: application/json");
- curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
-
- curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"key\":\"value\"}");
-
- CURLcode ret = curl_easy_perform(hnd);
上面的代码仅仅只是请求部分,想要获取响应内容,还得设置回调函数并在回调里解析数据:
- curl_easy_setopt(curl, CURLOPT_HEADER, 0);
- curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb);
- curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_userdata);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_cb);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_userdata);
curl_easy
尚且如此繁琐,curl_multi
更加让人望而却步。
- var client = new RestClient("http://127.0.0.1:8080/echo");
- var request = new RestRequest(Method.POST);
- request.AddHeader("content-type", "application/json");
- request.AddParameter("application/json", "{\"key\":\"value\"}", ParameterType.RequestBody);
- IRestResponse response = client.Execute(request);
- package main
-
- import (
- "fmt"
- "strings"
- "net/http"
- "io/ioutil"
- )
-
- func main() {
- url := "http://127.0.0.1:8080/echo"
-
- payload := strings.NewReader("{\"key\":\"value\"}")
-
- req, _ := http.NewRequest("POST", url, payload)
-
- req.Header.Add("content-type", "application/json")
-
- res, _ := http.DefaultClient.Do(req)
-
- defer res.Body.Close()
- body, _ := ioutil.ReadAll(res.Body)
-
- fmt.Println(res)
- fmt.Println(string(body))
- }
- OkHttpClient client = new OkHttpClient();
-
- MediaType mediaType = MediaType.parse("application/json");
- RequestBody body = RequestBody.create(mediaType, "{\"key\":\"value\"}");
- Request request = new Request.Builder()
- .url("http://127.0.0.1:8080/echo")
- .post(body)
- .addHeader("content-type", "application/json")
- .build();
-
- Response response = client.newCall(request).execute();
- <?php
-
- $request = new HttpRequest();
- $request->setUrl('http://127.0.0.1:8080/echo');
- $request->setMethod(HTTP_METH_POST);
-
- $request->setHeaders(array(
- 'content-type' => 'application/json'
- ));
-
- $request->setBody('{"key":"value"}');
-
- try {
- $response = $request->send();
-
- echo $response->getBody();
- } catch (HttpException $ex) {
- echo $ex;
- }
- import requests
-
- url = "http://127.0.0.1:8080/echo"
-
- payload = "{\"key\":\"value\"}"
- headers = {
- 'content-type': "application/json"
- }
-
- response = requests.request("POST", url, data=payload, headers=headers)
-
- print(response.text)
- require 'uri'
- require 'net/http'
-
- url = URI("http://127.0.0.1:8080/echo")
-
- http = Net::HTTP.new(url.host, url.port)
-
- request = Net::HTTP::Post.new(url)
- request["content-type"] = 'application/json'
- request.body = "{\"key\":\"value\"}"
-
- response = http.request(request)
- puts response.read_body
- var http = require("http");
-
- var options = {
- "method": "POST",
- "hostname": "127.0.0.1",
- "port": "8080",
- "path": "/echo",
- "headers": {
- "content-type": "application/json"
- }
- };
-
- var req = http.request(options, function (res) {
- var chunks = [];
-
- res.on("data", function (chunk) {
- chunks.push(chunk);
- });
-
- res.on("end", function () {
- var body = Buffer.concat(chunks);
- console.log(body.toString());
- });
- });
-
- req.write(JSON.stringify({ key: 'value' }));
- req.end();
string
、map
、反射、ORM
,写复杂应用层协议简直就是找虐,更别说写数据库应用了。如不分场合,强行使用c/c++
写web应用,开发效率注定感人,可能人家已经开始展示各种炫酷图表了,你还在那解析字符串。以TCP Echo Server为例,展示各个网络库的写法。
代码:https://github.com/ithewei/libhv/tree/master/echo-servers
libevent:http://libevent.org/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "event2/event.h"
- #include "event2/listener.h"
- #include "event2/bufferevent.h"
- #include "event2/buffer.h"
-
- //#define RECV_BUFSIZE 8192
-
- void error_cb(struct bufferevent* bev, short event, void* userdata) {
- bufferevent_free(bev);
- }
-
- void read_cb(struct bufferevent* bev, void* userdata) {
- //static char recvbuf[RECV_BUFSIZE];
- //int nread = bufferevent_read(bev, &recvbuf, RECV_BUFSIZE);
- //bufferevent_write(bev, recvbuf, nread);
- struct evbuffer* buf = evbuffer_new();
- int ret = bufferevent_read_buffer(bev, buf);
- if (ret == 0) {
- bufferevent_write_buffer(bev, buf);
- }
- evbuffer_free(buf);
- }
-
- void on_accept(struct evconnlistener* listener, evutil_socket_t connfd, struct sockaddr* peeraddr, int addrlen, void* userdata) {
- struct event_base* loop = evconnlistener_get_base(listener);
- struct bufferevent* bev = bufferevent_socket_new(loop, connfd, BEV_OPT_CLOSE_ON_FREE);
- bufferevent_setcb(bev, read_cb, NULL, error_cb, NULL);
- bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
- }
-
- int main(int argc, char** argv) {
- if (argc < 2) {
- printf("Usage: cmd port\n");
- return -10;
- }
- int port = atoi(argv[1]);
-
- struct event_base* loop = event_base_new();
-
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- struct evconnlistener* listener = evconnlistener_new_bind(
- loop, on_accept, NULL,
- LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE,
- -1, (struct sockaddr*)&addr, sizeof(addr));
- if (listener == NULL) {
- return -20;
- }
-
- event_base_dispatch(loop);
-
- evconnlistener_free(listener);
- event_base_free(loop);
- return 0;
- }
memcached
、libwebsockets
、360的evpp
),稳定性有保障;libev:http://software.schmorp.de/pkg/libev.html
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
-
- #include "ev.h"
-
- #define RECV_BUFSIZE 8192
- static char recvbuf[RECV_BUFSIZE];
-
- void do_recv(struct ev_loop *loop, struct ev_io *io, int revents) {
- int nread, nsend;
- nread = recv(io->fd, recvbuf, RECV_BUFSIZE, 0);
- if (nread <= 0) {
- goto error;
- }
- nsend = send(io->fd, recvbuf, nread, 0);
- if (nsend != nread) {
- goto error;
- }
- return;
-
- error:
- ev_io_stop(loop, io);
- close(io->fd);
- free(io);
- }
-
- void do_accept(struct ev_loop *loop, struct ev_io *listenio, int revents) {
- struct sockaddr_in peeraddr;
- socklen_t addrlen = sizeof(peeraddr);
- int connfd = accept(listenio->fd, (struct sockaddr*)&peeraddr, &addrlen);
- if (connfd <= 0) {
- return;
- }
-
- struct ev_io* io = (struct ev_io*)malloc(sizeof(struct ev_io));
- ev_io_init(io, do_recv, connfd, EV_READ);
- ev_io_start(loop, io);
- }
-
- int main(int argc, char** argv) {
- if (argc < 2) {
- printf("Usage: cmd port\n");
- return -10;
- }
- int port = atoi(argv[1]);
-
- struct sockaddr_in addr;
- int addrlen = sizeof(addr);
- memset(&addr, 0, addrlen);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- int listenfd = socket(AF_INET, SOCK_STREAM, 0);
- if (listenfd < 0) {
- return -20;
- }
- if (bind(listenfd, (struct sockaddr*)&addr, addrlen) < 0) {
- return -30;
- }
- if (listen(listenfd, SOMAXCONN) < 0) {
- return -40;
- }
-
- struct ev_loop* loop = ev_loop_new(0);
-
- struct ev_io listenio;
- ev_io_init(&listenio, do_accept, listenfd, EV_READ);
- ev_io_start(loop, &listenio);
-
- ev_run(loop, 0);
- ev_loop_destroy(loop);
- return 0;
- }
优点:libev可以说是libevent的精简版,库源码极为短小精悍,不到八千行;
缺点:
socket->bind->listen
这个流程;recv/send
;windows
平台实现不佳;- #define _GNU_SOURCE 1
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "uv.h"
-
- typedef struct {
- uv_write_t req;
- uv_buf_t buf;
- } uv_write_req_t;
-
- void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
- buf->base = (char*)malloc(suggested_size);
- buf->len = suggested_size;
- }
-
- void close_cb(uv_handle_t* handle) {
- free(handle);
- }
-
- void write_cb(uv_write_t* req, int status) {
- uv_write_req_t* wr = (uv_write_req_t*)req;
- free(wr->buf.base);
- free(wr);
- }
-
- void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
- if (nread <= 0) {
- uv_close((uv_handle_t*)stream, close_cb);
- return;
- }
- uv_write_req_t* wr = (uv_write_req_t*)malloc(sizeof(uv_write_req_t));
- wr->buf.base = buf->base;
- wr->buf.len = nread;
- uv_write(&wr->req, stream, &wr->buf, 1, write_cb);
- }
-
- void do_accept(uv_stream_t* server, int status) {
- uv_tcp_t* tcp_stream = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
- uv_tcp_init(server->loop, tcp_stream);
- uv_accept(server, (uv_stream_t*)tcp_stream);
- uv_read_start((uv_stream_t*)tcp_stream, alloc_cb, read_cb);
- }
-
- int main(int argc, char** argv) {
- if (argc < 2) {
- printf("Usage: cmd port\n");
- return -10;
- }
- int port = atoi(argv[1]);
-
- uv_loop_t loop;
- uv_loop_init(&loop);
-
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- //addr.sin_family = AF_INET;
- //addr.sin_port = htons(port);
- uv_ip4_addr("0.0.0.0", port, &addr);
-
- uv_tcp_t tcp_server;
- uv_tcp_init(&loop, &tcp_server);
- int ret = uv_tcp_bind(&tcp_server, (struct sockaddr*)&addr, 0);
- if (ret) {
- return -20;
- }
- ret = uv_listen((uv_stream_t*)&tcp_server, SOMAXCONN, do_accept);
- if (ret) {
- return -30;
- }
-
- uv_run(&loop, UV_RUN_DEFAULT);
- return 0;
- }
优点:
nodejs
的底层库,有nodejs
这个大佬背书,稳定性、性能都毋庸置疑;uv_
前缀开头;缺点:
uv_ip4_addr->uv_tcp_bind->uv_listen
这一套流程;buffer
,uv_read_start/uv_write
需要自己申请和释放内存,往往需要结合自己的业务实现一个内存池,不然每次读写都直接malloc/free
,在某些操作系统上可能有性能损耗和内存碎片问题;POCO 用户手册、示例:https://docs.pocoproject.org/current/
POCO C++库入门指南:https://blog.csdn.net/danlan_shiguang/article/details/124419814
比 libevent、libuv 更易用的国产网络库:github:https://github.com/ithewei/libhv
c 版本:
- #include "hv/hloop.h"
-
- void on_close(hio_t* io) {
- }
-
- void on_recv(hio_t* io, void* buf, int readbytes) {
- hio_write(io, buf, readbytes);
- }
-
- void on_accept(hio_t* io) {
- hio_setcb_close(io, on_close);
- hio_setcb_read(io, on_recv);
- hio_read(io);
- }
-
- int main(int argc, char** argv) {
- if (argc < 2) {
- printf("Usage: cmd port\n");
- return -10;
- }
- int port = atoi(argv[1]);
-
- hloop_t* loop = hloop_new(0);
- hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
- if (listenio == NULL) {
- return -20;
- }
- hloop_run(loop);
- hloop_free(&loop);
- return 0;
- }
c++版本:
- #include "hv/TcpServer.h"
-
- using namespace hv;
-
- int main(int argc, char* argv[]) {
- if (argc < 2) {
- printf("Usage: %s port\n", argv[0]);
- return -10;
- }
- int port = atoi(argv[1]);
-
- TcpServer srv;
- int listenfd = srv.createsocket(port);
- if (listenfd < 0) {
- return -20;
- }
- printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
- srv.onConnection = [](const SocketChannelPtr& channel) {
- std::string peeraddr = channel->peeraddr();
- if (channel->isConnected()) {
- printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
- } else {
- printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
- }
- };
- srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
- // echo
- printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
- channel->write(buf);
- };
- srv.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
- printf("> %.*s\n", (int)buf->size(), (char*)buf->data());
- };
- srv.setThreadNum(4);
- srv.start();
-
- while (1) sleep(1);
- return 0;
- }
优点:
libhv
本身是参考了libevent、libev、libuv
的实现思路,它们的核心都是事件循环(即在一个事件循环中处理IO、定时器等事件),没有历史包袱,去其糟粕,取其精华;muduo
和evpp
;SSL/TLS
;HTTP
脚手架;WebSocket
协议;MQTT
协议redis
、kafka
、mysql
;739352073
)可供寻求技术支持;asio有C++11版本,不依赖boost。Asio:http://think-async.com/
- #include <cstdlib>
- #include <iostream>
- #include <boost/bind.hpp>
- #include <boost/asio.hpp>
-
- using boost::asio::ip::tcp;
-
- class session {
- public:
- session(boost::asio::io_service& io_service) :
- socket_(io_service) {
- }
-
- tcp::socket& socket() {
- return socket_;
- }
-
- void start() {
- socket_.async_read_some(boost::asio::buffer(data_, max_length),
- boost::bind(&session::handle_read, this,
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
- }
-
- void handle_read(const boost::system::error_code& error,
- size_t bytes_transferred) {
- if (!error) {
- boost::asio::async_write(socket_, boost::asio::buffer(data_,
- bytes_transferred), boost::bind(&session::handle_write,
- this, boost::asio::placeholders::error));
- } else {
- delete this;
- }
- }
-
- void handle_write(const boost::system::error_code& error) {
- if (!error) {
- socket_.async_read_some(boost::asio::buffer(data_, max_length),
- boost::bind(&session::handle_read, this,
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred));
- } else {
- delete this;
- }
- }
-
- private:
- tcp::socket socket_;
- enum {
- max_length = 1024
- };
- char data_[max_length];
- };
-
- class server {
- public:
- server(boost::asio::io_service& io_service, short port) :
- io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(),
- port)) {
- session* new_session = new session(io_service_);
- acceptor_.async_accept(new_session->socket(), boost::bind(
- &server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
- }
-
- void handle_accept(session* new_session,
- const boost::system::error_code& error) {
- if (!error) {
- new_session->start();
- new_session = new session(io_service_);
- acceptor_.async_accept(new_session->socket(), boost::bind(
- &server::handle_accept, this, new_session,
- boost::asio::placeholders::error));
- } else {
- delete new_session;
- }
- }
-
- private:
- boost::asio::io_service& io_service_;
- tcp::acceptor acceptor_;
- };
-
- int main(int argc, char** argv) {
- if (argc < 2) {
- printf("Usage: cmd port\n");
- return -10;
- }
- int port = atoi(argv[1]);
-
- boost::asio::io_service io_service;
- server s(io_service, port);
- io_service.run();
-
- return 0;
- }
优点:
boost
不能整体入标准库,asio
自然也不得成神;beast
支持HTTP
和WebSocket
;websocketpp
支持WebSocket
协议;ACE:https://github.com/cflowe/ACE
ACE也是很经典的网络库,出自《C++网络编程》作者之手,设计精妙程度堪称一流,支持协议范围也很广,但是使用复杂度和学习复杂度较高,一直有“学我者生,用我者死”的评价。
QUIC 全称:Quick UDP Internet Connections,是一种基于 UDP 的传输层协议。由 Google 自研,2012 年部署上线,2013 年提交 IETF,2021 年 5 月,IETF 推出标准版 RFC9000。
QUIC协议是传输层 "更快、更稳、更高效" 的网络通信协议。
QUIC 协议是在 UDP 的基础之上,集成了 TCP 的可靠传输特性,集成了 TLS1.3 协议,保证了用户数据传输的安全。
:https://zhuanlan.zhihu.com/p/405387352
:https://zhuanlan.zhihu.com/p/655070575
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。