'TCP IP Connection Problem with Unity from Android Phone to PC
I have created a simple application with Unity that contains a button and an InputField, this application uses TCP Connection, sends a string that is written in the inputbox to the C# console application that runs as the Server.
Everything works perfectly when I use both applications on the same PC, but when I install the apk file on my phone it does not work. Both PC and Phone are connected to the same router, and I also turned the firewall off. This is not my first time using Unity or tcp connection on a C# application but it is my first time using them together. Is there anyone had the same issue? And how did you solve it? I appreciate any help. Thanks to everyone from now!
the application looks like this App image
Client Script on Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System;
public class Client : MonoBehaviour
{
private TcpClient tcpClient;
public async Task Initialize(string ip, int port)
{
try
{
tcpClient = new TcpClient();
await tcpClient.ConnectAsync(ip, port);
}
catch { }
}
public void Disconnect()
{
if (tcpClient.Connected)
tcpClient.Close();
}
public bool Connected()
{
if (tcpClient != null)
if (tcpClient.Connected)
return true;
return false;
}
public async Task Read()
{
var buffer = new byte[4096];
var ns = tcpClient.GetStream();
MemoryStream ms = new MemoryStream();
while (true)
{
var bytesRead = await ns.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead <= 0) break; // Stream was closed
ms.Write(buffer, 0, bytesRead);
ms.Seek(0, SeekOrigin.Begin);
}
Debug.Log("connection closed");
}
public void BeginSend(string msg)
{
try
{
var bytes = Encoding.ASCII.GetBytes(msg + "\n");
var ns = tcpClient.GetStream();
ns.BeginWrite(bytes, 0, bytes.Length, EndSend, bytes);
}
catch { }
}
public void EndSend(IAsyncResult result)
{
var bytes = (byte[])result.AsyncState;
}
}
TCP Server C# Console Application -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System;
public class Client : MonoBehaviour
{
private TcpClient tcpClient;
public async Task Initialize(string ip, int port)
{
try
{
tcpClient = new TcpClient();
await tcpClient.ConnectAsync(ip, port);
}
catch { }
}
public void Disconnect()
{
if (tcpClient.Connected)
tcpClient.Close();
}
public bool Connected()
{
if (tcpClient != null)
if (tcpClient.Connected)
return true;
return false;
}
public async Task Read()
{
var buffer = new byte[4096];
var ns = tcpClient.GetStream();
MemoryStream ms = new MemoryStream();
while (true)
{
var bytesRead = await ns.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead <= 0) break; // Stream was closed
ms.Write(buffer, 0, bytesRead);
ms.Seek(0, SeekOrigin.Begin);
}
Debug.Log("connection closed");
}
public void BeginSend(string msg)
{
try
{
var bytes = Encoding.ASCII.GetBytes(msg + "\n");
var ns = tcpClient.GetStream();
ns.BeginWrite(bytes, 0, bytes.Length, EndSend, bytes);
}
catch { }
}
public void EndSend(IAsyncResult result)
{
var bytes = (byte[])result.AsyncState;
}
}
And the UIController script that controls UI and TCP Connection
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
public class UIControl : MonoBehaviour
{
//objects
public Text textDeneme;
private static byte[] MessageToByteArray(string message, Encoding encoding)
{
var byteCount = encoding.GetByteCount(message);
if (byteCount > byte.MaxValue)
throw new ArgumentException("Message size is greater than 255 bytes in the provided encoding");
var byteArray = new byte[byteCount + 1];
byteArray[0] = (byte)byteCount;
encoding.GetBytes(message, 0, message.Length, byteArray, 1);
return byteArray;
}
public void connectBtn()
{
string message = textDeneme.text;
var byteArray = MessageToByteArray(message, Encoding.ASCII);
using (var tcpClient = new TcpClient())
{
tcpClient.Connect("192.168.1.133", 5000);
using (var networkStream = tcpClient.GetStream())
using (var bufferedStream = new BufferedStream(networkStream))
{
bufferedStream.Write(byteArray, 0, byteArray.Length);
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
