#include "ace/SOCK_Acceptor.h" #include "ace/SOCK_Stream.h" #include "ace/Mem_Map.h" #include "ace/Auto_Ptr.h" extern char* get_url_pathname(ACE_SOCK_Stream* peer){ return "test.txt"; } int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { ACE_INET_Addr server_addr; ACE_SOCK_Acceptor acceptor; ACE_SOCK_Stream peer; if (server_addr.set(80) == -1) return 1; if (acceptor.open(server_addr) == -1) return 1; while (1) { if (acceptor.accept(peer) != -1) { peer.disable(ACE_NONBLOCK); // 确保阻塞的send_n //ACE_Auto_Array_Ptr<char> pathname(get_url_pathname(&peer)); // ACE_Auto_Array_Ptr<X> 对象在析构时会释放其构造时的对应指针的内存 ACE_Mem_Map mapped_file(get_url_pathname(&peer)/*pathname.get()*/); if (peer.send_n(mapped_file.addr(), mapped_file.size()) == -1) return 1; peer.close(); } } return acceptor.close() == -1 ? 1 : 0; }
在工作目录添加"test.txt"文件,内容为:Hello World!
编译运行,在浏览器中输入http://127.0.0.1:80