LAN Broadcasting and IP

Advanced game related topics
ewardd
User
User
Posts: 33
Joined: Tue Jul 16, 2013 12:28 pm

LAN Broadcasting and IP

Post by ewardd »

Hello guys!

I've done LAN Broadcasting, so my client program can find my server program in LAN.
The idea is - client is broadcasting, server answer and says his own ip address (192.168.79.2)

Server is getting his own local ip by connecting to himself thru UDP and receiving IP Address from it.

Code: Select all

  FakeConnection = OpenNetworkConnection(Hostname(), UDPPort, #PB_Network_UDP)
  If FakeConnection
    LocalIP = GetClientIP(FakeConnection)
    CloseNetworkConnection(FakeConnection)
  EndIf
  
  ;IPString(LocalIP)
Client using this IP to connect to server.
It works alright on my own pc, if I run server and client, but when I run client on another PC, I get answer on broadcasting from server - server ip (192.168.79.2), but I cannot connect to it. Simply cant create a connection.

Checking IPs in cmd via ipconfig command I realized, that these two ips 192.168.72.2 (server PC) and 192.168.56.1 (client PC) are VirtualBox-HostOnly Network Adapter IPs.
Tho true Ethernet IPv4 addresses are 192.168.1.101 and 192.168.1.107 and using them I can connect successfully.

So, how can I get server real local ip (by real I mean not VirtualBox ones, but nice 192.168.1.101 ones), so it will tell client its ip and client will connect using it and not VirtualBox IPs.

P.s. Im not using VirtualBox for testing. There are only network adapters, that probably screw things up
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: LAN Broadcasting and IP

Post by Keya »

i think you might need an EventClient() call in there. The logic to your code doesnt seem right to me either because you're asking for the IP address from the 'connection', but as you're using UDP there's no real connection, so i think you can only determine the IP address after an event like receiving data or disconnection

or if you meant "how do i get my internet IP address instead of internal LAN/VM one" you might need to call out to one of those free public "whatsmyip" websites for that
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: LAN Broadcasting and IP

Post by heartbone »

Something like this is what I do to find the first two LAN physical NIC IP addresses on a machine.
The first one is usually the loopback address.

Code: Select all

If InitNetwork() = 0 : MessageRequester("PROGRAM","Could not initialize network interface.") : End : EndIf
ExamineIPAddresses() : IND= NextIPAddress()
If IND : INTADDR$= IPString(IND) : IND= NextIPAddress() : If IND : INTADDR2$= IPString(IND) : IND= NextIPAddress() : If IND : INTADDR3$= IPString(IND) : EndIf : EndIf : EndIf
Keep it BASIC.
ewardd
User
User
Posts: 33
Joined: Tue Jul 16, 2013 12:28 pm

Re: LAN Broadcasting and IP

Post by ewardd »

Keya, UDP is connectionless protocol, right, but you still can get the local IP of the host. I run that "fake connection" code on server. Server creates connection to itself using Hostname() instead of IP, and then, using GetClientIP() server gets the local IP address of himself, instead of Hostname.

Con = OpenNetworkConnection("MY_PC_HOSTNAME", UDPPort, #PB_Network_UDP) and then GetClientIP(Con) gonna return IP instead of hostname, if hostname is specified.


heartbone, yeah, i know ExamineIPAddresses(). The thing is, that im getting VirtualBox lan ip from there, which I dont need at all, because if server will send to client VB ip, client will be unable to connect.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: LAN Broadcasting and IP

Post by Keya »

so your OP mentioned in ipconfig you could see IP addresses you weren't after, but was it also showing you the IP address(es) you were after?
DarkDragon
Addict
Addict
Posts: 2218
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: LAN Broadcasting and IP

Post by DarkDragon »

You basically described your problem as "How do I get my external ip, that is internal inside another LAN".
ewardd wrote:The thing is, that im getting VirtualBox lan ip from there, which I dont need at all, because if server will send to client VB ip, client will be unable to connect.
I think that highly depends on the way virtualbox is configured. Usually it is using a virtual "router" and NAT. Thats like if you put a router behind a network which has a completely different subnet. The server can't ever know for sure his IP address as seen from the client without sending something in this case. Also the self-connection you use is not useful, since many of the computers, routers and network devices are intelligent enough to not send it outside the subnet. In fact I have my hostname in the /etc/hosts file leading to a local connection only. Also my previous router (I think it was a telekom device) didn't allow connections from the inside over the outside ip back to the inside.

As you send something to the broadcaster, can't the broadcaster find the IP address of the replier itself? I mean every IPv4 package contains source and target IP address.
bye,
Daniel
Post Reply