C
Chizl
I'm building a web server and having some issues with the
TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using
MS Web Stress Tool to test against my web server and when I see 200
connections at once to it, and I'm seeing 2/3 of the sockets as socket
errors.. They never get into my code, so it has to be failing on the
connection side.. Backlog is the only thing I can think could be the
problem. this.svrListener.Start(1024); should be well over enough, but
with no param passed in Start or with 4096, it seems to be exactly the same.
My code goes as follows...
-------------------------------------
//declare listener
private TcpListener svrListener;
//declare port
private Int32 m_lPort = 80; // Select any free port you wish.
Default to 80
//declare host
private String m_szHost = ""; // Select any ip you wish
//declare max kb
private Int32 m_lMaxKBPerSec = 50; // Select max KB per second. Default is
50KB.
//signal.
private static ManualResetEvent clientConnected = new
ManualResetEvent(false);
//Constructor
public CListen(string szHost, Int32 lPort, Int32 lMaxKBPerSec)
{
this.m_lPort = lPort;
this.m_szHost = szHost;
this.m_lMaxKBPerSec = lMaxKBPerSec;
}
//start listener
public void ProcessListener()
{
IPAddress inputDNS_IP;
//setup IP to listen on
if (this.m_szHost.Length == 0)
{
inputDNS_IP = IPAddress.Any;
this.m_szHost = "Any";
}
else
inputDNS_IP = Dns.GetHostEntry(this.m_szHost).AddressList[0];
//log it
try
{
//set to listen on specific IPort
this.svrListener = new TcpListener(inputDNS_IP, this.m_lPort);
//setup timeouts..
this.svrListener.Server.ReceiveTimeout = 30000;
this.svrListener.Server.SendTimeout = 30000;
//start with a backlog "TCP/IP Queue" of 1024
this.svrListener.Start(1024);
}
catch (SocketException e)
{
//log it
}
while (true)
{
// Set the event to nonsignaled state.
clientConnected.Reset();
// Accept the connection.
// BeginAcceptSocket() creates the accepted socket.
this.svrListener.BeginAcceptSocket(new
AsyncCallback(DoAcceptSocketCallback), this.svrListener);
//log it
// Wait until a connection is made and processed before
// continuing.
clientConnected.WaitOne();
//verify service isn't asking for this DLL to stop.
if (MyWebServer.StopStatus)
break;
}
//this will allow a browser to finish downloading what
//files they are downloading, while not allowing any
//new connections.
while (MyWebServer.CurrentConnection > 0)
Thread.Sleep(1000);
}
// Process the client connection.
public void DoAcceptSocketCallback(IAsyncResult ar)
{
//log client connecting
//Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
//End the operation and display the received data on the
//console.
Socket sckConnection = listener.EndAcceptSocket(ar);
//Signal the calling thread to continue, connection info received..
clientConnected.Set();
//increment counter, if fail then too many connections
if (!MyWebServer.AddConnection())
{
//log it
//send message to browser, too many connections
return;
}
//log it accepting
//set vars for Socket Class
CSocket socket = new CSocket(sckConnection, this.m_szHost, this.m_lPort,
this.m_lMaxKBPerSec);
//start the thread
Thread th = new Thread(new ThreadStart(socket.Process));
th.Start();
//log thread created
}
TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using
MS Web Stress Tool to test against my web server and when I see 200
connections at once to it, and I'm seeing 2/3 of the sockets as socket
errors.. They never get into my code, so it has to be failing on the
connection side.. Backlog is the only thing I can think could be the
problem. this.svrListener.Start(1024); should be well over enough, but
with no param passed in Start or with 4096, it seems to be exactly the same.
My code goes as follows...
-------------------------------------
//declare listener
private TcpListener svrListener;
//declare port
private Int32 m_lPort = 80; // Select any free port you wish.
Default to 80
//declare host
private String m_szHost = ""; // Select any ip you wish
//declare max kb
private Int32 m_lMaxKBPerSec = 50; // Select max KB per second. Default is
50KB.
//signal.
private static ManualResetEvent clientConnected = new
ManualResetEvent(false);
//Constructor
public CListen(string szHost, Int32 lPort, Int32 lMaxKBPerSec)
{
this.m_lPort = lPort;
this.m_szHost = szHost;
this.m_lMaxKBPerSec = lMaxKBPerSec;
}
//start listener
public void ProcessListener()
{
IPAddress inputDNS_IP;
//setup IP to listen on
if (this.m_szHost.Length == 0)
{
inputDNS_IP = IPAddress.Any;
this.m_szHost = "Any";
}
else
inputDNS_IP = Dns.GetHostEntry(this.m_szHost).AddressList[0];
//log it
try
{
//set to listen on specific IPort
this.svrListener = new TcpListener(inputDNS_IP, this.m_lPort);
//setup timeouts..
this.svrListener.Server.ReceiveTimeout = 30000;
this.svrListener.Server.SendTimeout = 30000;
//start with a backlog "TCP/IP Queue" of 1024
this.svrListener.Start(1024);
}
catch (SocketException e)
{
//log it
}
while (true)
{
// Set the event to nonsignaled state.
clientConnected.Reset();
// Accept the connection.
// BeginAcceptSocket() creates the accepted socket.
this.svrListener.BeginAcceptSocket(new
AsyncCallback(DoAcceptSocketCallback), this.svrListener);
//log it
// Wait until a connection is made and processed before
// continuing.
clientConnected.WaitOne();
//verify service isn't asking for this DLL to stop.
if (MyWebServer.StopStatus)
break;
}
//this will allow a browser to finish downloading what
//files they are downloading, while not allowing any
//new connections.
while (MyWebServer.CurrentConnection > 0)
Thread.Sleep(1000);
}
// Process the client connection.
public void DoAcceptSocketCallback(IAsyncResult ar)
{
//log client connecting
//Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
//End the operation and display the received data on the
//console.
Socket sckConnection = listener.EndAcceptSocket(ar);
//Signal the calling thread to continue, connection info received..
clientConnected.Set();
//increment counter, if fail then too many connections
if (!MyWebServer.AddConnection())
{
//log it
//send message to browser, too many connections
return;
}
//log it accepting
//set vars for Socket Class
CSocket socket = new CSocket(sckConnection, this.m_szHost, this.m_lPort,
this.m_lMaxKBPerSec);
//start the thread
Thread th = new Thread(new ThreadStart(socket.Process));
th.Start();
//log thread created
}