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 /
  • Help Room /
avatar image
0
Question by Suppart · May 11, 2017 at 02:12 PM · androidnetworkingmobile

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;
     }
 }



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

195 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

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


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