Android App crashes on " server.Start() " HELP
Hi everyone! I'm creating a server android application for communicate with my arduino , with the code below it work just fine ( on pc testing ) but when run it on my phone and the code meet the line server.Start();
the application just crushes with no exception.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class SimpleServer : MonoBehaviour {
public string data = "nm";
string response;
public Text txt;
string txtCnt;
Thread mThread;
public int port = 5893;
public string ipServer = "192.168.1.15";
TcpClient client;
StreamWriter streamW;
StreamReader streamR;
TcpListener server = null;
bool isConnected = false;
public GyroManager cm;
void Start () {
}
void Update () {
txt.text = txtCnt;
}
public void SetupServer()
{
writeConsolle("Begin...");
try
{
IPAddress localAddr = IPAddress.Parse(ipServer);
server = new TcpListener(port);
writeConsolle("Starting : "+port);
server.Start();
data = "nm";
// Client get connected with the server
client = server.AcceptTcpClient();
isConnected = true;
streamW = new StreamWriter(client.GetStream());
streamR = new StreamReader(client.GetStream());
writeConsolle("connected..");
sendMessage("welcome");
while(isConnected){
data = cm.getData();
sendMessage(data);
writeConsolle(data);
if(!isConnected)
break;
Thread.Sleep(150);
}
}
catch (SocketException e)
{
writeConsolle("SocketException" + e.ToString ());
}
finally
{
// Stop listening for new clients.
server.Stop();
client.Close ();
streamW.Close();
streamR.Close();
}
}
IEnumerator waitTime(int millis){
yield return new WaitForSeconds(millis);
}
public void writeConsolle(string x){
txtCnt += x+"\r\n";
}
public void sendMessage(string x){
streamW.Write (x+"\n");
streamW.Flush();
}
public void turnOffServer(){
if (isConnected) {
try {
isConnected = false;
writeConsolle ("Server - Stopped");
} catch (SocketException e) {
writeConsolle ("Exception" + e.ToString ());
}
}
}
public void StartServer(){
StartCoroutine (startThread ());
}
public IEnumerator startThread(){
// Starting the thread.
writeConsolle ("Starting the thread....");
yield return new WaitForSeconds (3);
mThread = new Thread (SetupServer);
writeConsolle ("Starting the server....");
yield return new WaitForSeconds (3);
mThread.Start();
}
}
Any thought of why this happens ? Maybe the tcpListener is incompatible with android ? Any permission conflict ? Thanks in advance.
Comment