Problems connecting Host and Client using Transport Layer API

Hi there!
I am having trouble trying to send simple messages from one instance of my game to another.
To try out the Transport Layer API I created a new script which does different thing for whether the instance is supposed to be a client or a host. Because the script is just to test things out I repurpose “Join” to send messages if i execute it after running it for the first time.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class networking : MonoBehaviour {

    HostTopology topology;
    int movementChannel;
    int hostId = -1;
    public bool isHost = true;
    int myConnectionId;
   
    void Update () {
        if (isHost && hostId != -1) {
            int usedHostId;
            int usedConnectionId;
            int usedChannelId;
            byte[] recBuffer = new byte[1024];
            int datasize;
            byte error;
            NetworkEventType n = NetworkTransport.Receive (out usedHostId, out usedConnectionId, out usedChannelId, recBuffer, 1024, out datasize, out error);
            switch (n) {
            case NetworkEventType.Nothing:
                //Debug.Log ("Nothing");
                break;
            case NetworkEventType.ConnectEvent:
                Debug.Log ("Got Connection");
                Debug.Log ("HostID:" + usedHostId + "  ConnectionID:" + usedConnectionId);
                break;
            case NetworkEventType.DataEvent:
                Debug.Log ("Got Data");
                Stream stream = new MemoryStream (recBuffer);
                BinaryFormatter formatter = new BinaryFormatter ();
                string message = formatter.Deserialize (stream) as string;
                Debug.Log ("The message is: " + message);
                break;
            case NetworkEventType.DisconnectEvent:
                Debug.Log ("Disconnection");
                break;
            }
        }
    }

    void sendMessage(string message){
        byte error;
        byte[] bytes = new byte[1024];
        Stream stream = new MemoryStream (bytes);
        BinaryFormatter formatter = new BinaryFormatter ();
        formatter.Serialize (stream, message);
        Debug.Log ("sending to connectionID:" + myConnectionId);
        bool ret = NetworkTransport.Send(hostId, myConnectionId, movementChannel, bytes, bytes.Length,  out error);
        Debug.Log ("Send " + ret + " Error " + error);
    }

    HostTopology getTopology () {
        NetworkTransport.Init();
        ConnectionConfig config = new ConnectionConfig();
        movementChannel = config.AddChannel(QosType.UnreliableSequenced);
        topology = new HostTopology (config, 7);
        return topology;
    }

    public void Host () {
        hostId = NetworkTransport.AddHost(getTopology(), 40555);
        Debug.Log ("I am a Host!");
        isHost = true;
    }

    public void Join () {
        if (isHost) {
            hostId = NetworkTransport.AddHost(getTopology(), 40554);
            Debug.Log ("I am a Client!");
            byte error;
            myConnectionId = NetworkTransport.Connect (hostId, "127.0.0.1", 40555, 0, out error);
            Debug.Log ("Connection:" + myConnectionId + " Connect error:" + error);
            isHost = false;
        } else {
            sendMessage("Hello Server!");
        }
    }
}

If I run my Script and execute “Host” in one game instance and “Join” in the other, I get “Got Connection” for the host and “Connection:1 Connect error:0” on the client. If I execute “Join” another time however, my client outputs "sending to connectionID:"1 and “Send False Error 2”.

How can it be that my connection is established (I evidently got an ID for it) without errors but I can’t send any messages?
If I start only the client I get the same output, even though there is no host running.

You should call Receive on server and client. When you call connect you just send request to connect, connect means that you received response for your reuest via networktransport.receive function

Thanks, now that I am also listening for packets on my client everything works!

You don’t need to add a port when you are creating a client socket with NetworkTransport.AddHost().