OpenNetworkConnection() LocalPort parameter not respected

Mac OSX specific forum
verleihnix
User
User
Posts: 13
Joined: Sun Dec 14, 2008 4:55 am
Location: Switzerland

OpenNetworkConnection() LocalPort parameter not respected

Post by verleihnix »

Hello Community,

i came across some bug while using OpenNetworkConnection() on a MAC.

in the Documentation it states:

Connection = OpenNetworkConnection(ServerName$, Port [, Mode [, TimeOut [, LocalIP$ [, LocalPort]]]])

LocalPort (optional) The local port to bind the connection to. By default a random local port is automatically chosen for the new connection, but it can be overridden with this parameter


=== NETWORK CLIENT SIDE CODE SNIP ===
LocalPort = 7000
Timeout = 5000
ConnectionID = OpenNetworkConnection("192.168.168.103", Port,#PB_Network_TCP,Timeout,"",LocalPort)
=== END NETWORK CLIENT SIDE CODE SNIP ===

when i connect to the server and i fetch the connected port with the GetClientPort(ClientID) command, i can see that the port on the client side has been randomly choosen instead of the port I configured it to be.

the same code running on the Windows is working fine.

I am runningPureBasic 5.22 LTS (x64) on a Mac OS X

Kind regards

Mike
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: OpenNetworkConnection() LocalPort parameter not respecte

Post by Fred »

Could you post a full working snippet please ?
verleihnix
User
User
Posts: 13
Joined: Sun Dec 14, 2008 4:55 am
Location: Switzerland

Re: OpenNetworkConnection() LocalPort parameter not respecte

Post by verleihnix »

Yes, sure, i am happy to provide the code :)

=== SERVER ===

Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

Port = 6832

If CreateNetworkServer(0, Port)
  Debug "PureBasic - Server created"
  Repeat
    SEvent = NetworkServerEvent()
    If SEvent
      ClientID = EventClient()
      Select SEvent
        Case #PB_NetworkEvent_Connect
          Debug "Server - A new client has connected! Client port: " + GetClientPort(ClientID)
          Delay(1000)
          Quit=1
      EndSelect
    EndIf
  Until  Quit=1
CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf

End   
=== CLIENT ====

Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

RemotePort = 6832
LocalPort = 7000
Timeout = 5000

ConnectionID = OpenNetworkConnection("127.0.0.1",RemotePort,#PB_Network_TCP,Timeout,"",LocalPort)
If ConnectionID
  Debug "PureBasic - Client connected to server on port " + Str(LocalPort) 
  CloseNetworkConnection(ConnectionID)
Else
  MessageRequester("PureBasic - Client", "Can't find the server (Is it launched ?).", 0)
EndIf
  
End 

Now When i execute the "Client" on a MAC, the LocalPort is selected randomly instead of the requested port "LocalPort = 7000".

When i execute this code on a Windows box, all goes well and the Local Port is set as requested.

so on the Server debug window:

when the Windows client connects:
>PureBasic - Server created
>PureBasic - Server - A new client has connected ! Client port: 7000

When the MAc connects:
>PureBasic - Server created
>PureBasic - Server - A new client has connected ! Client port: 2739 <----- some random port number

Greets Mike
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: OpenNetworkConnection() LocalPort parameter not respecte

Post by Fred »

It seems to work OK here, can anybody else confirm the issue ?
verleihnix
User
User
Posts: 13
Joined: Sun Dec 14, 2008 4:55 am
Location: Switzerland

Re: OpenNetworkConnection() LocalPort parameter not respecte

Post by verleihnix »

Mistery located, but not solved yet!

I dug deeper into this issue and I found out the following:

When i run this code on a NATIVE MAC (as in the physical Macbook Pro), then everything works very well.

I was playing around with a virtual MAC running in a Oracle Virtualbox, it seems, that the network virtualization layer of Virtualbox seems to mess up things. In the virtual MAC, the OpenNetworkConnection() LocalPort parameter is not respected.

I dont think its in the resonsibility of the Purebasic compiler, but its still strange that this happens. If you have any ideas or tips to try, I am willing to find out the reason behind this. If its not worth it, we can just go over it and continue happy with our lives :) and forget about this. Your call.

Apologies if I have wasted your time! Thank you very much for the wonderful product you have created and the effort you put into all of this!

Maybe someone comes across this problem in the future and this thread can save some hours and lots of head scratching!

Greetings Mike
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: OpenNetworkConnection() LocalPort parameter not respecte

Post by Fred »

Moved to Mac forum as we don't officially support Virtualized env has it can be an error in the emulator.
Post Reply