TCP data sending problem

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? Here’s my Unity code:

    public Texture btnTexture;
    public GUIStyle style;
    TcpClient mySocket = new TcpClient();
    public NetworkStream theStream;
    public String Host = "my_ip_address";
    public Int32 Port = 8888;
    public bool sent = false;

    void OnGUI()
    {
        if (GUI.Button (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 4), Screen.width/2, Screen.width/2), btnTexture))
        {
            if(!sent)
            {
                mySocket.Connect(Host, Port);
                theStream = mySocket.GetStream();
                sent = true;
            }
            byte[] sendStream = System.Text.Encoding.ASCII.GetBytes("TCP message sent from Phil's Phone" + " $");
            theStream.Write(sendStream, 0, sendStream.Length);
            theStream.Flush();
            //Application.LoadLevel ("Page1");
        }

    }

Sounds like it could be the firewall. It could also be the TcpListener (which I assume that you are using). Make sure you bind the listener to IPAdderess.Any and not to “127.0.0.1”.

Is your phone connected to the same network as your computer? I’m not sure if you intend for it to work outside of your network or not, but you shouldn’t have an issue if your phone is on the same network as the computer hosting the server. If you do it that way, make sure you use your local IP address to connect to your computer. However, if you intend to access your server from outside of your local network you will need to sort out the firewall issues with port forwarding etc and then use your external address to connect.

hi, Im on an android to android TCP connection, and Server is ok, is working with other native android App. but my Unity android App is never connecting, is not passing this line: socket = new TcpClient(host, port);
Someone knows why? Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.IO;
using Newtonsoft.Json;
using System.Net;
using System.Net.Sockets;
using UnityEngine.Networking;



public class SockMan : MonoBehaviour {

    public Text myText;

    private bool socketReady;
    private TcpClient socket;
    private NetworkStream stream;
    private StreamWriter writer;
    private StreamReader reader;

    void Start() {
        print("Start");
        ConnectToServer();
    }

    public void ConnectToServer() {
        print("Setup");
        myText.text = "Setup...";
        if(socketReady)
            return;
        String host = "192.168.0.15";
        Int32 port = 8080;

        try {
            print("Enter Try");
            myText.text = "Enter Try...";
            socket = new TcpClient(host, port);
            myText.text = "1";
            stream = socket.GetStream();
            myText.text = "2";
            writer = new StreamWriter(stream);
            myText.text = "3";
            reader = new StreamReader(stream);
            myText.text = "4";
            socketReady = true;
            myText.text = "Connected!";
            Send("{ \"type\": \"video\", \"value\": \"2.mp4\"}");
            //myText.text = "5";
        } catch {}
    }

    private void Update() {
        if(socketReady) {
            if(stream.DataAvailable) {
                string data = reader.ReadLine();
                if(data != null)
                    OnIncomingData(data);
            }
        }
    }

    private void OnIncomingData(string data) {
        print("Server: " + data);
    }

    private void Send(string data) {
        myText.text = "Will send";
        if (socketReady) {
            myText.text = "Socket Ready";
            writer.WriteLine(data);
            myText.text = "5";
            writer.Flush();
            myText.text = "6";
            myText.text = "Sent";
        }
    }
}