Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:57:59+00:00 2026-05-17T22:57:59+00:00

I have a c# socket based server which serves TCP clients. I use telnet

  • 0

I have a c# socket based server which serves TCP clients.
I use telnet based monitoring to see if the server is listening.
to versify clients are up i use server keep alive (sending a random string data), and if socket expection raises-i remove the client from a client’s dictionary.
the probelm is: the telnet connections to the socket doesn’t get removed from the list of connections and the number of clients is rising up slowly but surely.
i tried a telnet manually to the server through command line and than closing the telnet connection-nothing.the server just keep sending the keep alive to the telnet connection with no execptions.
this is my read call back:

 protected void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;
        // Retrieve the state object and the handler socket
        // from the async state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.socket;
        if (state.socket == null)
            return;
        if (!state.socket.Connected)
            return;

        int id = state.id;
        try
        {
            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                string newAddedStr = string.Empty;
                newAddedStr = Encoding.UTF8.GetString(state.buffer, 0, bytesRead);
                //cut the new message and add it 
                if (state.sb == null)
                    state.sb = new StringBuilder();
                    state.sb.Append(newAddedStr);
                // There might be more data, so store the data received so far.
                //add data until end of XML
                content = state.sb.ToString();
                //IF GOT FULL MESSAGE FROM SOCKET
                if ((content.Length > 0) /*&& (content.IndexOf("\0") > -1)*/)
                {
                    String strh = String.Format("Client # {0} data: ", id);
                    strh += content.Replace("\0", "");
                    if (!strh.Contains("keepalive"))
                        LogWriter.Trace(strh, "");
                    // l(writeToGetTextBoxMsg), new object[] { strh });
                    if (state != null)
                    {
                        if (state.sb == null)
                            state.sb = new StringBuilder();
                        state.sb.Length = 0;
                    }
                    //add the rest of the xml


                    string objData = content.Replace("\0", "");
                    string xmlData = objData.ToString();
                    try
                    {


                        if (xmlData.Contains("Unsubscribe"))
                        {
                            RemoveSubscriber(xmlData);
                        }
                        else
                        {
                            if (xmlData.Contains("Subscribe"))
                            {
                                if (!state.IsInitiated)
                                {
                                    state.Init();
                                    state.socketClient.OnSocketError += new SocketErrorHandler(socketClient_OnSocketError);
                                    state.socketClient.clientSocket = handler;
                                }
                                AddSubscriber(xmlData, state);
                            }

                        }
                        xmlData = null;
                        objData = null;
                        content = null;

                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        LogWriter.Trace(newAddedStr,"ArgumentOutOfRangeException in ReadCallback");
                    }
                    catch (Exception ex)
                    {
                        LogWriter.TraceError(ex.Message + " " + ex.StackTrace + " " + newAddedStr);
                    }
                    #region oldCode

                    #endregion
                }
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(this.ReadCallback), state);
            }
        }
        catch (System.Net.Sockets.SocketException es)
        {
            closeSocket(state, false, "Execption:" + es.Message + "," + es.StackTrace);
            if (es.ErrorCode != 64)
            {
                LogWriter.Trace(string.Format("Socket Exception: {0}, {1}.", es.ErrorCode, es.ToString()), "");
            }
        }
        catch (Exception e)
        {
            closeSocket(state, false,"Execption:"+e.Message+","+e.StackTrace);
            if (e.GetType().FullName != "System.ObjectDisposedException")
            {
                Console.WriteLine("Exception: " + e.StackTrace);
                LogWriter.Trace("Exception Message: " + e.ToString() + e.StackTrace, "");
                Console.WriteLine("Exception Message: " + e.ToString());
                LogWriter.Trace("ReadCallback:" + e.Message + " " + e.StackTrace, "ERROR");
            }
        }
    }

any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:57:59+00:00Added an answer on May 17, 2026 at 10:57 pm

    When you do a read of a socket and 0 is returned you know that the other side of the connection has closed. Are you doing that?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that encapsulates tcp socket communications with a server. For each
I have a worker thread that is listening to a TCP socket for incoming
I have a server that sends data via a socket, the data is a
I have a server that listens for a connection on a socket: public class
I have a class which I'm serialising to send over a unix socket and
I have some experience making multiplayer turn-based games using sockets, but I've never attempted
I have a Php application using stream_socket_client(), to get data through tcp from a
I have a c# application that runs as a windows service controlling socket connections
We have a simple project where we read data from a socket and we
You have multiple network adapters. Bind a UDP socket to an local port, without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.