Search code examples
c#unity-game-enginetcpclienttcplistenerandroid-unity-plugin

Establishing a TCP connection between Android and Desktop


I'm trying to establish a socket connection between my Mobile device and Desktop. Mobile Device(Android) will act as a server while the desktop is client machine.

Following is my code for server,

public class PhoneCamera : MonoBehaviour 
{
    private TcpListener listner;
    private const int port = 8010;
    private bool stop = false;

    private List<TcpClient> clients = new List<TcpClient>();

    public void Start ()
    {
       Application.runInBackground = true;
       initAndWaitForWebCamTexture();
    }

    void initAndWaitForWebCamTexture()
    {
        listner = new TcpListener(IPAddress.Any, port);

        listner.Start();
        //Start sending coroutine
        StartCoroutine(senderCOR());
    }

    WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();

    IEnumerator senderCOR()
    {
        bool isConnected = false;
        TcpClient client = null;
        NetworkStream stream = null;

        // Wait for client to connect in another Thread 
        Loom.RunAsync(() =>
        {
            while (!stop)
            {
                // Wait for client connection
                client = listner.AcceptTcpClient();
                // We are connected
                clients.Add(client);

                isConnected = true;
                stream = client.GetStream();
            }
        });

        //Wait until client has connected
        while (!isConnected)
        {
            yield return null;
        }

        LOG("Connected!");
    }

    void LOG(string messsage)
    {
        Debug.Log(messsage);
    }

    private void Update () 
    {
    }
}

Following is the code for client

public class Receiver : MonoBehaviour
{
    public bool enableLog = false;
    const int port = 8010;
    public string IP = "192.168.122.24";
    TcpClient client;

    private bool stop = false;

    //This must be the-same with SEND_COUNT on the server
    const int SEND_RECEIVE_COUNT = 15;

    // Use this for initialization
    void Start()
    {
        Application.runInBackground = true;

        tex = new Texture2D(0, 0);
        client = new TcpClient();

        //Connect to server from another Thread
        Loom.RunAsync(() =>
        {
            LOGWARNING("Connecting to server...");
            // if on desktop
            client.Connect(IPAddress.Loopback, port);
            // if using the IPAD
            //client.Connect(IPAddress.Parse(IP), port);
            Debug.Log("Connected");

        });
    }

    void Update()
    {
    }

    void OnApplicationQuit()
    {
        LOGWARNING("OnApplicationQuit");
        stop = true;

        if (client != null)
        {
            client.Close();
        }
    }
}

But for some reason, the client is not being to connect to the server running on Android. How can I be able to sort this out?


Solution

  • On TcpClient.Connect(IPEndPoint), you should specify the ip of the device you want to connect to.

    client.Connect(IPAddress.Loopback, port); 
    

    Should be commented out, while

    //client.Connect(IPAddress.Parse(IP), port);
    

    Is IMHO the right code line.

    I currently cannot test your code, and my reputation is too low to post this as a comment...sorry.