B4J Question Socket client that not connect on server

amorosik

Expert
Licensed User
I'm using the classic minimal Erel example of B4J for a socket server

B4X:
Sub Process_Globals
    Private server As ServerSocket
    Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    server.Initialize(61042, "server")
    server.Listen
    StartMessageLoop
End Sub

Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("New connection")
    If Successful Then
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
    End If
    server.Listen
End Sub

Sub astream_NewData (Buffer() As Byte)
    Log("New Data")
    Log(BytesToString(Buffer, 0, Buffer.Length, "utf8"))
    astream.Write("received!".GetBytes("utf8"))
End Sub

And for client, i try to use this minimal code

B4X:
Sub Process_Globals
    Private astream As AsyncStreams
    Private socket As Socket
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    socket.Initialize("socket1")
    socket.connect("192.168.1.100",61042,5000)
 
    wait for socket1_connected(successful As Boolean)
    If successful Then
        Dim s As String="Comando1=PEPPE;Comando2=GIGI;Comando3=TONI"
        Dim stringa_in_byte() As Byte=s.GetBytes("UTF8")
        astream.Initializeprefix(socket.InputStream,True, socket.OutputStream,"EvAstream")
        astream.Write(stringa_in_byte)
    End If
   
    socket.Close
    astream.Close
End Sub

Private Sub EvAstream_NewData (Buffer() As Byte)
    Log("EvAstream NewData")
End Sub

Private Sub EvAstream_Error
    Log("EvAstream Error")
End Sub

Private Sub EvAstream_Terminated
    Log("EvAstream Terminated")
End Sub

Server code run correctly and remain in listen mode
When i start client code, it break on wait for socket1_connected(successful As Boolean) row
Error log is:

B4X:
Program started.
Hello world!!!
Program terminated (StartMessageLoop was not called).

What's wrong on code above?

(B4J 9.10, JNetwork 1.20, JRandomAccesFile 2.34, jdk-11.0.1)
 
Last edited:
Top