Can't connect to own server

I’m new to Unity. I want to communicate with my own server which right now is a super simple script written in Clojure, that prints the payload from client.

It’s running localhost:8080. I connect with telnet and it works.

I looked how to connect with server in Unity. I found I need 1. Network view - added one to my player script. 2. Code to connect:

I couldn’t find a “pure” example about how to do this, but this is what I came up, from many examples:

private string serverHost = "127.0.0.1";
private int serverPort = 8080;
	
void Start () {
    NetworkConnectionError ne = Network.Connect(serverHost, serverPort);
    Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
}

It doesn’t work. Right after start I get:

The connection request to 127.0.0.1:8080 failed. Are you sure the server can be connected to?

I also logged NetworkConnectionError ne and shows NoError.

I also tried using the internal IP in my LAN - same result. I try again with telnet 127.0.0.1:8080 and everything works.

What’s the problem here? Do I have to implement a protocol in the server? But then I guess I would get a different sort of error message, this, looks like it can’t reach the server at all.

Thanks!

None of the built in networking works with anything but another computer which also uses the built in networking. There is no way around this. You can not write your own server which communicates with a unity client which uses the built in networking, sorry.

If you wanna connect to custom socket servers you need to use .NET sockets.