Host-unknown exception in XMPP (Xamarin.Android)

2019-09-04 07:51发布

问题:

I continue with a connection to server. I have succesfully added XMPP library to my Xamarin.Android project, but when I try to make a simple connection:

        using (var cl = new XmppClient(hostname,username,password))
        {
            cl.Connect();

            Console.WriteLine("Contacts on " + cl.Jid.Node + "'s contact-list:");
            foreach (var item in cl.GetRoster())
                Console.WriteLine(" - " + item.Jid);
        }     

this case occure:

09-25 12:37:14.713 I/mono-stdout( 2821): _xmpp-client._tcp.jabbim.cz 2454 IN SRV 20 0 5222 lb2-hap.jabbim.cz
  |--- Name _xmpp-client._tcp.jabbim.cz
09-25 12:37:14.717 I/mono-stdout( 2821):   |--- Name _xmpp-client._tcp.jabbim.cz
  |--- Port: 5222
09-25 12:37:14.721 I/mono-stdout( 2821):   |--- Port: 5222
09-25 12:37:14.729 I/mono-stdout( 2821):   |--- Priority20
  |--- Priority20
09-25 12:37:14.741 I/mono-stdout( 2821):   |--- Type Srv
  |--- Type Srv
  |--- Target: lb2-hap.jabbim.cz
09-25 12:37:14.765 I/mono-stdout( 2821):   |--- Target: lb2-hap.jabbim.cz
_xmpp-client._tcp.jabbim.cz 2454 IN SRV 10 0 5222 lb1-hap.jabbim.cz
09-25 12:37:14.773 I/mono-stdout( 2821): _xmpp-client._tcp.jabbim.cz 2454 IN SRV 10 0 5222 lb1-hap.jabbim.cz
  |--- Name _xmpp-client._tcp.jabbim.cz
09-25 12:37:14.781 I/mono-stdout( 2821):   |--- Name _xmpp-client._tcp.jabbim.cz
  |--- Port: 5222
09-25 12:37:14.781 I/mono-stdout( 2821):   |--- Port: 5222
  |--- Priority10
09-25 12:37:14.785 I/mono-stdout( 2821):   |--- Priority10
09-25 12:37:14.789 I/mono-stdout( 2821):   |--- Type Srv
  |--- Type Srv
  |--- Target: lb1-hap.jabbim.cz
09-25 12:37:14.797 I/mono-stdout( 2821):   |--- Target: lb1-hap.jabbim.cz
09-25 12:37:15.457 D/Mono    ( 2821): Assembly Ref addref System.Xml[0xb7c5d400] -> System[0xb7c24320]: 7
09-25 12:37:16.033 D/Mono    ( 2821): DllImport attempting to load: '/system/lib/liblog.so'.
09-25 12:37:16.057 D/Mono    ( 2821): DllImport loaded library '/system/lib/liblog.so'.
09-25 12:37:16.057 D/Mono    ( 2821): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
09-25 12:37:16.057 D/Mono    ( 2821): Searching for '__android_log_print'.
09-25 12:37:16.065 D/Mono    ( 2821): Probing '__android_log_print'.
09-25 12:37:16.065 D/Mono    ( 2821): Found as '__android_log_print'.
09-25 12:37:16.085 I/MonoDroid( 2821): UNHANDLED EXCEPTION:
09-25 12:37:16.085 I/MonoDroid( 2821): Sharp.Xmpp.XmppDisconnectionException: Unrecoverable stream error: host-unknown
09-25 12:37:16.093 I/MonoDroid( 2821): at Sharp.Xmpp.Core.StreamParser.NextElement (string[]) <IL 0x0015d, 0x00713>
09-25 12:37:16.093 I/MonoDroid( 2821): at Sharp.Xmpp.Core.XmppCore.InitiateStream (string) <IL 0x000b5, 0x002eb>
09-25 12:37:16.093 I/MonoDroid( 2821): at Sharp.Xmpp.Core.XmppCore.SetupConnection (string) <IL 0x00007, 0x00053>
09-25 12:37:16.097 I/MonoDroid( 2821): at Sharp.Xmpp.Core.XmppCore.Connect (string) <IL 0x0004f, 0x001bf>
09-25 12:37:16.097 I/MonoDroid( 2821): at Sharp.Xmpp.Im.XmppIm.Connect (string) <IL 0x00078, 0x002b7>
09-25 12:37:16.101 I/MonoDroid( 2821): at Sharp.Xmpp.Client.XmppClient.Connect (string) <IL 0x00007, 0x00033>

It seems something is missing in a configuration. Thanks a lot for reply.

回答1:

According to this page the exception:

Sharp.Xmpp.XmppDisconnectionException: Unrecoverable stream error: host-unknown

refers to:

the value of the 'to' attribute provided by the initiating entity in the stream header does not correspond to a hostname that is hosted by the server.

So the hostname you are using to establish the connection does not match the hostname of the server. So are you sure that the hostname you are using is the right one and the server uses exactly this hostname?



回答2:

Probably this is an issue with DNS resolution in Android or server configuration. Sharp.Xmpp uses the https://arsofttoolsnet.codeplex.com/ package.

$ nslookup -querytype=SRV _xmpp-client._tcp.jabbim.cz Server: 100.72.64.140 Address: 100.72.64.140#53

Non-authoritative answer: _xmpp-client._tcp.jabbim.cz service = 10 0 5222 lb1-hap.jabbim.cz. _xmpp-client._tcp.jabbim.cz service = 20 0 5222 lb2-hap.jabbim.cz.

Authoritative answers can be found from: lb1-hap.jabbim.cz internet address = 88.86.102.58 lb2-hap.jabbim.cz internet address = 37.157.196.214`

According to your snippet, resolution is correct, and your are connecting to lb1-hap.jabbim.cz. Could it be that from console you are connecting to lb2, and lb2, lb1 have a different configuration?



回答3:

Use this:

client = new XmppClient(hostname, username, password);
client.Hostname = hostname;
client.Connect();

The reason seems to be a bug in Sharp.Xmpp. In the XmppClient() function the hostname gets altered to the DNS Lookup, in your case lb1-hap.jabbim.cz.

Later in the Connect() function, the first XML Stanza is send to the same hostname "to:lb1-hap.jabbim.cz". Then the server responses correctly with Unknown Host because it wants jabbim.cz