B4J Question can i connect server and client on the same machine using Socket

I am Writing code to connect server and client.I have only one machine so i want to connect server and client on the same machine to test the code .How to do that ?

Server.b4j

B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region
Sub Process_Globals
    Private server As ServerSocket
    Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
    server.Initialize(6500,"server")
    server.Listen
    If server.IsInitialized = True Then
        Log("Server Started.......")
    End If
    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(BytesToString(Buffer, 0, Buffer.Length, "utf8"))
    astream.Write("received!".GetBytes("utf8"))
End Sub

Client.b4j
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region

Sub Process_Globals
    Private Socket As Socket
    Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
    Socket.Initialize("Socket1")
    Socket.Connect("127.0.0.1" ,6500,20000)
    If Socket.Connected = True Then
        Log("Connected")
    End If
    StartMessageLoop
End Sub


Sub Socket1_Connected (Successful As Boolean)
    If Successful Then
        astream.Initialize(Socket.InputStream, Socket.OutputStream, "astreams")
    Else
        ' raise error here
    End If
 
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim Msg As String
    Msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(Msg)
 
End Sub


When I run Client.b4j I get the following message in the Logs

Waiting for debugger to connect...
shell switching to alternate port: 9054
shell switching to alternate port: 9055









 
Top