Android Question Set socket local port manually

KZero

Active Member
Licensed User
Longtime User
Hello,

i need to set Socket local ports manually the current socket able to set the remote port only

any idea ?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim mysocket As Socket
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     mysocket.Initialize("socket")
     BindToLocalPort(mysocket, 54323)
     mysocket.Connect("google.com", 80, 0)
   End If

End Sub

Sub socket_Connected (Successful As Boolean)
   Log(Successful)
End Sub

Sub BindToLocalPort(socket As Socket, port As Int)
   Dim r As Reflector
   r.Target = socket
   Dim jo As JavaObject = r.GetField("socket")
   Dim inet As JavaObject
   Dim server As ServerSocket 'ignore
   inet.InitializeNewInstance("java.net.InetSocketAddress", Array(server.GetMyIP, port))
   jo.RunMethod("setReuseAddress", Array(True))
   jo.RunMethod("bind", Array(inet))
End Sub
 
Upvote 0
Top