- Home /
The question is answered, right answer was accepted
Unity tcp_client only working on localhost
Hello,
I am using Unity 2018.1.5 to connect to my python server through TCP. I can only manage to connect when running client & server on the same machine and I am using port 50000. If I change the port to pretty much anything else OR if I try to run the unity executable on any other device on the network, I get no connection.
I have tried using my local ip "192.168.100.9", my public IP and even a domain name. I have opened the correct ports on my router, and firefox/chrome can connect to my server from any device in the network without a problem.
So, the way I see it, unity is doing something that prevents other ports from working and cannot connect unless the server is running on the same machine. (I should mention that when running on the server machine, "localhost", my local ip and my public ip all seem to work with unity).
The code I am using:
public String host = "localhost";
public int port = 50000;
try
{
tcp_socket = new TcpClient(host, port);
net_stream = tcp_socket.GetStream();
socket_writer = new StreamWriter(net_stream);
}
catch (Exception e)
{
// Something went wrong
Debug.Log("Socket error: " + e);
}
Answer by alextagtal · Jul 02, 2018 at 02:14 PM
Figured it out, I was using the public String variable "host" to connect, and I kept changing its value in the script (which is the default value), but it already had a set value in the editor (namely "localhost") so it wasn't really updating and unity was always trying to connect to localhost.
A simple "Debug.Log(host)" would have saved me the trouble...
Great you figured it out (after just 16 $$anonymous$$utes ^^). Some tips for the future:
Only make variables public which you really want to change / serialize it in the Unity editor. If the variable should be given by the user at runtime, don't make it public / serializable.
For debugging on an actual device i strongly recommend to just use the I$$anonymous$$GUI. You can simply use a TextArea as a simple console for log messages and implement a simple UI
void OnGUI()
{
host = GUILayout.TextField(host);
if (GUILayout.Button("connect"))
Connect();
}
In this case you would immediately see that host is still "localhost" ^^. For a simple on-device logging solution:
static string log = "";
public static void Log(string aText)
{
log += aText + "\n";
}
// inside OnGUI
log = GUILayout.TextArea(log);
if (GUILayout.Button("clear"))
log = "";
Finally when asking questions avoid too much pseudo code if you have a concrete problem with your code. $$anonymous$$any issues are related to when the code in questio is executed. So it would be great to know the context / stacktrace from where the code is executed.
When i was into mobile development i actually created a whole set of debugging tools with the I$$anonymous$$GUI. That included a simple scene hierarchy browser / inspector, a console for all debug.Log messages and even a seperate free fly camera
Follow this Question
Related Questions
This script freezes unity. Anyone knows answer? 1 Answer
Unity TCP async functions 0 Answers
Problems with TLS handshake 0 Answers
Multiple Cars not working 1 Answer
How can I read data sent using the Transport API with python server 1 Answer