Comments (4)
No problem, yes I ended up writing this, feels a bit messy, but works
private void ConnectToServer(bool reconnect = false)
{
//Dispose of old connection if reconnecting
if (reconnect)
client.Dispose();
_log.AddInfo($"Connecting to {serverAddr}:{serverPort}");
client = new TcpClient(serverAddr, serverPort, false, null, null);
client.Connected += ServerConnected;
client.Disconnected += ServerDisconnected;
client.DataReceived += MessageReceived;
try
{
client.Connect();
}
catch (Exception)
{
WriteToChat("Can not connect to server, retrying in 5 seconds");
if(!reconnect) new Thread(TryReconnect).Start();
}
}
private void ServerDisconnected(object sender, EventArgs e)
{
WriteToChat("Server disconnected, Retrying in 5 seconds.");
new Thread(TryReconnect).Start();
}
private void TryReconnect()
{
while (!client.IsConnected)
{
Thread.Sleep(5000);
ConnectToServer(true);
}
}
from supersimpletcp.
Hi @OpNop try reinstantiating the entire client object inside your TryReconnect method ie client = new TcpClient(... and attaching your callbacks in there. Once itโs disconnected the TcpClient object has to be reinstantiated.
Iโll be back at my computer tomorrow night and can provide more detail. Sorry Iโm mobile right now (which explains the less than thorough answer!).
from supersimpletcp.
Hi @OpNop did this help solve the issue for you? Sorry I was out of town. Cheers, Joel
from supersimpletcp.
Glad it works. I'd love to build reconnect logic into the objects themselves but it isn't as clean as I would like. Perhaps in a future version. Cheers!
from supersimpletcp.
Related Issues (20)
- Keep alive values HOT 2
- keepalive is not working on win7 HOT 1
- keepalive option HOT 4
- Message Framing Question HOT 1
- Send(); fails on Windows 8/8.1 HOT 2
- SimpleTcpServer DataReceived method spawns a new task for each DataReceived event HOT 3
- receive data not fast after connect HOT 1
- Server.IsListening wrong behavior when MaxConnections reached HOT 1
- Client connection is lost with no obvious reason HOT 10
- how to include dll file in exe file to be a one exe file ?? HOT 1
- NoDelay option for listener HOT 1
- KeepAliveSettings for server are applied in seconds on connecting client instead of ms HOT 1
- An active client with keep alive that does not process incoming message leads memory buildup in the server HOT 1
- What metadata is being embeded when the data is send in chunks? HOT 1
- SimpleTcpClient.Events.Disconnected is not triggered when SimpleTcpServer.Close() is invoked. HOT 1
- Block Unwanted Participants HOT 1
- multiple IP address HOT 2
- Keep Alive in linux container HOT 1
- The server sends messages to all clients on a regular basis HOT 2
- The client connecting to the Server on Maui is immediately kicked out. HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from supersimpletcp.