Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by w88dy · Jul 01, 2017 at 02:33 PM · androidunity 5networkingthreadsudp

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.

networkissue-logcat.txt (8.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Communicating between two android apps via USB OTG (no wireless or internet) 0 Answers

[Networking Related] Should I use Unity as a client for mini-games, or should I use a web-based solution? 0 Answers

Android build is not receiving UDP broadcasts 4 Answers

Dynamic sending and loading objects. 0 Answers

[Beginner] /C#/UDP/LocalHost/ receiving data the IPEndPoint.Port is different 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges