473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous Sockets and High Async IO Thread Count

Hi there,

Hoping someone could advise me here pls; I'm creating a C# class that
implements a telnet client socket allowing a VB . NET application to
communicate with telnet servers. After leaving the app running for just 6
hrs, the thread count exploded to close to 1000, before the app finally stops
responding. The handles probably hit close to 10000.

Tracing my code, I isolated the leak to when I execute a telnet command.
Everytime I run a telnet command, a thread and several handles are spawned.
After the the command completes, the thread will not die. As I'm unable to
call a EndConnect due to the nature of telnet protocol, could this be giving
me this problem? I tried destroying the telnet client's parent object with a
"Dispose" and "Finalize" and even calling GC, but it still doesn't work out.
The codes attached; hope you can help, thanks!

-------------------------------------------------------------------
public bool Connect()
{
try
{
//connectDone is a ManualResetEvent
connectDone.Reset();

//setup keep-alive heart beats
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,
10);

//attempt an async connection
s.BeginConnect(iep , callbackProc, s );

//wait for 12 sec until connected to host
for (int count=0; count<24; count++)
{
System.Threading.Thread.Sleep(500);
.....
}

catch(SocketException SockEx) { ... }

catch(Exception NormEx) { ... }
.....
}
private void ConnectCallback( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock1 = (Socket)ar.AsyncState;
if ( sock1.Connected )
{
connectDone.Set();
receiveDone.Reset();

// Define a new Callback to read and handle the data
AsyncCallback receiveData = new AsyncCallback( OnReceivedData )

// Begin reading data asynchronously
sock1.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None,
receiveData , sock1 );
}
}
catch( Exception ex )
{
MessageBox.Show("Setup Receive callbackProc failed!" );
}
}
// This function dispatches a telnet command to the host server
private void SendMessage(string strText)
{
try
{
Byte[] smk = new Byte[strText.Length];

for ( int i=0; i < strText.Length ; i++)
{
//doing telnet stuff ...
}

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 0);

//*** callbackProc is a new AsyncCallback(ConnectCallback) delegate
sendDone.Reset();
ar2 = s.BeginSend(smk , 0 , smk.Length , SocketFlags.None ,
callbackProc , s );

//check to ensure that async operation has completed
while (ar2 == null || ar2.IsCompleted == false)
{
System.Threading.Thread.Sleep(500);
}

s.EndSend(ar2);
sendDone.Set();
}

catch(Exception ers) { ... }
}
Nov 22 '05 #1
2 4532
Since this is kind of a "non-typical" application, you might want to look
into a custom ThreadPool where you can control more of what is happening
"under the hood".
Stephen Toub of MS has some excellent code in various recent issues of MSDN
Magazine, or you might look at Ami Bar's "SmartThreadPool" over at
codeproject.com
Peter

"brendonlam" <br********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hi there,

Hoping someone could advise me here pls; I'm creating a C# class that
implements a telnet client socket allowing a VB . NET application to
communicate with telnet servers. After leaving the app running for just 6
hrs, the thread count exploded to close to 1000, before the app finally
stops
responding. The handles probably hit close to 10000.

Tracing my code, I isolated the leak to when I execute a telnet command.
Everytime I run a telnet command, a thread and several handles are
spawned.
After the the command completes, the thread will not die. As I'm unable to
call a EndConnect due to the nature of telnet protocol, could this be
giving
me this problem? I tried destroying the telnet client's parent object with
a
"Dispose" and "Finalize" and even calling GC, but it still doesn't work
out.
The codes attached; hope you can help, thanks!

-------------------------------------------------------------------
public bool Connect()
{
try
{
//connectDone is a ManualResetEvent
connectDone.Reset();

//setup keep-alive heart beats
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.KeepAlive,
10);

//attempt an async connection
s.BeginConnect(iep , callbackProc, s );

//wait for 12 sec until connected to host
for (int count=0; count<24; count++)
{
System.Threading.Thread.Sleep(500);
.....
}

catch(SocketException SockEx) { ... }

catch(Exception NormEx) { ... }
.....
}
private void ConnectCallback( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock1 = (Socket)ar.AsyncState;
if ( sock1.Connected )
{
connectDone.Set();
receiveDone.Reset();

// Define a new Callback to read and handle the data
AsyncCallback receiveData = new AsyncCallback( OnReceivedData )

// Begin reading data asynchronously
sock1.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None,
receiveData , sock1 );
}
}
catch( Exception ex )
{
MessageBox.Show("Setup Receive callbackProc failed!" );
}
}
// This function dispatches a telnet command to the host server
private void SendMessage(string strText)
{
try
{
Byte[] smk = new Byte[strText.Length];

for ( int i=0; i < strText.Length ; i++)
{
//doing telnet stuff ...
}

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay,
0);

//*** callbackProc is a new AsyncCallback(ConnectCallback) delegate
sendDone.Reset();
ar2 = s.BeginSend(smk , 0 , smk.Length , SocketFlags.None ,
callbackProc , s );

//check to ensure that async operation has completed
while (ar2 == null || ar2.IsCompleted == false)
{
System.Threading.Thread.Sleep(500);
}

s.EndSend(ar2);
sendDone.Set();
}

catch(Exception ers) { ... }
}

Nov 22 '05 #2
Hi Peter,

Thanks for the references; checked them out, but these were on spinning off
new threads for functions. Any way to do the same when I'm dealing with
asynch socket IO? Thanks

brendon lam

"Peter Bromberg [C# MVP]" wrote:
Since this is kind of a "non-typical" application, you might want to look
into a custom ThreadPool where you can control more of what is happening
"under the hood".
Stephen Toub of MS has some excellent code in various recent issues of MSDN
Magazine, or you might look at Ami Bar's "SmartThreadPool" over at
codeproject.com
Peter

"brendonlam" <br********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
Hi there,

Hoping someone could advise me here pls; I'm creating a C# class that
implements a telnet client socket allowing a VB . NET application to
communicate with telnet servers. After leaving the app running for just 6
hrs, the thread count exploded to close to 1000, before the app finally
stops
responding. The handles probably hit close to 10000.

Tracing my code, I isolated the leak to when I execute a telnet command.
Everytime I run a telnet command, a thread and several handles are
spawned.
After the the command completes, the thread will not die. As I'm unable to
call a EndConnect due to the nature of telnet protocol, could this be
giving
me this problem? I tried destroying the telnet client's parent object with
a
"Dispose" and "Finalize" and even calling GC, but it still doesn't work
out.
The codes attached; hope you can help, thanks!

-------------------------------------------------------------------
public bool Connect()
{
try
{
//connectDone is a ManualResetEvent
connectDone.Reset();

//setup keep-alive heart beats
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.KeepAlive,
10);

//attempt an async connection
s.BeginConnect(iep , callbackProc, s );

//wait for 12 sec until connected to host
for (int count=0; count<24; count++)
{
System.Threading.Thread.Sleep(500);
.....
}

catch(SocketException SockEx) { ... }

catch(Exception NormEx) { ... }
.....
}
private void ConnectCallback( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock1 = (Socket)ar.AsyncState;
if ( sock1.Connected )
{
connectDone.Set();
receiveDone.Reset();

// Define a new Callback to read and handle the data
AsyncCallback receiveData = new AsyncCallback( OnReceivedData )

// Begin reading data asynchronously
sock1.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None,
receiveData , sock1 );
}
}
catch( Exception ex )
{
MessageBox.Show("Setup Receive callbackProc failed!" );
}
}
// This function dispatches a telnet command to the host server
private void SendMessage(string strText)
{
try
{
Byte[] smk = new Byte[strText.Length];

for ( int i=0; i < strText.Length ; i++)
{
//doing telnet stuff ...
}

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay,
0);

//*** callbackProc is a new AsyncCallback(ConnectCallback) delegate
sendDone.Reset();
ar2 = s.BeginSend(smk , 0 , smk.Length , SocketFlags.None ,
callbackProc , s );

//check to ensure that async operation has completed
while (ar2 == null || ar2.IsCompleted == false)
{
System.Threading.Thread.Sleep(500);
}

s.EndSend(ar2);
sendDone.Set();
}

catch(Exception ers) { ... }
}


Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2803
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
2
497
by: brendonlam | last post by:
Hi there, Hoping someone could advise me here pls; I'm creating a C# class that implements a telnet client socket allowing a VB . NET application to communicate with telnet servers. After...
3
2468
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new...
48
5365
by: Steve - DND | last post by:
I'm trying to determine if I need to make my application multi-threaded, or if it can be done with asynchronous programming. I realize that asynch calls do create a new thread in the background,...
4
5415
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
4
3795
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
4
2748
by: Macca | last post by:
I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application...
2
2169
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
4
3590
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
1
559
by: Navin Mishra | last post by:
Hi, I've an ASP.NET web service that consumes other web services as well as sends data to client using TCP blocking sockets on a custom thread pool threads. If I use asynchronous sockets to send...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.