开发者

telnet connection to a c# socket doesn't close

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜