AnyIpv4 and LoopbackIpv4
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.
Answer by Gavor · May 05, 2020 at 12:27 AM
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.
m_Driver = NetworkDriver.Create();
m_Connection = default(NetworkConnection);
// Basically I convert my string IP into an IPAddress object using Parse
IPAddress serverAddress = IPAddress.Parse(GameObject.Find("IP").GetComponent<InputField>().text);
NativeArray<byte> nativeArrayAddress;
// Convert that into a NativeArray of byte data
nativeArrayAddress = new NativeArray<byte>(serverAddress.GetAddressBytes().Length, Allocator.Temp);
nativeArrayAddress.CopyFrom(serverAddress.GetAddressBytes());
// Set to AnyIpv4
NetworkEndPoint endpoint = NetworkEndPoint.AnyIpv4;
endpoint.SetRawAddressBytes(nativeArrayAddress); // Use SetRawAddressBytes
endpoint.Port = 9000;
// Then call the Connect method on your driver.
m_Connection = m_Driver.Connect(endpoint);
Thank you so much for the answer! I really appreciate it. Question: Why is the port always set to 9000? Should I allow the player to alter it? And also this should allow people on different networks to play, right? Also what is addressData and where does it come from, is address data just nativeArrayAddress?
The port doesn't have to be 9000, in a later version of this code I use a value entered into an inputfield by the user. It does have to be an unused port though. I have tested this with friends and it works 100%. $$anonymous$$ake sure the port uses is forwarded on the router (depends on your network and internet, but 99% of the time you'll need to forward the port).
Apologies, I put this code together from two different sources and didn't proof read it well enough it seems. addressData is nativeArrayAddress. I'll amend the original answer.
Edit 4: Previous problem isn't a problem anymore, I got it working! A problem though is that my console is being spammed with:
Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 5)
no idea why this is happening, pretty sure it's because I'm not releasing something or rather, but if that means I need to close and open the connection every time I want to send data then that's a bit annoying. Basically, with this I'm just trying to have the world's most basic multiplayer where the transform.position of the players is just sent to the host and then to the other players, but idk how to stream data without doing something dumb and unoptimized like having the connection open and close unnecessarily every time data is sent. I really appreciate your help up until this point :), not sure if background in networking experience would be able to help
usually that problem is called if the allocator is using Allocator.Temp or Allocator.TempJob and the NativeArray has not be disposed by the 4th frame. ideally, it should be disposed off at the end of the same function when the job is completed. Hope this helps
Answer by IgnitedGames · Mar 17, 2020 at 04:35 AM
Another unanswered question there, but the answer to this question is here.
Your answer
Follow this Question
Related Questions
What is the most efficient way to host multiple games on different ports? 1 Answer
Multiplayer Camera Rotation On Client 0 Answers
UNET match making without the relay server 3 Answers
Android multiplayer using WiFi 5 Answers
Unity Networking 1 Answer