Problem with a Socket server Opening/Accepting Many sockets and the GC running.

From: Phillip (spam_at_vwars.com)
Date: 03/26/04


Date: 26 Mar 2004 12:11:11 -0800

Hello,
I have checked all the groups and messages and cannot find this
situation:

I have created a synchrounous and Asynchronous servers that are
listening on port 11000 and ready to take a new connection. (this is
test code for a DNS handler)

I created the Listener socket, Listen.bind and Listen.listen(10) is
called, I loop and block on the Listen.accept() waiting for a new
connection to be started by my clients.
>From the Listen.Accept, I get the new socket Handler and reacieve a
data, looking for an end marker then echo the data back to the client.
I then do handle.Shutdown(both) and Handle.close and go back to the
top of the loop waiting for the next accept to unblock.

After 3956 succesful connections, my clients get a 11048 error, for 70
seconds while the GC is running.
THen all is well and my clients can connect for about another 3900
connections.

I have read all the GC stuff, and socket stuff. Tried to ensure
nothing is still un-fread or locked up and I have tried forcing the GC
to run.
Nothing works. I tried ReuseAddress and Linger(true) and
Linger(false).
Nothing Helps.

My clients can't wait 70 seconds for the next connect if it that
client happens to be #3957.

I have included most of the code below.
THanks in advance.
Phillip O.

public static int StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
int retval = 0;
StringBuilder data = new StringBuilder();
// Creates CompareInfo for the InvariantCulture.
System.Globalization.CompareInfo myComp =
CultureInfo.InvariantCulture.CompareInfo;
int connectioncount = 0;
        // Establish the local endpoint for the socket.
        // Dns.GetHostName returns the name of the
        // host running the application.
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

// Create a TCP/IP socket. the Listern socket.
using(Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp ))
{
        data.Capacity = 100000; // a really big buffer!!!
        // Bind the sockets to the local endpoint and
        // listen for incoming connections.
        try
        {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                // Start listening for connections.
                while (connectioncount < 500)
                {
                Console.WriteLine("Waiting for a connection...");
                // Program is suspended while waiting for an incoming connection.
                using(Socket handler = listener.Accept())
                {
                        data.Length = 0; // reset the data.
                        // An incoming connection needs to be processed.
                        while (true)
                        {
                                int bytesRec = handler.Receive(bytes);
                                data.Append(Encoding.ASCII.GetString(bytes,0,bytesRec));
                                if (myComp.IndexOf (data.ToString(), "<EOF>") > -1)
                                { break; }
                        }
                // Show the data on the console.
                Console.WriteLine( "Text received : {0}", data.ToString());
                // Echo the data back to the client.
                        try
                        {
                        byte[] msg = Encoding.ASCII.GetBytes(data.ToString());
                        handler.Send(msg);
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                        }
                        catch (Exception e)
                        {
                        Console.WriteLine(e.ToString());
                        }
                } // end using handler.
                connectioncount++;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.GetTotalMemory(true);
                } // end while
        try
        {
        retval = 1; // Ending this set on the socket.
        // hopefully the gc will run now
        listener.Close();
        }
        catch (Exception e)
        {
        Console.WriteLine(e.ToString());
        }
} // end try.
catch (Exception e)
{
Console.WriteLine(e.ToString());
retval = 2; // really having a problem.
}
Console.WriteLine("\nPort {0} CLosed", localEndPoint.Port);
} // end user listener.
GC.Collect();
GC.WaitForPendingFinalizers();
GC.GetTotalMemory(true);
return(retval);
}



Relevant Pages

  • Winsock control and threads
    ... Basicaly I want a listen socket to listen for incomming connection, ... The listener is one object exposed by the componenet. ... it creates an instance of the ActiveSocket object ...
    (microsoft.public.win32.programmer.networks)
  • Re: Network connections use
    ... >Is it possible to find out which listener (exe, service, ISAPI, activeX, ... or program did established connection? ... >Is there any relation between program and open socket (for all opened ... And Chris, posting your email address openly will get you more unwanted email, ...
    (microsoft.public.windowsxp.network_web)
  • Problem with a Socket server program opening/accepting many connections and the GC is running.
    ... listening on port 11000 and ready to take a new connection. ... I created the Listener socket, ... connection to be started by my clients. ... I have read all the GC stuff, and socket stuff. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: good name to dispatch events (naming convention)
    ... clients, sends them over to some broker, and then dispatches results/ ... instance of the Connection class is instantiated on the server for each ... a generic handler: ... read off socket. ...
    (comp.object)
  • Re: Handling multiple connections
    ... each time select says the socket has more data. ... All data being sent by clients are prepended with a header that indicates ... received and thus delays receiving on the other readable sockets. ... When the client closes the connection, ...
    (microsoft.public.win32.programmer.networks)

Quantcast