473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4522
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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...

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.