B4J Question get the client IP with B4J?

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi, is posible get the client IP with B4J?

i'm using ServerSocket, working very well.
but i'm no able to get client ip.

here my code
[servermanager]
B4X:
Sub Class_Globals
    Private server As ServerSocket
    Private clients As List
End Sub

Public Sub Initialize (port As Int)
    server.Initialize(port, "server")
    clients.Initialize
    server.Listen
End Sub

Private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Dim c As Client
        c.Initialize(NewSocket, Me)
        clients.Add(c)
    End If
    server.Listen
End Sub

Public Sub getMyIp As String
    LogError(server.GetMyIP)
    Return server.GetMyIP
End Sub


Public Sub getNumberOfClients As Int
    LogError(clients.Size)
    Return clients.Size
End Sub


Public Sub ClientDisconnected (client1 As Client)
    clients.RemoveAt(clients.IndexOf(client1))
End Sub


i here how initialize de server manager

B4X:
Sub Class_Globals
    Private astream As AsyncStreams
    Private mManager As ServerManager
    Public String2Send As String
End Sub

Public Sub Initialize (socket As Socket, manager As ServerManager)
    mManager = manager    
    astream.Initialize(socket.InputStream, socket.OutputStream, "astream")
    Dim s As String
    astream.Write(s.Replace(CRLF, Main.EOL).GetBytes("UTF8"))
End Sub

Public Sub NewFrame (header() As Byte, data() As Byte)
    astream.Write(header)
    astream.Write(data)
    astream.Write(Main.EOL.GetBytes("UTF8"))
End Sub

Private Sub AStream_NewData (Buffer() As Byte)
    

'

    
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log($"Result  =>  ${s}"$)
    Dim p As String = s.Replace("[","").Replace("]","")
    Log($"Result  =>  ${p}"$)
    Dim result() As String =  Regex.Split(",",p)


    For x = 0 To result.Length -1 
        Log(result(x))
    Next
'    
'    If result(3) = "UD_LTE" Then 
'        Log("4G connection")
'    else if result(3) = "UD_WCDMA" Then
'        Log("3G connection")
'    else if result(3) = "UD" Then
'        Log("2G connection")
'        
        
'    End If
    
    
'    Dim deviceID As String = result(1)

    If s.Contains("LK") Then 
        SendData(s)
        Log(DateTime.Time(DateTime.Now))
    End If


    Dim result() As String =  Regex.Split(",",s)    
    
End Sub

Public Sub SendData (data As String )
    Log("Sending DATA " & data)
    astream.Write(data.GetBytes("UTF8"))
    'astream.Close
End Sub

Private Sub AStream_Terminated
    LogError("Closing Client Connection")
End Sub

Private Sub AStream_Error
    mManager.ClientDisconnected(Me)
    LogError("Astream Error")
End Sub



Sub latitude_conv(data As String) As String
    Dim degree As String = data.SubString2(0,2)
    Dim minutes As String = data.SubString2(2,9)
    Dim result As Float = degree+minutes/60
    Return result
End Sub

Sub longitude_conv(data As String) As String
    Dim degree As String = data.SubString2(0,3)
    Dim degree1 As String = degree.Replace("0","-")
    Dim minutes As String = data.SubString2(3,9)
    Log(degree1)
    Dim result As Float = degree+minutes/60
    Return "-"&result
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code:
B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
   Dim jo As JavaObject = NewSocket
   Dim socket As JavaObject = jo.GetField("socket")
   Return socket.RunMethodJO("getInetAddress", Null)).RunMethod("getHostAddress", Null)
End Sub

Your code looks wrong. There is a wrong assumption that messages will not be split or merged.
How are the clients implemented? Why build a socket server? Why not prefix mode? Why not AsyncStreamsText?
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
Untested code:
B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
   Dim jo As JavaObject = NewSocket
   Dim socket As JavaObject = jo.GetField("socket")
   Return socket.RunMethodJO("getInetAddress", Null)).RunMethod("getHostAddress", Null)
End Sub

Your code looks wrong. There is a wrong assumption that messages will not be split or merged.
How are the clients implemented? Why build a socket server? Why not prefix mode? Why not AsyncStreamsText?

There is a wrong assumption that messages will not be split or merged.
thats true, just learning all the data i'm receviging, a dondt have de operational code of this device.


let me check prefix mode and AsyncStreamsText.

thanks
 
Upvote 0
Top