#include <iostream> #include <map> #include <memory> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <unistd.h> void DeleteIntPtr(int *p) { std::cout << "call DeleteIntPtr" << std::endl; std::cout << *p << std::endl; close(*p); delete p; } int main() { int *clientfd = new int(socket(AF_INET, SOCK_STREAM, 0)); std::shared_ptr<int> sptr(clientfd, DeleteIntPtr); std::cout << sptr.get() << std::endl; std::cout << *sptr.get() << std::endl; std::cout << *clientfd << std::endl; if (-1 == *clientfd) { std::cout << "create socket error! errno: " << errno << std::endl; exit(EXIT_FAILURE); } struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8888); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //连接rpc服务节点 if (-1 == connect(*sptr.get(), (struct sockaddr*)&server_addr, sizeof(server_addr))) { std::cout << "connect error! errno: " << errno << std::endl; //close(clientfd); //exit(EXIT_FAILURE); return 0; } //发送rpc请求 /*if (-1 == send(clientfd, send_rpc_str.c_str(), send_rpc_str.size(), 0)) { std::cout << "send error! errno: " << errno << std::endl; close(clientfd); return; } close(clientfd);*/ return 0; } /// #if 0 typedef std::map<std::string, int> DEMOMAP; void fun(const DEMOMAP &m) { for (auto mm : m) { std::cout << mm.first << std::endl; } } int main() { std::auto_ptr<DEMOMAP> m(new DEMOMAP()); m.get()->insert({"demo", 1}); std::cout << m.get()->empty() << std::endl; fun(*m.get()); return 0; } #endif