I am writing a project on Unity, due to certain limitations it is not possible to use the built-in unit for network play. Therefore, I am writing my own.

In general, there is a server:

class Server : MonoBehaviour { void Start(){ listener = new TcpListener(8080); listener.Start(); Thread t = new Thread(new ThreadStart(Service)); t.Start(); } void Service(){ while(true){ Socket soc = listener.AcceptSocket(); Debug.Log("Connected: " + soc.RemoteEndPoint); try{ Stream s = new NetworkStream(soc); StreamReader sr = new StreamReader(s); StreamWriter sw = new StreamWriter(s); sw.AutoFlush = true; // do something s.Close(); } catch(Exception e){ Debug.Log(e.Message); } Debug.Log("Disconnected: " + soc.RemoteEndPoint); soc.Close(); } } void OnGUI () { int i = 0; foreach (System.Net.IPAddress ip in System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList) { GUI.Label (new Rect (0,i*50,100,(i+1)*50), ip.ToString()); ++i; } } } 

And there is a customer

 class Client : MonoBehaviour { void Start() { try{ client = new TcpClient("127.0.0.1", 8080); s = client.GetStream(); sr = new StreamReader(s); sw = new StreamWriter(s); sw.AutoFlush = true; } catch (Exception e){ Debug.Log(e.Message); } } } 

A common task is to connect and send data.

When I start both the server and the client on the computer, everything works - the client connects, the data is transferred (and it works with 127.0.0.1 and with any local ip from those displayed in onGUI )

When I start the server on the computer, and try to connect from the phone on the android (or vice versa), I constantly get an error during the connection from the series "did not connect, because the time has expired".

At the same time, the computer and the phone are in the same local network (I tried to connect via usb, and via usb in modem mode, and distributed Wi-Fi from the phone, connecting to it from the computer - in all cases the same thing).

Who faced, how to deal, how to connect (the task is to transfer data from device A to device B within the same local network)?

  • I'm not a fan of android and I will not say about him. But, there must be a router for connecting devices. Have you opened ports in the firewall \ firewall in Windows? Have you forwarded ports in a router (Port Forwarding)? - Align

0