B4J Question serversocket listening on a single IP

Discussion in 'B4J Questions' started by wl, Aug 1, 2015.

  1. wl

    wl Well-Known Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to bind the serversocket to a specific ip address:
    Code:
    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:
    Code:
    Dim ServerSocket1 As ServerSocket
    InitializeWithIp(ServerSocket1, 
    "ServerSocket1"55555"127.0.0.1")
     
    dar2o3, lemonisdead and wl like this.
  3. swissmade

    swissmade Active Member Licensed 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: Jun 1, 2016
  4. Erel

    Erel Administrator Staff Member Licensed User

    GetMyIp is not relevant and will not be affected by this code.
     
    swissmade likes this.
  5. swissmade

    swissmade Active Member Licensed User

    Ok thanks
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice