- Home /
Best way to create a UDP network for Android
I recently asked a question regarding threading on Android (Google Daydream specific) which wasn't performing as expected. I was using a thread for communication to transfer information between an embedded system, and once the communication failed (i.e. the embedded system turned off, or any other 'can't transmit/receive' reason) the thread would abort and would attempt to reconnect to the embedded system until a connection is made again.
This works perfectly running in Unity (5.6) on Windows, but once ported to Android the thread seems to just hang once the network goes down and won't acknowledge that the thread has died. The Update() function continues to loop, but it's not until I leave the scene that the thread dies and the code continues to perform as expected (see my other post here).
I'm trying a new idea now where I'm assuming it's not the threading code which is problematic, or Android's inability to handle threads (as I've had no indication from anyone that what I'm doing can't be done on Android) but perhaps the handling of networks on Android or my UDP code.
I've got my UDP code below and I wanted to see if anyone saw anything which immediately looks wrong:
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
public class UDPConn : MonoBehaviour {
Socket sock;
IPEndPoint destEnd;
IPEndPoint endPoint;
EndPoint sender;
// Bool for connection status
public bool socketReady = false;
// Receive timeout (ms).
int rxTimeout = 1000;
byte[] msg;
// Try to initiate connection.
public bool setupSocket(int conPort)
{
try
{
String hostName = "";
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
hostName = ip.ToString();
}
}
System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(hostName);
endPoint = new IPEndPoint(ipaddress, conPort);
System.Net.IPAddress remoteIP = System.Net.IPAddress.Parse("192.168.20.2");
destEnd = new IPEndPoint(remoteIP, conPort);
sender = (EndPoint)destEnd;
sock = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
sock.Bind(endPoint);
sock.ReceiveTimeout = rxTimeout;
// Send message to 'wake' other side.
sock.SendTo(System.Text.Encoding.ASCII.GetBytes("Connect"), sender);
// Attempt to read from the socket. This will timeout if the other side isn't active.
string readData = readSocket();
if (readData != null) {
socketReady = true;
return true;
}
else
{
socketReady = false;
return false;
}
}
catch (Exception e)
{
Debug.Log("Socket error: " + e);
return false;
}
}
// Send message to server.
public void writeSocket(string theLine)
{
if (!socketReady)
{
return;
}
sock.SendTo(System.Text.Encoding.ASCII.GetBytes(theLine), sender);
}
// Read message from server.
public string readSocket()
{
try
{
msg = new Byte[256];
sock.ReceiveFrom(msg, ref sender);
String returnData = System.Text.Encoding.ASCII.GetString(msg);
return returnData;
}
catch (Exception e)
{
Debug.Log("Socket error: " + e);
return null;
}
}
// Disconnect from the socket.
public void closeSocket()
{
if (!socketReady)
{
return;
}
Debug.Log("UDPConn:closeSocket");
sock.Close();
socketReady = false;
}
}
Other than my code, I also save the Logcat at the point of where I disconnected the network from the Android phone (by swiping down from the screen, disabling the WiFi). My other idea outside of the UDP code is that perhaps I need to catch some sort of network error from Android and handle it better. The first item in Logcat is the last Debug.Log message I sent within Unity, and the first cnss-daemon message is the point at which I disconnected the network. Is there anything in there that can help identify my issue?
Thanks very much for any help. Sorry for the long post.