Android Question How to connect to Jabber

mohammad_2015

New Member
I want to connect to the jabber.hot-chilli.net server using the default b4a socket library
Both username and password are "test"
I used these codes but they did not work
Also, no error occurs, only two or three xml codes are sent and received, and then there is no response.

Please advise me where the problem is with the codes

B4X:
Sub Connect_Click
    server.Initialize("server")
    server.Connect("jabber.hot-chilli.net",5222 ,5000)
End Sub


Sub server_Connected(Connected As Boolean)As Boolean
    If Connected = True Then
        x.Initialize(server.InputStream,server.OutputStream,"x")
        If x.IsInitialized = False Then Return
        Dim buffer() As Byte
        buffer =( "<stream:stream xmlns='jabber:client' to='jabber.hot-chilli.net' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='ar' >").GetBytes("UTF8")
        x.Write(buffer)
        Dim buffer1() As Byte
        Dim stf As String = Chr(0) & "test" & Chr(0) & "test"
        Dim stU As StringUtils
        Dim str As String = stU.EncodeBase64(stf.GetBytes("UTF8"))
        Dim str1 As String = "<auth id='sasl2' xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" & str & "</auth>"
        buffer1 = str1.GetBytes("UTF8")
        x.Write(buffer1)
    Else
    
        ToastMessageShow("please check internet connection.",True)
    End If
End Sub



Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        StartService("Test")
        server = NewSocket
        x.InitializePrefix(server.InputStream, False, server.OutputStream, "x")
    Else
        
        ToastMessageShow(LastException.Message, True)
    End If
End Sub


Sub x_NewData (Buffer() As Byte)
    Try
        Dim xml As String
        xml = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        xml = xml.Replace("""","'")
        
        If xml.Contains("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>")  Then
            Dim buffers() As Byte
            buffers = ("<stream:stream xmlns='jabber:client' to='jabber.hot-chilli.net' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='ar' >").GetBytes("UTF8")
            x.Write(buffers)
            Dim buffers1() As Byte
            Dim stt As String = "<iq type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>basic4android</resource></bind></iq>"
            buffers1= stt.GetBytes("UTF8")
            x.Write(buffers1)
            Dim buffers2() As Byte
            buffers2= "<iq id='jcl_2' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>".GetBytes("UTF8")
            x.Write(buffers2)
        
        Else If xml.Contains("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>") Then
            Dim buffers3() As Byte
            buffers3= "<iq type='get' id='aab9a'><query xmlns='jabber:iq:roster'/></iq>".GetBytes("UTF8")
            x.Write(buffers3)
            Dim buffers4() As Byte
            buffers4= "<presence><show></show></presence>".GetBytes("UTF8")
            x.Write(buffers4)
            
        Else If xml.Contains("<not-authorized/>") Then
            x.Close
            server.Close
            ToastMessageShow("Wrong Password Or Username",False)
        
        Else If xml.Contains("<presence from='[email protected]/basic4android' to='[email protected]/basic4android' xml:lang='ar'><show/><x xmlns='vcard-temp:x:update'><photo/></x></presence>") Then
            ToastMessageShow("Connect Successful",False)
                    
        End If
            
        
    Catch       
        Log(LastException)
    End Try

        
    

    
End Sub
 

mohammad_2015

New Member
This is the source of the project. There is also an xml file in the project folder that contains xml code to connect to. Thank you for your help
 

Attachments

  • ConnectToJabber.zip
    86.1 KB · Views: 69
Upvote 0

mohammad_2015

New Member
Regarding the previous problem, I contracted another source, which was done according to the link below. At this point you should get the xml code similar to example 7 and example 8 described in the link below. But I do not receive such a code. That is, the codes stop at this point and no more code is received
I also remember that I put the xml code that I connected to the server with other applications in the project folder.

Please please please help if anyone has a solution

https://xmpp.org/extensions/xep-0178.html
 

Attachments

  • ConnectToJabber2.zip
    11.1 KB · Views: 70
Upvote 0
Top