0
\$\begingroup\$

I am currently working on a "Master server" system for our Unity Game using Unity's Low Level "Transport layer API" for networking.

My problem is that I would like to prevent clients from connecting to eachother. I have tried not giving them a port on the Addhost() call, but it doesn't seem to change anything. They also do not seem to detect the connections, so basically I find myself with NetworkTransport.Connect(..) calls that work (the error byte is 0, meaning no error) but no actual connections. I don't understand what's going on.

I can't really show my code because it's quite long, but here's how it works, in summary :

The Clients objects initialise a Socket as normal with only one Reliable channel, no port, and one connection allowed by default. Then they have the ability to connect to another socket, as you'd expect. My problem COULD be coming from there : for testing purposes the IP I use for testing is 127.0.0.1. What I do not understand is, how can it "find itself" when it is not bound to any port ? And more importantly how can it NOT find the Master Server object whose socket IS bound to a port ?

Speaking about Master Server, it's an object that also initialises its socket, but with a port and many allowed connections.

When a client connects and the connection did not yield any errors, it considers the connection to the Master server to be successful and proceeds sending a message containing some information for the server about this client : the username chosen by the player for example.

When the master server receives a connection event, it registers the received ConnectionID and waits for more information from that same connection, for example the aforementioned username.

Both the client and the server handle receiving messages with the NetworkListener class (Static). On each frame it executes the Listen() method which executes a "normal" NetworkTransport.Receive() body. I can give you that part of the code as it is fairly common in design :

public static void Listen()
{
    int recHostID;
    int recConnectionID;
    int recChannelID;
    byte[] buffer = new byte[1024];
    int recDataSize;
    byte error;
    NetworkEventType e = NetworkTransport.Receive(out recHostID, out recConnectionID, out recChannelID, buffer, 1024, out recDataSize, out error);
    Debug.Log("Listening");
    switch(e)
    {
        case (NetworkEventType.Nothing):
            break;
        case (NetworkEventType.ConnectEvent):
            Debug.Log("Connection established. ConnectionID = " + recConnectionID);
            if (OnConnectionReceived != null)
            OnConnectionReceived(recHostID, recConnectionID, recChannelID);
            break;
        case (NetworkEventType.DataEvent):
            Debug.Log("Received data !");
            BinaryFormatter f = new BinaryFormatter();
            MemoryStream stream = new MemoryStream(buffer);
            Message = f.Deserialize(stream) as NetworkMessage;
            break;
        case (NetworkEventType.DisconnectEvent):
            Debug.Log("Connection terminated. ConnectionID = " + recConnectionID);
            break;
    }
}

Network message is a simple data structure which contains a ConnectionID (the one it's from), a message title and content. The title is used to determine the general purpose of the message. The content is the rest of the information.

What happens in my current build of the client :

The user is prompted to enter a username, then he clicks "Search for match". The search for match button is supposed to trigger the connection to the Master Server. The logs say that the connection was successful (as no errors are detected) but no Connection event is received on either side. This of course prevents everything else from working.

If you need further description of the problem just ask and I'll answer ASAP.

Thanks a lot in advance !

\$\endgroup\$
-2
\$\begingroup\$

Your Listen method needs to be called in the Update "loop", like such:

void Update()
{
   Listen();
}

Edit: If the connection establishment works, you should check your if statement. Thanks for the downvote..

You could also use the debugger to check your source code or simply use google :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.