Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

TCP data sending problem

Discussion in 'Multiplayer' started by philthegiant, Jan 12, 2015.

  1. philthegiant

    philthegiant

    Joined:
    Jan 6, 2015
    Posts:
    5
    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:

    Code (CSharp):
    1.     public Texture btnTexture;
    2.     public GUIStyle style;
    3.     TcpClient mySocket = new TcpClient();
    4.     public NetworkStream theStream;
    5.     public String Host = "my_ip_address";
    6.     public Int32 Port = 8888;
    7.     public bool sent = false;
    8.  
    9.     void OnGUI()
    10.     {
    11.         if (GUI.Button (new Rect ((Screen.width / 2) - Screen.width/4, (Screen.height / 4), Screen.width/2, Screen.width/2), btnTexture))
    12.         {
    13.             if(!sent)
    14.             {
    15.                 mySocket.Connect(Host, Port);
    16.                 theStream = mySocket.GetStream();
    17.                 sent = true;
    18.             }
    19.             byte[] sendStream = System.Text.Encoding.ASCII.GetBytes("TCP message sent from Phil's Phone" + " $");
    20.             theStream.Write(sendStream, 0, sendStream.Length);
    21.             theStream.Flush();
    22.             //Application.LoadLevel ("Page1");
    23.         }
    24.  
    25.     }
     
    Last edited: Jan 12, 2015
  2. Brent_Farris

    Brent_Farris

    Joined:
    Jul 13, 2012
    Posts:
    881
    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".
     
  3. unityuser1324235141

    unityuser1324235141

    Joined:
    Apr 5, 2013
    Posts:
    44
    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.
     
  4. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. using System.Collections.Generic;
    7. using System.Text.RegularExpressions;
    8. using System.Runtime.Serialization.Formatters.Binary;
    9. using System;
    10. using System.IO;
    11. using Newtonsoft.Json;
    12. using System.Net;
    13. using System.Net.Sockets;
    14. using UnityEngine.Networking;
    15.  
    16.  
    17.  
    18. public class SockMan : MonoBehaviour {
    19.  
    20.     public Text myText;
    21.  
    22.     private bool socketReady;
    23.     private TcpClient socket;
    24.     private NetworkStream stream;
    25.     private StreamWriter writer;
    26.     private StreamReader reader;
    27.  
    28.     void Start() {
    29.         print("Start");
    30.         ConnectToServer();
    31.     }
    32.  
    33.     public void ConnectToServer() {
    34.         print("Setup");
    35.         myText.text = "Setup...";
    36.         if(socketReady)
    37.             return;
    38.         String host = "192.168.0.15";
    39.         Int32 port = 8080;
    40.  
    41.         try {
    42.             print("Enter Try");
    43.             myText.text = "Enter Try...";
    44.             socket = new TcpClient(host, port);
    45.             myText.text = "1";
    46.             stream = socket.GetStream();
    47.             myText.text = "2";
    48.             writer = new StreamWriter(stream);
    49.             myText.text = "3";
    50.             reader = new StreamReader(stream);
    51.             myText.text = "4";
    52.             socketReady = true;
    53.             myText.text = "Connected!";
    54.             Send("{ \"type\": \"video\", \"value\": \"2.mp4\"}");
    55.             //myText.text = "5";
    56.         } catch {}
    57.     }
    58.  
    59.     private void Update() {
    60.         if(socketReady) {
    61.             if(stream.DataAvailable) {
    62.                 string data = reader.ReadLine();
    63.                 if(data != null)
    64.                     OnIncomingData(data);
    65.             }
    66.         }
    67.     }
    68.  
    69.     private void OnIncomingData(string data) {
    70.         print("Server: " + data);
    71.     }
    72.  
    73.     private void Send(string data) {
    74.         myText.text = "Will send";
    75.         if (socketReady) {
    76.             myText.text = "Socket Ready";
    77.             writer.WriteLine(data);
    78.             myText.text = "5";
    79.             writer.Flush();
    80.             myText.text = "6";
    81.             myText.text = "Sent";
    82.         }
    83.     }
    84. }
    85.