B4J Question How to connect client and server using Socket in same pc

I have Posted My code.I successfully initialize server but I can't connect sever to Client.Can anyone please tell me what is wrong in my code ? Can i connect client and server in the same pc ?

Server Code

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public Server As ServerSocket
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    server_side
  
End Sub

Sub server_side()

    Try
        Server.Initialize(6000,"localhost")
        Server.Listen()
        If Server.IsInitialized = True Then
            Log("Started")      
        End If
  
    Catch
        Log("Waiting for Client")
    End Try
 
End Sub


Client Code
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public socket As Socket
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Try  
        socket.Initialize("socket")
        socket.Connect("localhost",6000,10000)  
        If socket.Connected = True Then
            Log("Connected")
        Else
            Log("Not Connected")  
        End If  
    Catch
            Log("Waiting to server")
    End Try
 
End Sub
 
Last edited:
Top