NetCoreServer
                                
                                 NetCoreServer copied to clipboard
                                
                                    NetCoreServer copied to clipboard
                            
                            
                            
                        [TCP Server] Session Doesnot call OnDisconnected() when client turn off internet
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?
We need more information.
Can you please post your code (the relevant disconnect part)?
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().
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.
Did you fixed it?
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
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.
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.
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.
"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.
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;
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()
I'm using ver 6.7.0
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 believe in order for these settings to be applied they have to be
0 or more. It seems like-1is 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)
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.