B4J Question serversocket listening on a single IP

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Is it possible to have the serversocket to listen on a given port on a single IP if the PC it is running on has multiple IP addresses ?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to bind the serversocket to a specific ip address:
B4X:
Sub InitializeWithIp(Server As ServerSocket, EventName As String, Port As Int, ip As String)
   Server.Initialize(0, EventName)
   Dim ia As JavaObject
   ia = ia.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(ip))
   Dim s As JavaObject = Server
   Dim socket As JavaObject
   socket.InitializeNewInstance("java.net.ServerSocket", Array(Port, 50, ia))
   s.SetField("ssocket", socket)
End Sub

Example:
B4X:
Dim ServerSocket1 As ServerSocket
InitializeWithIp(ServerSocket1, "ServerSocket1", 55555, "127.0.0.1")
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Use this functions my Server Socket is still listening to an other IP from a vmware adapter.
GetMyIP give me an other ipaddress
Thanks for help
 
Last edited:
Upvote 0
Top