-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TcpServer call OnConnected after object disposed #115
Comments
Fixed in 5.0.8 please try! |
I try 5.0.8 and 5.0.9, the OnDisconnected still call before OnConnected. also, still got The Server will execute I use PHP socket to call the TCP server. <?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, "127.0.0.1", 12345);
print "#socket_connect#";
$cmd = "##TEST##";
$len = strlen($cmd);
$offset = 0;
while ($offset < $len)
{
$sent = socket_write($socket, substr($cmd, $offset), $len - $offset);
if ($sent === false)
break; //Error
$offset += $sent;
}
if ($offset < $len)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
}
socket_close($socket);
print "#socket_close#";
?> |
Update protected override void OnConnected()
{
SendAsync("##TEST##");
Disconnect();
} will have same result |
Reproduced. I'll look into it! |
Could you please try version 5.0.11? |
try both 5.0.11 and 12, still happens The exception is on the Server Side in my case, and not case by You may discover another issue in this case. We may expect, the flow of function call on the Server Side should be Thanks and sorry if I make you misunderstand. |
TrySend() has call Socket.SendAsync() so it's all ok with async. Task.Factory.StartNew() was an old workaround that put unnecessary thread racing. On a Server Side I don't understand how you managed to step OnDisconnected() --> OnConnected(). OnConnected() should not be called for the Session after any disconnect. A new session should be created for a new connection... |
I don't know and may be my code issue, you may check as below: I will able to get (able >90% some time)
with the code as below using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using NetCoreServer;
namespace TcpChatTest
{
class Program
{
public static bool _error = false;
class ChatClient : NetCoreServer.TcpClient
{
public ChatClient(string address, int port) : base(address, port) { }
protected override void OnConnected()
{
SendAsync("##TEST##");
Disconnect();
}
}
public class ChatServer : NetCoreServer.TcpServer
{
public ChatServer(IPAddress address, int port) : base(address, port) { }
protected override TcpSession CreateSession() { return new ChatSession(this); }
}
public class ChatSession : NetCoreServer.TcpSession
{
public ChatSession(TcpServer server) : base(server) { }
protected override void OnConnected()
{
try
{
_ = Socket.RemoteEndPoint.ToString();
}
catch(Exception e)
{
Console.WriteLine($"{Id} error :\n\r{e}");
Program._error = true;
}
Console.WriteLine($"{Id} connected! ");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
Console.WriteLine($"{Id} {Socket.RemoteEndPoint} call [{message}] ");
}
protected override void OnDisconnected()
{
Console.WriteLine($"{Id} disconnected!");
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Session caught an error: {error}");
}
}
static void Main(string[] args)
{
var port = 12345;
Console.WriteLine("Server starting...");
var server = new ChatServer(IPAddress.Any, port);
server.Start();
Thread.Sleep(1000);
for (int i = 0; i < 1000; i++)
{
if (_error) break;
Console.WriteLine($"Client starting... {i}");
_ = new ChatClient("127.0.0.1", port).ConnectAsync();
}
Console.WriteLine($"error statis {_error}");
}
}
} You will able to see "d051e1b5-9a09-4700-a609-a3bd7945aca7 " is calling OnReceived -> OnDisconnected -> OnConnected. Maybe the multithreading with Console.WriteLine will make me misunderstanding the real situation? |
Thanks for samples! I'll look into it. |
Reproduced and fixed in 5.0.13 |
Very fast response. Thanks for your work! |
It's the right behavior. Because the connection establishing (OnConnecting() handler called), but suddenly disconnected by peer, therefore you'll receive OnDisconnected() then instead of OnConnected(). |
Thanks for your explanation~ |
In some case (if the connect disconnect immediately after connect), the OnConnected will call after OnDisconnected, and the OnConnected function will not able to access the Sockets object due to it already disposed.
I can add a addition checking on this case. May I know it is a bug or any better solution?
Thanks
The text was updated successfully, but these errors were encountered: