We help IT Professionals succeed at work.
Get Started

Closing a TCP Asynch socket C#

Last Modified: 2015-03-01
I have a TCP socket application based on examples of asynch socket programming available on Microsoft site and an example on Code Project.  
My client is a GPRS based device which does not generally do a clean close on the TCP connection.   I need to remove these idle connections on my TCP server.  Am maintaining a hashtable in the server that keeps a record of the unique unitID sent by the client and the associated socket.  When the client reconnects -I need to course through the hashtable and close the socket related with the previous client ID and remove the entry in the hashtable and add this new entry with the current socket.  
My question is how to cleanly and surely remove a socket and how to manage the hashtable while doing so

My code is as follows

private bool removePrevSocketIfPresent(Hashtable hashTableUnits, long unitID)
            bool allWEll = false;
            SocketAsyncEventArgs tempSocketToremove;
            DataHoldingUserToken tempTokenToRemove;
           //locking the hashtable for the whole operation
            lock (TRInitModule.hashTableUnits.SyncRoot)
                bool markForRemoval = false;
                foreach (DictionaryEntry entry in TRInitModule.hashTableUnits)
                    Int64 currentUnitID = Convert.ToInt64(entry.Key);
                    if (currentUnitID == unitID)
                            tempSocketToremove = (SocketAsyncEventArgs)(entry.Value);
                            tempTokenToRemove = (DataHoldingUserToken)tempSocketToremove.UserToken;
                            tempSocketToremove.SocketError = SocketError.OperationAborted;

                            //is the above line enough or do I need to do the below lines too?
                           // tempSocketToremove.AcceptSocket.Close();
                           // tempTokenToRemove.Reset();
                            //if (tempTokenToRemove.theDataHolder.dataMessageReceived != null)
                            //    tempTokenToRemove.CreateNewDataHolder();
                         markForRemoval = true;
                                Console.WriteLine("One socket removed, count is = " + TRInitModule.hashTableUnits.Keys.Count);
                            allWEll = true;
                        catch (Exception eRT)
                            allWEll = false;
                            Console.WriteLine("ERROR IN  REMOVE ITEM  " + eRT.Message);

                        markForRemoval = false;
                if (markForRemoval)
            return allWEll;

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE