无法将 Gloox Xmpp 客户端连接到 Openfire 服务器

Posted

技术标签:

【中文标题】无法将 Gloox Xmpp 客户端连接到 Openfire 服务器【英文标题】:Unable to connect Gloox Xmpp client to Openfire server 【发布时间】:2012-08-03 06:09:49 【问题描述】:

我的任务是创建一个 C++ XMPP 客户端,该客户端必须与服务器上的其他客户端进行通信。我使用 Gloox 库编写了一个简单的 C++ XMPP 客户端,用于在服务器上注册一个新用户。 我使用的服务器是Openfire,IDE是VC++ 2008。

// register_example.cpp

class RegTest : public RegistrationHandler, ConnectionListener, LogHandler



public:
RegTest() 
virtual ~RegTest() 

void start()

  j = new Client( "localhost" );
  j->disableRoster();
  j->registerConnectionListener( this );
  m_reg = new Registration( j );
  m_reg->registerRegistrationHandler( this );
  j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
  j->connect();
  delete( m_reg );
  delete( j );


virtual void onConnect()

 m_reg->fetchRegistrationFields();


virtual void onDisconnect( ConnectionError e ) 
printf( "register_test: disconnected: %d\n", e );    

virtual bool onTLSConnect( const CertInfo& info )

  printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n",
  info.status, info.issuer.c_str(), info.server.c_str(),
  info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
  info.compression.c_str() );
  return true;


virtual void handleRegistrationFields( const JID& /*from*/, int fields, std::string  instructions )

  printf( "fields: %d\ninstructions: %s\n", fields, instructions.c_str() );
  RegistrationFields vals;
  vals.username = "testuser";
  vals.password = "testpwd";
  m_reg->createAccount( fields, vals );


virtual void handleRegistrationResult( const JID& /*from*/, RegistrationResult result )

  printf( "result: %d\n", result );
  j->disconnect();


virtual void handleAlreadyRegistered( const JID& /*from*/ )

  printf( "the account already exists.\n" );


virtual void handleDataForm( const JID& /*from*/, const DataForm& /*form*/ )

  printf( "datForm received\n" );


virtual void handleOOB( const JID& /*from*/, const OOB& oob )

  printf( "OOB registration requested. %s: %s\n", oob.desc().c_str(), oob.url().c_str() );


virtual void handleLog( LogLevel level, LogArea area, const std::string& message )

  printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );

 private:
 Registration *m_reg;
 Client *j;
;

int _tmain(int argc, _TCHAR* argv[])

  RegTest *r = new RegTest();
  r->start();
  return 0;

代码构建成功,但是当我运行代码时,我收到以下错误代码 400--- Bad Request。请告诉我出了什么问题? 日志 -

    **log: level: 0, area: 8, This is gloox 1.0, connecting to localhost:-1...

log: level: 1, area: 32, DnsQuery_UTF8() failed: 9003
log: level: 1, area: 32, Notice: no SRV record found for localhost, using defaul
t port.
log: level: 0, area: 32, Connecting to localhost (127.0.0.1:5222)
log: level: 0, area: 32, Connected to localhost (127.0.0.1:5222)
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
 xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'  xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xml
ns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanis
m>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanis
m></mechanisms><compression xmlns='http://jabber.org/features/compress'><method>
zlib</method></compression><auth xmlns='http://jabber.org/features/iq-auth'/><re
gister xmlns='http://jabber.org/features/iq-register'/></stream:features>
log: level: 0, area: 262144, <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
log: level: 0, area: 131072, <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
log: level: 0, area: 4, starting TLS handshake...
status: -842150451
issuer: CN=ds-001d92dde38d
peer: CN=ds-001d92dde38d
protocol: TLSv1
mac: SHA
cipher: AES_128
compression:
log: level: 0, area: 8, connection encryption active
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
 xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'  xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DI
GEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism>
<mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns='http://jabber.or
g/features/compress'><method>zlib</method></compression><auth xmlns='http://jabb
er.org/features/iq-auth'/><register xmlns='http://jabber.org/features/iq-registe
r'/></stream:features>
log: level: 0, area: 262144, <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mech
anism='ANONYMOUS'/>
log: level: 0, area: 131072, <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
log: level: 0, area: 4, SASL authentication successful
log: level: 0, area: 262144, <?xml version='1.0' ?><stream:stream to='localhost'
 xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'  xml:lang
='en' version='1.0'>
log: level: 0, area: 131072, <stream:stream xmlns:stream='http://etherx.jabber.o
rg/streams' xmlns='jabber:client' from='ds-001d92dde38d' id='ab49a54c' xml:lang=
'en' version='1.0'/>
log: level: 0, area: 131072, <stream:features xmlns:stream='http://etherx.jabber
.org/streams'><compression xmlns='http://jabber.org/features/compress'><method>z
lib</method></compression><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><sessi
on xmlns='urn:ietf:params:xml:ns:xmpp-session'/></stream:features>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00000029' type='set' from='loc
alhost' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><re
source/></bind></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:00000029' to='ds
-001d92dde38d/ab49a54c'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>ab49
a54c@ds-001d92dde38d/ab49a54c</jid></bind></iq>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00004823' type='set' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><session xmlns='urn:ietf:p
arams:xml:ns:xmpp-session'/></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:00004823' to='ab
49a54c@ds-001d92dde38d/ab49a54c'/>
log: level: 0, area: 262144, <presence from='ab49a54c@ds-001d92dde38d/ab49a54c'
xmlns='jabber:client'><priority>0</priority><c xmlns='http://jabber.org/protocol
/caps' hash='sha-1' node='http://camaya.net/gloox' ver='1LpOuB49QCQE6BcZoAF5tBoA
Ldg='/></presence>
log: level: 0, area: 262144, <iq id='uid:501a3ee9:000018be' type='get' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><query xmlns='jabber:iq:re
gister'/></iq>
log: level: 0, area: 131072, <iq type='result' id='uid:501a3ee9:000018be' to='ab
49a54c@ds-001d92dde38d/ab49a54c'><query xmlns='jabber:iq:register'><username/><p
assword/><email/><name/><x xmlns='jabber:x:data' type='form'><title>XMPP Client
Registration</title><instructions>Please provide the following information</inst
ructions><field var='FORM_TYPE' type='hidden'><value>jabber:iq:register</value><
/field><field var='username' type='text-single' label='Username'><required/></fi
eld><field var='name' type='text-single' label='Full name'/><field var='email' t
ype='text-single' label='Email'/><field var='password' type='text-private' label
='Password'><required/></field></x></query></iq>
datForm received
fields: 77
instructions:
log: level: 0, area: 262144, <iq id='uid:501a3ee9:00006784' type='set' from='ab4
9a54c@ds-001d92dde38d/ab49a54c' xmlns='jabber:client'><query xmlns='jabber:iq:re
gister'><username>testuser</username><password>testpwd</password><name/><email/>
</query></iq>
log: level: 0, area: 131072, <iq type='error' id='uid:501a3ee9:00006784' to='ab4
9a54c@ds-001d92dde38d/ab49a54c'><query xmlns='jabber:iq:register'><username>test
user</username><password>testpwd</password><name/><email/></query><error code='4
00' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></er
ror></iq>
result: 4
log: level: 0, area: 262144, </stream:stream>
register_test: disconnected: 17**

【问题讨论】:

【参考方案1】:

服务器需要的信息多于“jabber:iq:register”命名空间架构所支持的信息,因此服务器向您发送了一个数据表单。您必须返回优先于“jabber:iq:register”字段的填充数据表单。它在 XEP-0077(带内注册)的 Extensibility 部分中定义。

【讨论】:

以上是关于无法将 Gloox Xmpp 客户端连接到 Openfire 服务器的主要内容,如果未能解决你的问题,请参考以下文章

无法将 agsXmpp 客户端连接到 eJabberd 服务器

通过 XMPP 连接到 yahoo messenger

无法将我的应用服务器连接到 FCM XMPP 服务器

gloox 是不是通过 BOSH 支持 BOSH 和 XMPP?

Android/XMPP:连接类型更改后无法重新连接到服务器

Jabber-Net 发布版本无法连接到 XMPP 服务器