Scalable SSL socket server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zoho
    New Member
    • Mar 2012
    • 11

    Scalable SSL socket server

    HI,

    I have developed C# socket server using SSLStream . I followed MSDN examples on developing it. I am having few issues
    1.The memory keep on increasing on more number of clients connected.
    2.It is not accepting new clients after sometime, may be after few days, eventhough already connected clients are sending and receiving data.
    When I do netstat -a , I see tcplistener is listening at port but when I do telenet [server] [port] , returns with could not make connection to specified port.But restarting the server fixes the issue and accepts new clients as well.

    Thanks


    Any ideas?

    Thanks
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    It would help us if you were to show the code you're having issues with, and a better description of the issue(s) you're having.

    NOTE: Please use code tags when posting your code :)

    Comment

    • zoho
      New Member
      • Mar 2012
      • 11

      #3
      Here is the code to accept the connections

      Code:
      public void Start(int Port) 
          { 
              IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Port); 
              // create the listener which listens for incoming connections 
              listener = new TcpListener(localEndPoint); 
              listener.Start();  
              //Accept connections asynchronously 
              listener.BeginAcceptSocket(acceptCallBack, listener); 
       
          } 
       
          public void Stop() 
          { 
              try 
              { 
                  if (listener!=null)  
                      listener.Stop(); 
                  listener = null; 
              } 
              catch { } 
          } 
       
          public void OnAccept(IAsyncResult result) 
          { 
              TcpListener listener = null; 
              SslStream sslStream = null; 
              try 
              { 
                  listener = result.AsyncState as TcpListener; 
                  client = listener.EndAcceptSocket(result); 
                  listener.BeginAcceptSocket(acceptCallBack, listener); 
                  log.DebugFormat("client cert required:{0},CheckCertifcateRevocation:{1} ", clientCertificateRequired,ConfigurationManager.AppSettings["CheckCertifcateRevocation"]); 
                  if (clientCertificateRequired) 
                  { 
                      sslStream = new SslStream(new NetworkStream(client, false), false, remoteCertValidation); 
                  } 
                  else 
                  { 
                      sslStream = new SslStream(new NetworkStream(client, false), false); 
                  } 
                  sslStream.BeginAuthenticateAsServer(serverCertificate, clientCertificateRequired, SslProtocols.Default, Convert.ToBoolean(ConfigurationManager.AppSettings["CheckCertifcateRevocation"]), authenticationCallBack, sslStream); 
              } 
              catch (Exception ex) 
              { 
                  if (sslStream != null) 
                  { 
                      sslStream.Dispose(); 
                      sslStream = null; 
       
                  } 
                  log.ErrorFormat("After accepting client {0} connection :{1}",client.RemoteEndPoint.ToString(), ex.Message); 
                  return; 
              } 
          } 
       
          public void OnAuthenticate(IAsyncResult result) 
          { 
       
       
              SslStream sslStream = null; 
              StateObject state = null; 
              try 
              { 
                  sslStream = result.AsyncState as SslStream; 
                  sslStream.EndAuthenticateAsServer(result); 
       
                  maxNumberAcceptedClients.WaitOne(); 
                  log.InfoFormat("SSL authenticated, Encrypted: {0}, Signed: {1}", sslStream.IsEncrypted, sslStream.IsSigned); 
       
                  Interlocked.Increment(ref numConnectedSockets); 
                  PerformanceCounters.SSLConnections.RawValue = numConnectedSockets; 
                  PerformanceCounters.SSLTotalConnections.Increment(); 
       
                  string IP = "?.?.?.?"; 
       
                  try 
                  { 
                      IP = client.RemoteEndPoint.ToString(); 
                  } 
       
                  catch { } 
                  log.InfoFormat("[{0}]\topened SSL client: #{1} connected", IP, numConnectedSockets); 
                  state = new StateObject(); 
                  state.workSocket = client; 
                  state.workSslStream = sslStream; 
                  ConnectionBase connection = (ConnectionBase)Activator.CreateInstance(ClientType, this, state); 
       
              } 
              catch (Exception ex) 
              { 
       
                  if (sslStream != null) 
                  { 
                      log.ErrorFormat("authentication exception for the client: {0}",client.RemoteEndPoint.ToString()); 
                      sslStream.Dispose(); 
                      sslStream = null; 
                  } 
                  state = null; 
                  log.DebugFormat("Exception In authentication: {0}", ex); 
                  return; 
              } 
       
          } 
      
      
      Thanks

      Comment

      • zoho
        New Member
        • Mar 2012
        • 11

        #4
        Any body who knows about sockets with sslstream?

        Comment

        Working...