ASTERSIK C++ 5038

来源:互联网 发布:淘宝退货超过七天 编辑:程序博客网 时间:2024/06/05 17:21
编写Asterisk事件监控程序 

原理:通过login action连上Asterisk5038端口,监听此端口并把消息输出。 

下面是C++实现的代码: 

 1 /* 2     File      : asteriskEventCat.cpp 3     Author    : Mike 4     E-Mail    : Mike_Zhang@live.com 5 */ 6  7 #include <iostream> 8 #include <string> 9 #include <fstream>10 #include <boost/asio.hpp>11 #define BLOCK_SIZE 10*102412 13 using namespace std;14 using namespace boost::asio;15 16 string strLogin(string userName,string pswwd)17 {18     string msg="";19     msg = "Action: login\r\n";20     msg += "UserName: " + userName + "\r\n";21     msg += "Secret: " + pswwd + "\r\n";22     msg += "\r\n";    23     return msg;24 }25 26 int main()27 {28     io_service iosv;29     ip::tcp::socket s(iosv);30     string svrIp = "";31     cout<<"Input server ip : ";32     cin>>svrIp;33     ip::tcp::endpoint ep(ip::address_v4::from_string(svrIp.c_str()),5038);34 35     boost::system::error_code ec; 36     s.connect(ep,ec); 37     if(ec)38     {39         cout << boost::system::system_error(ec).what() << endl;         40         return -1; 41     } 42     else43     {44         cout<<"Connect success!"<<endl;45     }46 47     string msg="";48 49     string userName,password;50     cout<<"User     : ";51     cin>>userName;52     cout<<"Password : ";53     cin>>password;54 55     msg += strLogin(userName.c_str(),password.c_str());56     //    msg += strLogin("admin","admin");57     size_t len = s.write_some(buffer(msg.c_str()), ec);58     if(len <= 0)59     {60         cout<<"Send message fail!"<<endl;61         return -1;62     }63 64     std::ofstream fout("EventCat.txt");65     while(true)66     {67         char buf[BLOCK_SIZE] = {0};68         len=s.read_some(buffer(buf), ec); 69         if(len<=0)70             break;71         cout.write(buf, len); 72         fout<<buf;73         fout.flush();74     }    75 76     return 0;77 }
原创粉丝点击