2012-07-23 105 views
0

我试图用C++ gloox库,首先我想从这里
https://github.com/knatten/blog.knatten.org/blob/master/gloox/bot.cpp为什么gloox不起作用?

然后运行的例子,我尝试另一个给定的例子,但我甚至看不到我的客户是网上...如何我能否至少发送出席信息并成为在线?我的电脑有问题吗?我正在使用Ubuntu。

#include <gloox/client.h> 
#include <gloox/messagehandler.h> 
#include <gloox/message.h> 
#include <gloox/presencehandler.h> 
#include <gloox/connectionlistener.h> 
#include <iostream> 
#include <string> 
using namespace std; 
using namespace gloox; 
//my one 
const string LOGIN = "gmail.com"; 
ostream& operator<<(ostream& os, Message::MessageType type) { 
    switch (type) { 
     case Message::Chat: 
      os << "Chat"; 
      break; 
     case Message::Error: 
      os << "Error"; 
      break; 
     case Message::Groupchat: 
      os << "Groupchat"; 
      break; 
     case Message::Headline: 
      os << "Headline"; 
      break; 
     case Message::Normal: 
      os << "Normal"; 
      break; 
     case Message::Invalid: 
      os << "Invalid"; 
      break; 
     default: 
      os << "unknown type"; 
      break; 
    } 
return os; 
} 

ostream& operator<<(ostream& os, const Message& stanza) { 
    os << "type:'" << stanza.subtype() << "' from:'" << stanza.from().full() << "' body:'" << stanza.body() << "'"; 
    return os; 
} 

class Bot : public MessageHandler, 
      /*public PresenceHandler,*/ 
      public ConnectionListener 
      /*,public RosterListener*/{ 
public: 
    Bot(){ 
     JID jid("[email protected]/home"); 
     client = new Client(jid, "mmaaria123"); 
     client->registerConnectionListener(this); 
     client->registerMessageHandler(this); 
     client->connect(true); 
    } 

    virtual void onConnect() { 
     cout << "It is connected good:)" << endl; 
     sleep(5); 
     client->disconnect(); 
    } 

    virtual void onDisconnect(ConnectionError e) { 
     cout << "ConnListener::onDisconnect() " << endl; 
    } 

    virtual bool onTLSConnect(const CertInfo& Info) { 

     time_t tm = Info.date_from; 
     time_t tm_to = Info.date_to; 
     cout << "\t Certificate information to " << LOGIN << endl; 
     cout << "\t========================================" << endl; 
     // number code << "\tServer status: " << Info.status; 
     if (Info.status !=0) 
      { 
      cout << "\t !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl; 
      cout << "\t !!! The certificate has a problem !!!" << endl; 
      cout << "\t !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl; 
      // parsing binary Status Cert 
      if ((CertInvalid) & Info.status) 
       { 
        cout << "\t- The certificate is not trusted " << endl; 
       } 
      if ((CertSignerUnknown) & Info.status) 
       { 
        cout << "\t- The certificate hasn't got a kown issuer" << endl; 
       } 
      if ((CertRevoked) & Info.status) 
       { 
        cout << "\t- The certificate has been revoked " << endl; 
       } 
      if ((CertExpired) & Info.status) 
       { 
        cout << "\t- The certificate has expired " << endl; 
       } 
      if ((CertNotActive) & Info.status) 
       { 
        cout << "\t- The certificate is not yet active " << endl; 
       } 
      if ((CertWrongPeer) & Info.status) 
       { 
        cout << "\t- The certificate has not issued for the peer we're connected to " << endl; 
       } 
      if ((CertSignerNotCa) & Info.status) 
       { 
        cout << "\t- The signer is not CA " << endl; 
       } 
      cout << endl; 
      } 
      // end parsing 
     else 
      // if Info.status=0 sertificate is valide 
      cout << " *** Cert is OK!" << endl; 

     cout << "\tChain: " << Info.chain << endl; 
     cout << "\tIssuer: " << Info.issuer << endl; 
     cout << "\tServer: " << Info.server << endl; 
     cout << "\tCert date from: " << ctime(&tm); 
     cout << "\tCert date to: " << ctime(&tm_to); 
     cout << "\tProtocol: " << Info.protocol << endl; 
     cout << "\tCipher: " << Info.cipher << endl; 
     cout << "\tMAC: " << Info.mac << endl; 
     cout << "\tCompression: " << Info.compression << endl; 
     cout << "\t========================================" << endl; 
    return true; 

    } 

    virtual void handleMessage(const Message& stanza, 
           MessageSession* session = 0){ 
    Message msg(Message::Chat, stanza.from(), "hello world"); 
    client->send(msg); 
    } 

private: 
    Client* client; 
}; 

int main(int argc, char* argv[]){ 
    cout << "1" << endl; 
    Bot b; 
} 

在控制台它打印 该证书不被信任 证书并没有一个kown发行 但onTLSConnect总是返回true,那么它至少应该是网上...铝laest它应打印“它连接好:)“

+0

你的代码是什么样的? – RedX 2012-07-23 06:08:04

+2

当我在Ubuntu 12.04系统上运行https://github.com/knatten/blog.knatten.org/blob/master/gloox/bot.cpp中的编译代码时,我看到了相同的结果,我在ubuntu 10.04系统上运行相同的二进制文件,它只是起作用。 @Maria你最终解决了这个问题吗? – Pelle 2013-06-27 14:30:17

+0

你在使用12.04 32位吗?自12.04以来,我有类似的问题,但在64位一切正常。 – cronos 2013-07-14 09:59:23

回答

0

检查你的代码,onTLSConnect只有一个return声明,那就是return true;在最后。它从不会在任何地方返回false

+0

是的,这是真的。但它不能成为原因,为什么这段代码不起作用......原因是Ubuntu或者我的互联网连接......但是我无法找到确切的一个...... – Maria 2012-07-23 08:53:02

+0

@Maria所以你的问题是不是说'onTLSConnect'即使在失败时也总是返回true? – 2012-07-23 08:55:16

+0

他的问题是,我没有看到,我从我的电脑在线,但它从另一个从Debian运行,并正常工作 – Maria 2012-07-23 09:15:29