Android internet connection being terminated instantly on mobile devices??
Desktop computers can send packets to my TCP server and receive packets from my TCP server PERFECTLY FINE, and Mobile devices can CONNECT to my TCP server as well, but as soon as bytes are transmitted or received to the mobile device, the connection is severed instantly. What gives?
Some background:
- I have Unity Pro
- I have tried adding the internet permission to the AndroidManifest.xml
- I have tried switching "Internet=required" in the project settings
- My firewall is off
- I have tried using Asynchronous and Synchronous TCP servers/clients provided on microsoft's official site
My client code:
public class NetworkBackend : MonoBehaviour {
public static NetworkBackend Instance;
string host_IP = "my-ip-goes-here";
int host_port = 8000;
int buffer_size = 256;
Socket sender;
net_flag NET_FLAG = net_flag.idle;
enum net_flag
{
idle,
connection_success,
}
void Awake()
{
Instance = this;
}
void Start()
{
Thread connect_thread = new Thread(Connect);
connect_thread.Start();
}
void FixedUpdate()
{
if (NET_FLAG != net_flag.idle)
{
switch(NET_FLAG)
{
case net_flag.connection_success:
CommenceSendRecieve();
break;
}
NET_FLAG = net_flag.idle;
}
}
void Connect()
{
try
{
IPHostEntry ipHostInfo = Dns.Resolve(host_IP);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, host_port);
sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
Debug.Log("Socket connected to "+sender.RemoteEndPoint.ToString());
NET_FLAG = net_flag.connection_success;
}
catch (Exception e) { }
}
catch (Exception e) { }
}
void CommenceSendRecieve()
{
Thread recieve_thread = new Thread(Recieve);
recieve_thread.Start();
}
public void SendBytes(byte[] bytes)
{
int bytesSent = sender.Send(bytes);
}
void Recieve()
{
byte[] recieve_buffer = new byte[1024];
while(true)
{
int bytesRec = sender.Receive(recieve_buffer);
Debug.Log("Got = " + Encoding.ASCII.GetString(recieve_buffer, 0, bytesRec));
}
}
// Release the socket.
//sender.Shutdown(SocketShutdown.Both);
//sender.Close();
}
My server code:
public class MainServer
{
public static string HOST = "my-ip-goes-here";
public static int PORT = 8000;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public MainServer() { }
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(HOST);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
string content = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",content.Length, content);
//Send(handler, content);
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
}
private static void Send(Socket handler, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
//handler.Shutdown(SocketShutdown.Both);
//handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartListening();
return 0;
}
}
Your answer
Follow this Question
Related Questions
How I use network for mobile 0 Answers
How to add realtime game events without updating build? 1 Answer
Photon synchronization on different devices failes 0 Answers
How Can I Get Assetbundle local path 0 Answers
Online "Quiz Game" (Networking) 0 Answers