B4J Question How to Set Network Server to localhost for Interprocess Comms

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

I wish to use network comms over the local host address (127.0.0.1) for communication with a B4X background task. I can get it working using the IP address that set on the system, but I cannot work out how to get the server to listen on the localhost address.

Here is my code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private server As ServerSocket
    Private client As Socket
    Private Astream As AsyncStreams
    Public IsConnected As Boolean
    Private tmr As Timer
    Private lblState As Label
End Sub


'============================================================================================
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("LayoutServer")
    MainForm.Title = "IPC Server"
    MainForm.Show

    listenForConnections
    tmr.Initialize("tmr", 1000)
End Sub


'============================================================================================
private Sub listenForConnections
    server.Initialize(17170, "server")
    Log($"My ip is: ${server.GetMyIP}"$)
    server.Listen
    Wait for server_NewConnection(successful As Boolean, NewSocket As Socket)
    Log("New Connection")
    If successful Then
        If Astream.IsInitialized Then
            Astream.Close
        End If
        Astream.InitializePrefix(NewSocket.InputStream, True, NewSocket.OutputStream, "astream")
        SetState(True)
    End If
End Sub

I had a look at:

Interprocess Comms

But I don't understand it. The attached zip file doesn't have complete projects so I couldn't figure out what the variables were.

I used the Video Tutorial on networking to build my system, but I can't figure out the localhost stuff.

I have attached the client and server projects in the zip.

Could anyone help with this?

Best regards
Rob
 

Attachments

  • InterProcComms_001.zip
    5.9 KB · Views: 199

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel

Yehh!

Silly me (again.) I forgot to change the port number so both apps had the same one.

I now get what the loop-back IP address means.

It's all working now.

I am making a little controller app for my micro-processor server so I can pause it and shut it down gracefully instead of just killing it in the task manager.

I will post on forum when I've finished it. It might be useful to others.

It's hard to get good customers these days.

best regards
Rob
 
Upvote 0
Top