I have created a Client and Server Script following the manual and a tutorial. When just attaching the scripts to objects everything works fine, but when making the unity editor a host (by creating server as well as client) and opening a build doing the same but this time only a client, it just doesn’t connect. I’m using the LoopbackIpv4 address (localhost).
Here is my Code:
BaseClient.cs
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Collections;
public class BaseClient : MonoBehaviour
{
public NetworkDriver driver;
protected NetworkConnection serverConnection;
private string ipAddr = "127.0.0.1";
private ushort port = 44444;
public ushort GetPort() { return port; }
public void SetPort(ushort port) { this.port = port; }
public string GetIpAddr() { return ipAddr; }
public void SetIpAddr(string IpAddr) { this.ipAddr = IpAddr; }
#if UNITY_EDITOR
private void Start() { Init(); }
private void Update() { UpdateServer(); }
private void OnDestroy() { Shutdown(); }
#endif
public virtual void Init()
{
driver = NetworkDriver.Create();
serverConnection = default;
NetworkEndpoint endPoint = NetworkEndpoint.LoopbackIpv4.WithPort(44444);
serverConnection = driver.Connect(endPoint);
}
public virtual void Shutdown()
{
driver.Dispose();
}
public virtual void UpdateServer()
{
driver.ScheduleUpdate().Complete();
UpdateMessagePump();
IsConnectionActive();
}
private void IsConnectionActive()
{
if (!serverConnection.IsCreated)
{
Debug.Log("Lost connection");
}
}
protected virtual void UpdateMessagePump()
{
DataStreamReader stream;
NetworkEvent.Type cmd;
while ((cmd = serverConnection.PopEvent(driver, out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Connect)
{
Debug.Log("Client connected to server");
}
else if (cmd == NetworkEvent.Type.Data)
{
OnDataReceived(stream);
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
serverConnection = default(NetworkConnection);
Debug.Log("Client disconnected");
}
}
}
public virtual void OnDataReceived(DataStreamReader stream)
{
NetPacket packet = null;
var packetType = (PacketType)stream.ReadByte();
packet = NetUtilities.Instance.CreatePacket(stream, packetType);
packet.ReceivedOnClient();
}
public virtual void SendPacketToServer(NetPacket packet)
{
DataStreamWriter writer;
driver.BeginSend(serverConnection, out writer);
packet.Serialize(ref writer);
driver.EndSend(writer);
}
}*
* *BaseServer.cs* *
csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Collections;
public class BaseServer : MonoBehaviour
{
public NetworkDriver driver;
protected NativeList connections;
[SerializeField] private bool isOpenForNewPlayers = true;
public bool IsOpenForNewPlayers() { return isOpenForNewPlayers; }
public void SetIsOpenForNewPlayers(bool isOpen) { isOpenForNewPlayers = isOpen; }
#if UNITY_EDITOR
private void Start() { Init(); }
private void Update() { UpdateServer(); }
private void OnDestroy() { Shutdown(); }
#endif
public virtual void Init()
{
driver = NetworkDriver.Create();
connections = new NativeList<NetworkConnection>(4, Allocator.Persistent);
NetworkEndpoint endPoint = NetworkEndpoint.AnyIpv4.WithPort(44444);
if (driver.Bind(endPoint) != 0)
Debug.Log("Could not bind to Port " + endPoint.Port);
else
driver.Listen();
}
public virtual void Shutdown()
{
driver.Dispose();
connections.Dispose();
}
public virtual void UpdateServer()
{
driver.ScheduleUpdate().Complete();
CleanUpConnection();
//if (IsOpenForNewPlayers())
AcceptNewConnections();
UpdateMessagePump();
}
private void CleanUpConnection()
{
for (int i = 0; i < connections.Length; i++)
{
if (!connections[i].IsCreated)
{
connections.RemoveAtSwapBack(i);
i--;
}
}
}
private void AcceptNewConnections()
{
NetworkConnection connection;
while((connection = driver.Accept()) != default)
{
Debug.Log("Added new connection");
connections.Add(connection);
}
}
protected virtual void UpdateMessagePump()
{
DataStreamReader stream;
for(int i = 0; i < connections.Length; i++)
{
NetworkEvent.Type cmd;
while ((cmd = driver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Data)
{
OnDataReceived(stream);
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
connections[i] = default(NetworkConnection);
Debug.Log("Client disconnect");
}
}
}
}
public virtual void OnDataReceived(DataStreamReader stream)
{
NetPacket packet = null;
var packetType = (PacketType)stream.ReadByte();
packet = NetUtilities.Instance.CreatePacket(stream, packetType);
packet.ReceivedOnServer(this);
}
public virtual void BroadcastPacket(NetPacket packet)
{
for (int i = 0; i < connections.Length; i++)
{
if (connections[i].IsCreated)
SendPacketToClient(connections[i], packet);
}
}
public virtual void SendPacketToClient(NetworkConnection connection, NetPacket packet)
{
DataStreamWriter writer;
driver.BeginSend(connection, out writer);
packet.Serialize(ref writer);
driver.EndSend(writer);
}
}*
```
I am using Unity 2022.3.18f1, Unity Transport v.2.1.0.
Thanks in advance!