Hi, I am fairly new to Unity, but I have created an app which upon the click of a button sends a simple TCP message to a very simple TCP server console application. The code works perfectly when i run it on my PC, but when i build it for Android and test it through my phone the app is not able to connect. Am i doing it wrong? Is simple TCP interaction done differently with android? Or can someone point me towards a good Android TCP tutorial? Here’s my Unity code:
public Texture btnTexture;
public GUIStyle style;
TcpClient mySocket = new TcpClient();
public NetworkStream theStream;
public String Host = "192.168.2.15";
public string banner = "";
public Int32 Port = 8888;
public bool sent = false;
StreamWriter theWriter;
StreamReader theReader;
void OnGUI()
{
if (GUI.Button (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 2) - Screen.height/4,
Screen.width/2, Screen.width/2), btnTexture))
{
try
{
if(!sent)
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
sent = true;
banner = "connected";
}
theWriter.Write("TCP Message" + " $");
theWriter.Flush();
//Application.LoadLevel ("Page1");
}
catch(Exception e)
{
banner = "not connected";
}
}
GUI.Label (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 2) - Screen.height/4, 20, 20), banner, style);
}