Question by
KaDaK4 · Jun 18, 2018 at 07:58 PM ·
c#networkingbroadcastpeer-peer
P2P Broadcasting issue
Hi guys, so I'm trying to broadcast my local devices in order to open connections to them but it doesn't seem to work. Here is the simplified code:
[SerializeField] private int broadcastPort;
[SerializeField] private int broadcastKey;
[SerializeField] private int broadcastVersion;
[SerializeField] private int broadcastSubVersion;
[SerializeField] private int broadcastInterval;
[SerializeField] private string broadcastMsg;
[SerializeField] private Text text;
private List<int> connectionsIds = new List<int>();
private int hostId;
private byte reliableId;
private byte unreliableId;
private NetworkReader recReader;
#region Initialization
void Awake()
{
CreateHost(broadcastPort);
StartBroadcast(broadcastPort);
}
void CreateHost(int port)
{
// Inicio el network transport
NetworkTransport.Init();
// Creo la configuracion de la conexion
var cgf = new ConnectionConfig ();
reliableId = cgf.AddChannel (QosType.Reliable);
unreliableId = cgf.AddChannel (QosType.Unreliable);
var topology = new HostTopology(cgf, 4);
// Comienzo a hostear
hostId = NetworkTransport.AddHost (topology, port);
}
void Connect(string ip, int port)
{
// Me conecto a la ip y puerto indicados y guardo la conexion resultante
byte error;
var cnn = NetworkTransport.Connect (hostId, ip, port, 0, out error);
if(!connectionsIds.Contains(cnn))
connectionsIds.Add(cnn);
}
private void StartBroadcast(int port)
{
// Comienzo a broadcastear para conectarme a los demas dispositivos
byte error;
byte error2;
NetworkTransport.StartBroadcastDiscovery(hostId, port, broadcastKey, broadcastVersion, broadcastSubVersion, null, 0, broadcastInterval, out error);
NetworkTransport.SetBroadcastCredentials(hostId, broadcastKey, broadcastVersion, broadcastSubVersion, out error2);
}
#endregion
void Update()
{
ReceiveMessages ();
}
public void SendToAll(byte[] bytes)
{
// Envio la informacion recibida a todas las conexiones
byte error;
for (int i = 0; i < connectionsIds.Count; i++)
{
var cnn = connectionsIds[i];
NetworkTransport.Send(hostId, cnn, reliableId, bytes, bytes.Length, out error);
}
}
void ReceiveMessages()
{
byte error;
int recHostId;
int recCnnId;
int recChlnId;
int recBytesCount;
byte[] recBytes = new byte[1024];
while(true)
{
var eventType = NetworkTransport.Receive(out recHostId, out recCnnId, out recChlnId, recBytes, recBytes.Length, out recBytesCount, out error);
if (eventType == NetworkEventType.BroadcastEvent)
HandleBroadcastEvent();
}
}
private void HandleBroadcastEvent()
{
byte error;
int port;
string address;
NetworkTransport.GetBroadcastConnectionInfo(hostId, out address, out port, out error);
Connect(address, port);
}
I also get 3 connection entries and I don't know where they come from. It happens when I hit play in editor mode
Comment
Did you already solve this issue ? I am interested if you managed to get it work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612165016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullReferenceException at adding a NetworkTransformChild component 1 Answer
Local Multiplayer Basic shooting 1 Answer
Network is spawning more objects then desired 1 Answer
Unity Network (Player) 1 Answer
What's wrong with my script ? 0 Answers