BrandonPotter / SimpleTCP

Straightforward .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).
Apache License 2.0
365 stars 108 forks source link

GetConnectedClientsCount keeps slowly increasing forever #22

Open fabs-rodriguez opened 7 years ago

fabs-rodriguez commented 7 years ago

I am using SimpleTcp for a GPRS server application. Basically, we have about 200 GPS trackers installed in vehicles throughout the country, and they transmit their coordinates to my SimpleTcp app via GPRS (i.e. via the cellular network) every 10 - 60 seconds for most vehicles (and every hour for some). Everything is working nicely, however, everytime I call "GetConnectedClientsCount", the count keeps increasing. I.e. it starts off on around 200 (so one connection for each vehicle) but over time it increases - after a day it's at about 10,000! Is this normal? Is SimpleTCP opening a new connection every time it receives data from a vehicle? I don't think so, otherwise the ConnectedClientsCount would increase much quicker. But for some reson, it's creating about 10 new connections every minute. Any ideas?

BrandonPotter commented 7 years ago

SimpleTcp server listens for any new connections on the port you specify. I'm guessing your tracker workflow is something like:

  1. Time to report position, wake up and establish new TCP connection to server.
  2. Transmit GPS coords.
  3. Go back to sleep.

And most likely, in step 3, the TCP connection is not being torn down in a way that SimpleTcp's socket can recognize, so at the network level, your server thinks it's still connected, just with no data / idle.

One thing you might try is to transmit something back to the connected clients every so often. Could be something as simple as a period (.) or something. That will likely cause the connections to be torn down when they can't be delivered.

Let me know if you've tried that already.

fabs-rodriguez commented 7 years ago

Thanks for the quick reply Brandon! Very good suggestion - I will implement it and let you know how it goes...

elitefuture commented 6 years ago

I have tried to do as you said if (server.IsStarted) server.Broadcast(new byte[] { 95 }); then ignoring the byte 95 if the byte array is 1, but the counter still goes higher

elitefuture commented 6 years ago

I have figured out that instead of broadcasting data, you should instead just set a timeout for each client, and let the client reconnect when needed

fabs-rodriguez commented 6 years ago

Hi elitefuture. If I may ask, how did you set the timeout for each client? Thanks...

elitefuture commented 6 years ago

server.ClientConnected += serverJoin; //when the server is made along with DataRecieved

    private void serverJoin(object sender, TcpClient e)
    {
                //Your server join code goes here if you have any
                e.Client.ReceiveTimeout = 100;//I believe this is with ticks(1000 ticks per second), and make sure you're up to date
    }
fabs-rodriguez commented 6 years ago

Thank you!!