ASTERSIK C++ 5038

编写 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*1024
12 
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     else
43     {
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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值