NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

[TCP Server] Session Doesnot call OnDisconnected() when client turn off internet

Open nobikin95 opened this issue 3 years ago • 15 comments

I am handling reconnect function of my game. I tried to turn off the internet on the client, and the server side does not trigger OnDisconnected() . Is there any way to fix that issue?

nobikin95 avatar Oct 25 '22 11:10 nobikin95

We need more information.

Can you please post your code (the relevant disconnect part)?

AmtechInnovarch avatar Nov 19 '22 22:11 AmtechInnovarch

I used TCPChatClient and Server in /NetCoreServer/examples/ folder. I deployed the server to a Cloud machine,client connected to the server and then ,after client turned off Wifi, Server Session did not call OnDisconnected().

nobikin95 avatar Nov 21 '22 06:11 nobikin95

You would need to modify the example code to detect socket disconnect exception and then trigger the OnDisconnected() event. The server session has no idea what the client's WiFi signal is doing.

I think this can be closed as it's not an issue with NetCoreServer.

Anyone, please correct me if I'm wrong here, but the socket layer is the only layer that can detect that event TMK.

AmtechInnovarch avatar Nov 22 '22 01:11 AmtechInnovarch

Did you fixed it?

701982376509132690 avatar Jan 19 '23 10:01 701982376509132690

We need more information.

Can you please post your code (the relevant disconnect part)?

The connection will not close on the server if, say, you turn on the VPN and the connection breaks

701982376509132690 avatar Jan 19 '23 10:01 701982376509132690

So, write a patch and submit a pull request @conspiracynomad. That's how this community works. We don't demand upgrades and fixes, especially without you posting YOUR code to let us see how you tried to implement.

SusuyoHashimoto avatar Jan 19 '23 19:01 SusuyoHashimoto

So, write a patch and submit a pull request @conspiracynomad. That's how this community works. We don't demand upgrades and fixes, especially without you posting YOUR code to let us see how you tried to implement.

Im using default code from example client/server, nothing has been changed, just default code. Just try to disable your internet and see if your session will close on the server. I will answer in advance, it won't be closed.

701982376509132690 avatar Jan 19 '23 19:01 701982376509132690

Hello, I don't know if it's a bug or a technical limitation. The client connects TCP with Server, then Client turns off internet, only OnDisconnected() on the client side trigger. Server-side Session OnDisconnected() didn't trigger.

nobikin95 avatar Jan 30 '23 09:01 nobikin95

"You would need to modify the example code to detect socket disconnect exception and then trigger the OnDisconnected() event. The server session has no idea what the client's WiFi signal is doing." - @AmtechInnovarch

It's been explained why this happens. Nobody will be able to make you understand if you just aren't getting it.

Sometimes we attempt things that we don't yet have the skills to attempt and we fail and try again. Keep trying and eventually you'll understand, or realize that you simply don't poses the analytical capacity to progress further. In the latter case it's dangerous to play with networking software you don't understand on systems open to the internet.

I realize you say you're using the example, but still, we like to see you post the code you're using so we know you aren't making unknown mistakes. Since you refuse to post the code you're using, we won't be able to help.

Also, this seems to be a feature request actually, not a bug.

BristleconeStudio avatar Jan 30 '23 19:01 BristleconeStudio

Set of TcpServer keep alive options should help (they are disabled by default):

public bool OptionKeepAlive { get; set; }
public int OptionTcpKeepAliveTime { get; set; } = -1;
public int OptionTcpKeepAliveInterval { get; set; } = -1;
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;

chronoxor avatar Jan 30 '23 19:01 chronoxor

Set of TcpServer keep alive options should help (they are disabled by default):

public bool OptionKeepAlive { get; set; }
public int OptionTcpKeepAliveTime { get; set; } = -1;
public int OptionTcpKeepAliveInterval { get; set; } = -1;
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;

I tried

class ChatServer : TcpServer
	{
		public ChatServer(IPAddress address, int port) : base(address, port)
		{
			OptionKeepAlive = true;
			OptionTcpKeepAliveTime = -1;
			OptionTcpKeepAliveInterval = -1;
			OptionTcpKeepAliveRetryCount = -1;
		}
		protected override TcpSession CreateSession() { return new ChatSession(this); }
		protected override void OnError(SocketError error)
		{
			Console.WriteLine($"Chat TCP server caught an error with code {error}");
		}
	}

Also on the client-side

public ChatClient(string address, int port) : base(address, port)
        {
            OptionKeepAlive = true;
            OptionTcpKeepAliveTime = -1;
            OptionTcpKeepAliveInterval = -1;
            OptionTcpKeepAliveRetryCount = -1;
        } 

But when client connected and then disable the network adapter, server-side session didn't call OnDisconnected()

nobikin95 avatar Jan 31 '23 08:01 nobikin95

I'm using ver 6.7.0

nobikin95 avatar Jan 31 '23 08:01 nobikin95

I believe in order for these settings to be applied they have to be 0 or more. It seems like -1 is the same as option disabled. Source code for reference: https://github.com/chronoxor/NetCoreServer/blob/master/source/NetCoreServer/TcpSession.cs#L103

TailyFair avatar Jan 31 '23 13:01 TailyFair

I believe in order for these settings to be applied they have to be 0 or more. It seems like -1 is the same as option disabled. Source code for reference: https://github.com/chronoxor/NetCoreServer/blob/master/source/NetCoreServer/TcpSession.cs#L103

I tried with

            OptionKeepAlive = true;
            OptionTcpKeepAliveTime = 0;
            OptionTcpKeepAliveInterval = 0;
            OptionTcpKeepAliveRetryCount = 0;

and TCP Session throws an Exception at the connection

Unhandled exception. System.Net.Sockets.SocketException (22): Invalid argument
at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean silent)

nobikin95 avatar Feb 01 '23 02:02 nobikin95

I tried with

            OptionKeepAlive = true;
            OptionTcpKeepAliveTime = 64;
            OptionTcpKeepAliveInterval = 64;
            OptionTcpKeepAliveRetryCount = 2;

Client now can connect with server. But it doesn't seem to solve my problem.

nobikin95 avatar Feb 01 '23 02:02 nobikin95