[TCP] Connection fails to TCP server opened on Android device #33
-
Hi!
Currect implementation works very well when it's running in Unity Editor, but on Android device sokect is unavaliable though I'm getting OnOpen callback. Am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 20 replies
-
How can you know the socket is "unavaliable" on mobile device? |
Beta Was this translation helpful? Give feedback.
-
Can you send me the log or more data about the error? |
Beta Was this translation helpful? Give feedback.
-
Write this example on your Unity project.
using System;
using Netly;
using Netly.Core;
using UnityEngine;
public class ConnectionExample : MonoBehaviour
{
static void Log(string message)
{
Debug.Log(message);
return;
// or
// LogUtils.Log(message);
// or
// Whatever
throw new NotImplementedException(nameof(Log));
}
internal TcpClient m_client = new TcpClient(false);
internal TcpServer m_server = new TcpServer(false);
internal Host m_host = new Host("0.0.0.0", PORT);
internal const int PORT = 12000;
internal int IDs = 0;
private void Start()
{
Log("Starting Server...");
m_server.OnClose(() => Log("[SERVER] Disconnected."));
m_server.OnError((e) => Log($"[SERVER] Error on connect: {e}"));
m_server.OnOpen(() =>
{
Log("[SERVER] Connected.");
StartClient();
});
m_server.OnEnter(client =>
{
IDs++;
int id = IDs;
Log($"[SERVER] Client connected ({client.Host}) ID: {id}.");
client.OnClose(() =>
{
Log($"[SERVER] Client disconnected ({client.Host} ID: {id})");
});
client.OnData((data) =>
{
Log($"[SERVER] Client received ({client.Host}) ID: {id}) DATA: {NE.GetString(data, NE.Mode.UTF8)}");
// ECHO effect.
client.ToData(data);
});
});
m_server.Open(m_host);
void StartClient()
{
// host listen on "0.0.0.0", internal client connect on "127.0.0.1"
var host = new Host("127.0.0.1", m_host.Port);
Log("Starting Client...");
m_client.OnOpen(() =>
{
Log($"[CLIENT] Connected. From ({m_client.Host})");
m_client.ToData(NE.GetBytes("Hello Server", NE.Mode.UTF8));
});
m_client.OnClose(() =>
{
Log($"[CLIENT] Disconnected. From ({m_client.Host})");
});
m_client.OnError((e) => Log($"[CLIENT] Error on connection: {e}"));
m_client.OnData((data) => Log($"[CLIENT] Data: {NE.GetString(data, NE.Mode.UTF8)}"));
m_client.Open(host);
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
Try put on
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Well, I started experimenting to find out if the problem was on the client or the server. By downloading the TCP debug application on GooglePlay, you can see that my client connected to the server created by the debuger app, but the debuger client did not connect to my server. Then I went and tried to create the server using TcpListener and it worked, as ( How to correct?You can correct it if you need to use the development version, just use it.
// get default backlog value.
const int defaultBacklog = (int)System.Net.Sockets.SocketOptionName.MaxConnections;
// set backlog value on <Tcp.Server.Open> as param.
TcpServer.Open(<Netly.Core.Host> host, backlog: defaultBacklog ); |
Beta Was this translation helpful? Give feedback.
-
Hi, glad it will get fixed in the next version! (and thanks for this incredible library!) Just adding a bit of information on this issue, in my app using NET8 on Android, the issue only appeared in devices running Android 13 and up (gladly, with the backlog fix everything is fine now!). On Android 12 and lower, weirdly, the code can run unaltered. |
Beta Was this translation helpful? Give feedback.
💯 Fixed
Well, I started experimenting to find out if the problem was on the client or the server. By downloading the TCP debug application on GooglePlay, you can see that my client connected to the server created by the debuger app, but the debuger client did not connect to my server.
Then I went and tried to create the server using TcpListener and it worked, as
Netly
andTcpListener
useSockets
as a base, so I opened the source code ofTcpListener
and started to see the Possible modification and I entered the problem (Backlog
).(
Backlog
) Netly used the value (0
) until it was corrected (db1eda4) and on Android (backlog=zero
) is (zero
) connections, and on Windows, value (zero
) is the same …