NetCoreServer
NetCoreServer copied to clipboard
SSLchat steems to be stuck in a disconnect reconnect loop without error or exception...
Here's my class...
`public class ChatClient : SslClient { public ChatClient(SslContext context, string address, int port) : base(context, address, port) {
}
public void DisconnectAndStop()
{
_stop = false;
DisconnectAsync();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
UIupdate.UpdateNetworkOutput("chat", $"Chat SSL client connected a new session with Id {Id}", Color.Aquamarine);
}
protected override void OnHandshaked()
{
UIupdate.UpdateNetworkOutput("chat", $"Chat SSL client handshaked a new session with Id {Id}", Color.Aquamarine);
}
protected override void OnDisconnected()
{
UIupdate.UpdateNetworkOutput("chat", $"Chat SSL client disconnected a session with Id {Id}", Color.Aquamarine);
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string encb = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
UIupdate.UpdateNetworkOutput("chat", $"{encb}", Color.Aquamarine);
}
protected override void OnError(SocketError error)
{
UIupdate.UpdateNetworkOutput("chat", $"Chat SSL client caught an error with code {error}", Color.Aquamarine);
}
private bool _stop;
}
public class OMOSSLCC
{
public IPAddress ipAdd = IPAddress.Parse(Settings.chatAddress);
string address;
int port;
X509Certificate2 cCert;
public SslContext context;
public ChatClient client;
public OMOSSLCC()
{
// SSL server address
address = ipAdd.ToString();
// SSL server port
port = Settings.chatPort;
cCert = new X509Certificate2(@"identity\certs\c.pfx", "qwerty", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(cCert);
// Create and prepare a new SSL client context
context = new SslContext(SslProtocols.Tls12, cCert, (sender, certificate, chain, sslPolicyErrors) => true);
// Create a new SSL chat client
client = new ChatClient(context, address, port);
}
public void Init()
{
// Connect the client
UIupdate.UpdateNetworkOutput("chat", $"Client connecting on port: {port}", Color.Aquamarine);
try
{
client.ConnectAsync();
UIupdate.UpdateNetworkOutput("chat", "Client Connected!", Color.Aquamarine);
}
catch (Exception ex)
{
UIupdate.UpdateNetworkOutput("chat", ex.Message, Color.Red);
}
}
// Disconnect the client
public void Disconnect()
{
UIupdate.UpdateNetworkOutput("chat", "Client disconnecting...", Color.Aquamarine);
client.DisconnectAsync();
UIupdate.UpdateNetworkOutput("chat", "Done!", Color.Aquamarine);
}
public void Send(string l)
{
// Send the entered text to the chat server
client.SendAsync(l);
}
}`
I never get the OnConnected() report to my UI. The client seems to connect, but then it's disconnected and sticks in a loop of trying to reconnect, but it just displays the OnDisconnect() UI update.
Is there a way to see what's going on? The exception isn't firing so I'm lost at this point.
Any help pointing me to the right direction would be appreciated.
TIA
I'm still looking into why, but it seems like I can only use the localhost IP 127.0.0.1.
When using an external IP for both server and client I get no errors, but the connection fails with a socket exception.
I'll keep investigating if it's a security issue or if it's just me not assigning the IP in the correct datatype or something. Seems I would get a datatype error if that were the case though.
I have the same problem, is there any way to solve it?