Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AnyIpv4 and LoopbackIpv4

Discussion in 'Multiplayer' started by JaimeShirazi, Mar 16, 2020.

  1. JaimeShirazi

    JaimeShirazi

    Joined:
    Jan 12, 2019
    Posts:
    5
    I'm creating a pretty jank multiplayer solution for my game that uses unity's transport system. To keep it short, there is absolutely no explanation anywhere as to what the hell NetworkEndPoint.AnyIpv4 and NetworkEndPoint.LoopbackIpv4 are. I'm basically trying to make it so you can join someone's game by typing in their IP Address and Port, and Host the game by using your IPv4 Address and selecting a port. I am using Unity's jobified client and server scripts in the documentation, but it doesn't say why it chooses to use loopbackipv4 and anyipv4, nor what they do. Can I use NetworkEndPoint.Parse()? I tried using it, but it didn't work. Any help is greatly appreciated.
     

    Attached Files:

    spatialfree likes this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm sorry, I'm having a difficult time writing a post without it coming off as insulting though I don't mean to, because "loopback" is a beginner networking concept. Pinging the loopback address is like the 'Hello World' of learning networking. So I'll just link to the wiki page.

    https://en.wikipedia.org/wiki/IPv4#Loopback

    I'm sure AnyIPv4 allows "any" address, and LoopbackIPv4 allows only addresses in the "loopback" range.
     
    JaimeShirazi likes this.
  3. JaimeShirazi

    JaimeShirazi

    Joined:
    Jan 12, 2019
    Posts:
    5
    Nah don't worry about sounding insulting, I've never used networking before and as this new system is completely new there's a real lack of stuff online about it. I really appreciate any bit of information about this. I'm just not sure how I'm meant to make it connect to a specific IP and Port if the AnyIpv4 and LoopbackIpv4 values aren't it. Here are the scripts I'm basing it off of, my situation is I understand how Jobs work and can read what each line does, but don't fully understand it. I can only get it to return as NetworkEvent.Type.Empty.

    JobifiedClient
    JobifiedServer

    I may just be entirely misunderstanding what this script is written to do, where it searches for literally any IP address willing to send data or something. Wish the documentation was a bit clearer.
     
    Last edited: Mar 17, 2020
  4. JaimeShirazi

    JaimeShirazi

    Joined:
    Jan 12, 2019
    Posts:
    5
    Bump, still looking for an answer
     
  5. JaimeShirazi

    JaimeShirazi

    Joined:
    Jan 12, 2019
    Posts:
    5
    Am still looking
     
  6. Gavor

    Gavor

    Joined:
    Apr 12, 2013
    Posts:
    7
    For what it's worth, I had the same problem today and am coming from a position of only having a high level understanding of networking and almost no Unity networking experience. Below is my solution.

    Basically I convert my string IP into an IPAddress object using Parse and then I convert that into a NativeArray of byte data. The endpoint object is set to AnyIpv4 and I use SetRawAddressBytes(), sending it the NativeArray.

    This may not be the best or only solution, this is just what I used today.

    Code (CSharp):
    1. m_Driver = NetworkDriver.Create();
    2.      m_Connection = default(NetworkConnection);
    3.      // Basically I convert my string IP into an IPAddress object using Parse
    4.      IPAddress serverAddress = IPAddress.Parse(GameObject.Find("IP").GetComponent<InputField>().text);
    5.      NativeArray<byte> nativeArrayAddress;
    6.      // Convert that into a NativeArray of byte data
    7.      addressData = new NativeArray<byte>(serverAddress.GetAddressBytes().Length, Allocator.Temp);
    8.      addressData.CopyFrom(serverAddress.GetAddressBytes());
    9.      // Set to AnyIpv4
    10.      NetworkEndPoint endpoint = NetworkEndPoint.AnyIpv4;
    11.      endpoint.SetRawAddressBytes(nativeArrayAddress);         // Use SetRawAddressBytes
    12.      endpoint.Port = 9000;
    13.    
    14.      // Then call the Connect method on your driver.
    15.      m_Connection = m_Driver.Connect(endpoint);
     
  7. Mrboseph

    Mrboseph

    Joined:
    Sep 27, 2014
    Posts:
    1
    Code (CSharp):
    1.  
    2.         string ipAddress = "192.168.0.1";
    3.         ushort port = 9000;
    4.         if (NetworkEndPoint.TryParse(ipAddress, port, out NetworkEndPoint endPoint))
    5.         {
    6.             m_Connection = m_driver.Connect(endPoint);
    7.         }
    This works for Transport Package Version 0.3.1-preview.4

    Edit : Clearly, substitute the address with target address and port.
     
    Anthelmed likes this.