Network Discovery - client not receiving broadcast
I'm using Network Discovery for Server/Clients.
I have a simple UI that allows the user to select whether they are the server or client, which then adds the correct prefab to the scene. Other than Use Network Manager and Show GUI set to false, all other settings are default.
I tried running the Broadcast example project and the client also did not receive broadcasts. I'm running unity 2017.1.0f3.
The computers are connected using ethernet cables and a Netgear router, all running Windows 10.
Server:
[AddComponentMenu( "Networking/NetServer" )]
public class NetServer : NetworkDiscovery
{
void Start()
{
Application.runInBackground = true;
StartServer();
}
//Call to create a server
public void StartServer()
{
int serverPort = CreateServer();
if( serverPort != -1 )
{
Debug.Log( "Server created on port : " + serverPort );
broadcastData = serverPort.ToString();
Initialize();
StartAsServer();
}
else
{
Debug.Log( "Failed to create Server" );
}
}
int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;
//Creates a server then returns the port the server is created with. Returns -1 if server is not created
private int CreateServer()
{
int serverPort = -1;
//Connect to default port
bool serverCreated = NetworkServer.Listen( defaultPort );
if( serverCreated )
{
serverPort = defaultPort;
Debug.Log( "Server Created with deafault port" );
}
else
{
Debug.Log( "Failed to create with the default port" );
//Try to create server with other port from min to max except the default port which we trid already
for( int tempPort = minPort; tempPort <= maxPort; tempPort++ )
{
//Skip the default port since we have already tried it
if( tempPort != defaultPort )
{
//Exit loop if successfully create a server
if( NetworkServer.Listen( tempPort ) )
{
serverPort = tempPort;
break;
}
//If this is the max port and server is not still created, show, failed to create server error
if( tempPort == maxPort )
{
Debug.LogError( "Failed to create server" );
}
}
}
}
return serverPort;
}
}
Client:
[AddComponentMenu("Networking/NetClient")]
public class NetClient : NetworkDiscovery
{
void Start()
{
StartClient();
}
public void StartClient()
{
Initialize();
StartAsClient();
}
public override void OnReceivedBroadcast( string fromAddress, string data )
{
Debug.Log( "Server Found: " + fromAddress + "\n" + "Data: " + data );
}
}
I've implemented Unity's network discovery class in my project to step through the code: https://bitbucket.org/Unity-Technol...Simple.cs?at=5.4&fileviewer=file-view-default
My client never receives a network event in the Update()
networkEvent = NetworkTransport.ReceiveFromHost( m_HostId, out connectionId, out channelId, m_MsgInBuffer, k_MaxBroadcastMsgSize, out receivedSize, out error );
No error, just networkEvent is NetworkEventType.Nothing
Wireshark shows the broadcast coming through:
Src: 10.1.10.125, Dst: 10.1.255.255
Src Port: 49220, Dst Port: 47777
Length: 67
Client is 10.1.10.13
Server is 10.1.10.125
I noticed that in the NetworkDiscovery code, in StartAsServer() when calling:
NetworkTransport.Addhost() - the hostId returns is 1.
In StartAsClient() AddHost returns hostId 0.
In the Update(), ReceiveFromHost() takes the hostId.
I tried just setting it to 1 on the client and get the error: "host id out of bound id {1} max id should be greater 0 and less than {1}"
Not sure if the id's need to match or this is fine?
Answer by Suraksha226 · Jul 22, 2019 at 05:27 AM
I removed the Update()
function from NetworkDiscovery
and replaced it with InvokeRepeating()
Answer by frgaj · Feb 01, 2018 at 09:23 PM
I think this is a bug with network discovery on windows, but I've found a workaround. I couldn't get it to work until I opened the same port with the UdpClient class and sent at least one byte of data to the broadcast address. After closing the port, the network discovery component started receiving the broadcasts from my other servers.
Your answer
Follow this Question
Related Questions
Card Game using Network Manager 0 Answers
P2P Broadcasting issue 0 Answers
Unet with LAN and wifi Hotspot 3 Answers
Is there any way to increase the rate of messages on Unet? 0 Answers