- Home /
TCP data sending with Android
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 = "192.168.2.25";
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);
}
Answer by supernat · Jan 13, 2015 at 02:51 AM
http://unity3d.com/unity/licenses
If you don't have Android Pro, you won't get .NET sockets.
I bought the "Good ol'Sockets" from the asset store https://www.assetstore.unity3d.com/en/#!/content/13166 which allows me to use System.Net.Sockets in free mode, but it still doesn't work, have you used or heard of this ? @supernat
I wasn't aware of that one, but I figured there were assets on the store. You need to follow the guide they provide though, here: http://cdn.lostpolygon.com/unity-assets/good-ol-sockets/files/Good%20Ol%27%20Sockets%20-%20User%20$$anonymous$$anual.pdf Did you use the LostPolygon namespace, and it's just not in the code above?
I just figured it out! Was careless on my part, just had to go into "Player Settings" and change "Internet Access" to Required and "Api compatibility level" to .NET 2.0 and it works from my phone now @supernat
Answer by Mabulhuda · Sep 18, 2015 at 08:17 PM
did you give the permission to use INTERNET in the manifest ?
<uses-permission android:name="android.permission.INTERNET" />
Answer by magonicolas · Oct 22, 2017 at 09:45 PM
Hi, Im trying to connect to a socket from my client, but is not working, it never connect, someone know what might be happening?
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;n
try {
print("Enter Try");
myText.text = "Enter Try...";
socket = new TcpClient(host, port);
stream = socket.GetStream();
writer = new StreamWriter(stream);
reader = new StreamReader(stream);
socketReady = true;
myText.text = "Connected!";
Send("{ \"type\": \"video\", \"value\": \"2.mp4\"}");
} 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) {
writer.WriteLine(data);
writer.Flush();
myText.text = "Sent";
}
}
}