473,740 Members | 6,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Async socket & active connections

Hi,

In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.

Mar 25 '07 #1
11 8610
On 25 Mar, 21:48, "atlaste" <atla...@gmail. comwrote:
Hi,

In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.
Just a guess, can you try it with keepalive set to 0?

Something like where s is a Socket:

s.SetSocketOpti on(System.Net.S ockets.SocketOp tionLevel.IP,
System.Net.Sock ets.SocketOptio nName.KeepAlive , 0);

Mar 26 '07 #2
Thanks for your reaction.

It is my understanding from TCP/IP keepalive have a different meaning
(see http://msdn.microsoft.com/library/de...et_options.asp
and http://msdn2.microsoft.com/en-us/lib...ptionname.aspx
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)

So while it actually sound like a good idea to set it to true -
doesn't really solve my problem I'm afraid.

Perhaps someone has another suggestion?

Cheers,
Stefan de Bruijn.
On Mar 26, 11:44 am, "DeveloperX " <nntp...@operam ail.comwrote:
On 25 Mar, 21:48, "atlaste" <atla...@gmail. comwrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.

Just a guess, can you try it with keepalive set to 0?

Something like where s is a Socket:

s.SetSocketOpti on(System.Net.S ockets.SocketOp tionLevel.IP,
System.Net.Sock ets.SocketOptio nName.KeepAlive , 0);

Mar 26 '07 #3
On 26 Mar, 12:43, "atlaste" <atla...@gmail. comwrote:
Thanks for your reaction.

It is my understanding from TCP/IP keepalive have a different meaning
(seehttp://msdn.microsoft. com/library/default.asp?url =/library/en-us/wins...
andhttp://msdn2.microsoft .com/en-us/library/system.net.sock ets.socketopt.. .
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)

So while it actually sound like a good idea to set it to true -
doesn't really solve my problem I'm afraid.

Perhaps someone has another suggestion?

Cheers,
Stefan de Bruijn.

On Mar 26, 11:44 am, "DeveloperX " <nntp...@operam ail.comwrote:
On 25 Mar, 21:48, "atlaste" <atla...@gmail. comwrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.
Just a guess, can you try it with keepalive set to 0?
Something like where s is a Socket:
s.SetSocketOpti on(System.Net.S ockets.SocketOp tionLevel.IP,
System.Net.Sock ets.SocketOptio nName.KeepAlive , 0);- Hide quoted text -

- Show quoted text -
Ah yes I see, how about changing the Linger option?

Basically the suggestion comes because of something I read about
Apache changing a method to close connections too become Linger_Close
and it caused lots of FIN_WAIT_2 issues for people.

Again, just an idea :)

Mar 26 '07 #4
Yes, that was what I expected as well. As soon as a connection is
established I do a:

socket.LingerSt ate.Enabled = false

I even set SocketOptionNam e.DontLinger to true just to be sure :) No
luck.

The disconnection procedure is currently as follows:

- dontlinger -true
- lingerstate.ena bled = false;
- socket.Shutdown (both);
- socket.begindis connect(some callback)
and in the callback:
- socket.enddisco nnect()
- socket.close(1) ;

I set the close timeout to 1 second because I want to avoid some fancy
rule that makes 0 an exception resulting in endless lingering and so
on.

So far no luck... Oh and I still get FIN_WAIT_2 states. (which
shouldn't be happening afaik)

Perhaps it helps if I provide a little more code about the connection
procedure:

---
private Socket socket;
private bool Connected = false;

public void Connect(IPAddre ss ip, int port)
{
socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
socket.Blocking = false;

try { socket.Connect( ip, port); }
catch (SocketExceptio n) { } // non-blocking
}

public bool IsConnected()
{
// NOTE: because of a bug in the .NET framework, we can't
use the "isconnecte d"
// property.

if (Connected == false && socket != null)
{
if (socket.Poll(0, SelectMode.Sele ctWrite))
{
socket.LingerSt ate.Enabled = false;
Connected = true;
}
}
return Connected;
}
---

Thanks,

Stefan.

On Mar 26, 1:51 pm, "DeveloperX " <nntp...@operam ail.comwrote:
On 26 Mar, 12:43, "atlaste" <atla...@gmail. comwrote:
Thanks for your reaction.
It is my understanding from TCP/IP keepalive have a different meaning
(seehttp://msdn.microsoft. com/library/default.asp?url =/library/en-us/wins...
andhttp://msdn2.microsoft .com/en-us/library/system.net.sock ets.socketopt.. .
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)
So while it actually sound like a good idea to set it to true -
doesn't really solve my problem I'm afraid.
Perhaps someone has another suggestion?
Cheers,
Stefan de Bruijn.
On Mar 26, 11:44 am, "DeveloperX " <nntp...@operam ail.comwrote:
On 25 Mar, 21:48, "atlaste" <atla...@gmail. comwrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.
Just a guess, can you try it with keepalive set to 0?
Something like where s is a Socket:
s.SetSocketOpti on(System.Net.S ockets.SocketOp tionLevel.IP,
System.Net.Sock ets.SocketOptio nName.KeepAlive , 0);- Hide quoted text -
- Show quoted text -

Ah yes I see, how about changing the Linger option?

Basically the suggestion comes because of something I read about
Apache changing a method to close connections too become Linger_Close
and it caused lots of FIN_WAIT_2 issues for people.

Again, just an idea :)

Mar 26 '07 #5
The major problem I'm currently facing is that my complete internet
connection just dies :) I've ruled out every option where the modem(s)
or ISP is involved - so that leaves me with the software itself.

I've attempted to disassemble the socket class with Reflector in a
last resort. There I found that the m_IsConnected property is used in
various methods - among others "SetToDisconnec ted" and
"UpdateStatusAf terSocketError" . However, I cannot find a direct cause
of my problems. (the members that don't get updated because of the
IsConnected bug are m_RightEndPoint , m_IsConnected and
m_IsDisconnecte d).

Can someone please confirm that this is the source of all this pain
and misery?

I've posted this message in other discussion groups as well, because
the problem doesn't solely concern C# but .NET in general.

Greetings,
Stefan.

On Mar 26, 5:26 pm, "atlaste" <atla...@gmail. comwrote:
Yes, that was what I expected as well. As soon as a connection is
established I do a:

socket.LingerSt ate.Enabled = false

I even set SocketOptionNam e.DontLinger to true just to be sure :) No
luck.

The disconnection procedure is currently as follows:

- dontlinger -true
- lingerstate.ena bled = false;
- socket.Shutdown (both);
- socket.begindis connect(some callback)
and in the callback:
- socket.enddisco nnect()
- socket.close(1) ;

I set the close timeout to 1 second because I want to avoid some fancy
rule that makes 0 an exception resulting in endless lingering and so
on.

So far no luck... Oh and I still get FIN_WAIT_2 states. (which
shouldn't be happening afaik)

Perhaps it helps if I provide a little more code about the connection
procedure:

---
private Socket socket;
private bool Connected = false;

public void Connect(IPAddre ss ip, int port)
{
socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
socket.Blocking = false;

try { socket.Connect( ip, port); }
catch (SocketExceptio n) { } // non-blocking
}

public bool IsConnected()
{
// NOTE: because of a bug in the .NET framework, we can't
use the "isconnecte d"
// property.

if (Connected == false && socket != null)
{
if (socket.Poll(0, SelectMode.Sele ctWrite))
{
socket.LingerSt ate.Enabled = false;
Connected = true;
}
}
return Connected;
}
---

Thanks,

Stefan.

On Mar 26, 1:51 pm, "DeveloperX " <nntp...@operam ail.comwrote:
On 26 Mar, 12:43, "atlaste" <atla...@gmail. comwrote:
Thanks for your reaction.
It is my understanding from TCP/IP keepalive have a different meaning
(seehttp://msdn.microsoft. com/library/default.asp?url =/library/en-us/wins...
andhttp://msdn2.microsoft .com/en-us/library/system.net.sock ets.socketopt.. .
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)
So while it actually sound like a good idea to set it to true -
doesn't really solve my problem I'm afraid.
Perhaps someone has another suggestion?
Cheers,
Stefan de Bruijn.
On Mar 26, 11:44 am, "DeveloperX " <nntp...@operam ail.comwrote:
On 25 Mar, 21:48, "atlaste" <atla...@gmail. comwrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.
Just a guess, can you try it with keepalive set to 0?
Something like where s is a Socket:
s.SetSocketOpti on(System.Net.S ockets.SocketOp tionLevel.IP,
System.Net.Sock ets.SocketOptio nName.KeepAlive , 0);- Hide quoted text -
- Show quoted text -
Ah yes I see, how about changing the Linger option?
Basically the suggestion comes because of something I read about
Apache changing a method to close connections too become Linger_Close
and it caused lots of FIN_WAIT_2 issues for people.
Again, just an idea :)

Mar 27 '07 #6
Stefan,
I don't understand why the effort to (in some ways) "reinvent the wheel",
but I've written what I believe are very efficient webcrawlers using the
built-in asynchronous methods without any of the nasty side effects you
describe. Timeouts can be added if necessary.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"atlaste" wrote:
Hi,

In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.

Mar 28 '07 #7
Well actually I have tried the async methods of webrequest, libcurl,
libwww and some other solutions. I am well aware of the capabilities
of those implementations .

Perhaps not so needless to say is that I don't merely concentrate my
efforts on http/web, but also on other protocols (which aren't so well
supported). I chose the example of a webcrawler because it is in my
opinion a good example of the amount of distribution and connections
that I'm looking for. Furthermore I'd like to compare the different
methods to evaluate them and simply pick the best method for my
purposes.

Reinventing the wheel or not and discussing if I'm doing that or not
isn't really what I would like to debate. The fact remains that the
whole network traffic just shuts down with my tcp wrapper class, which
just shouldn't happen in any case.

Thanks,

Stefan.

On Mar 28, 3:13 am, Peter Bromberg [C# MVP]
<pbromb...@yaho o.yabbadabbadoo .comwrote:
Stefan,
I don't understand why the effort to (in some ways) "reinvent the wheel",
but I've written what I believe are very efficient webcrawlers using the
built-in asynchronous methods without any of the nasty side effects you
describe. Timeouts can be added if necessary.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net

"atlaste" wrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.

Mar 28 '07 #8
On 28 Mar, 12:58, "atlaste" <atla...@gmail. comwrote:
Well actually I have tried the async methods of webrequest, libcurl,
libwww and some other solutions. I am well aware of the capabilities
of those implementations .

Perhaps not so needless to say is that I don't merely concentrate my
efforts on http/web, but also on other protocols (which aren't so well
supported). I chose the example of a webcrawler because it is in my
opinion a good example of the amount of distribution and connections
that I'm looking for. Furthermore I'd like to compare the different
methods to evaluate them and simply pick the best method for my
purposes.

Reinventing the wheel or not and discussing if I'm doing that or not
isn't really what I would like to debate. The fact remains that the
whole network traffic just shuts down with my tcp wrapper class, which
just shouldn't happen in any case.

Thanks,

Stefan.

On Mar 28, 3:13 am, Peter Bromberg [C# MVP]

<pbromb...@yaho o.yabbadabbadoo .comwrote:
Stefan,
I don't understand why the effort to (in some ways) "reinvent the wheel",
but I've written what I believe are very efficient webcrawlers using the
built-in asynchronous methods without any of the nasty side effects you
describe. Timeouts can be added if necessary.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"atlaste" wrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.- Hide quoted text -

- Show quoted text -
I couldn't reproduce it, but I can only do it at home as I've only got
access to 1.1 in the office and the Socket implementation is
different. Can you reproduce it in a tiny bit of code, ie just create
a socket, set the options, connect to a URL retrieve a bit of data and
close it? That's what I tried last night. I even tried things like not
closing the socket and so forth and couldn't get a FIN_WAIT_2.
Interesting problem though, networking in general is fascinating imo.

Mar 30 '07 #9
Lazy as I am, I haven't created a minimal testcase. Okay, that's not
really true... I just don't know how to create one, since I'm doing
terrible things with the sockets api, that I don't really know how to
reproduce in a proper context (read: without getting angry faces).

I did attempt to use async methods of socket, like Peter suggested.
Furthermore I keep track of TCP connect and disconnect operations in a
performance counter just to make sure no resource is
"leaking" (although that shouldn't be a problem in a GC'ed language
you could but shouldn't think...).

Funny enough the approach did help me somewhat. That said, I have to
elaborate on that:

I nowadays rely on the begin/end methods of connect and send. For the
disconnect operation I use the following sequence:

socket.LingerSt ate = new LingerOption(tr ue, 0);
socket.Shutdown (SocketShutdown .Both);
socket.Close(0) ;

See http://msdn2.microsoft.com/en-us/lib...ngerstate.aspx
for an explanation of the lingerstate I set during the way.

The usual behaviour is that *any* TCP socket operation on the pc just
fries. So it looks like the resources are slowly drained. The program
actually seems to work for a random amount of time; which supports the
theory, since not every host needs the same processing. I went back to
my log files and tried to find a pattern that matches the behaviour of
the program. It seems that the behaviour is not really random (!), but
rather magically has something to do with a factor (of connected/
disconnected) close to 61000.

.... why does that remind me of 2^16, which is usually the number of
file descriptors?

I currently try setting the socket back to blocking mode before
setting the lingerstate because I want to avoid that setting to affect
the socket behaviour (although it shouldn't). I should have the
results in about .. 4 hours or so... I hope this will end my long
journey of creating a "simple" TCP/IP client.

Cheers,
Stefan de Bruijn.

On 30 mrt, 11:38, "DeveloperX " <nntp...@operam ail.comwrote:
On 28 Mar, 12:58, "atlaste" <atla...@gmail. comwrote:
Well actually I have tried the async methods of webrequest, libcurl,
libwww and some other solutions. I am well aware of the capabilities
of those implementations .
Perhaps not so needless to say is that I don't merely concentrate my
efforts on http/web, but also on other protocols (which aren't so well
supported). I chose the example of a webcrawler because it is in my
opinion a good example of the amount of distribution and connections
that I'm looking for. Furthermore I'd like to compare the different
methods to evaluate them and simply pick the best method for my
purposes.
Reinventing the wheel or not and discussing if I'm doing that or not
isn't really what I would like to debate. The fact remains that the
whole network traffic just shuts down with my tcp wrapper class, which
just shouldn't happen in any case.
Thanks,
Stefan.
On Mar 28, 3:13 am, Peter Bromberg [C# MVP]
<pbromb...@yaho o.yabbadabbadoo .comwrote:
Stefan,
I don't understand why the effort to (in some ways) "reinvent the wheel",
but I've written what I believe are very efficient webcrawlers using the
built-in asynchronous methods without any of the nasty side effects you
describe. Timeouts can be added if necessary.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"atlaste" wrote:
Hi,
In an attempt to create a full-blown webcrawler I've found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnecte d" variable correctly (SocketExceptio n is thrown: non-
blocking has this effect...) - but doesn't appear to be a problem
because poll, read and write work fine.
For measuring the performance of the crawler, I started "perfmon.ms c"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...
My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn't support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT ", "FIN_WAIT_2 " or another
closing state.
After a host has completed, I try to disconnect the TCP/IP connection.
I've attempted combinations of "shutdown(both) ", (async) "disconnect "
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...
Perhaps someone knows a solution to this problem?
Greetings,
Stefan de Bruijn.- Hide quoted text -
- Show quoted text -

I couldn't reproduce it, but I can only do it at home as I've only got
access to 1.1 in the office and the Socket implementation is
different. Can you reproduce it in a tiny bit of code, ie just create
a socket, set the options, connect to a URL retrieve a bit of data and
close it? That's what I tried last night. I even tried things like not
closing the socket and so forth and couldn't get a FIN_WAIT_2.
Interesting problem though, networking in general is fascinating imo.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Apr 2 '07 #10

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

Similar topics

1
2157
by: Morgan Leppink | last post by:
Hi all - We have been developing a complex TCP socket communication app that is responsible for keeping numerous connections open to clients on routable IPs. The app receives request on a non-routable listener socket that marshals communications between the non-routable netowrk and the clients out on the routable network. The app runs as a .NET service in Win2K Server. Async
1
6578
by: Mark Smith | last post by:
Hi , Is it possible in .Net to define a async timer callback. What I am wanting to do is declare a time-out condition that gets executed should the something timeout. The reason I want to do this is to handle host being down that I am trying to connect to using TCPIP. Then after a timeout automatically try another host and just continue trying hosts until someone accepts a connection, varying my timeout as I loop.
0
1181
by: Macca | last post by:
Hi, My app has an asynchronous socket server to receive and transmit data over ethernet connections coming in through a switch. My App will handle concurrent data for possibly 20+ embedded C devices. My app will reside on a PC that is also connected to an office network. What I have found is that using the Socket server app examples from the net, they usually listen for all connections to the PC that the app is residing on.
6
3823
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to cause these to block? 2) You can connect together a binaryreader to a networkstream:
7
2861
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
5
4768
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a set of sensor data per second to a socket server. The socket server will do two things: 1. write data into a file via bsddb; 2. forward the data to a GUI written in wxpython. I am thinking the code should work as follow (not sure it is feasible)...
15
8408
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast (asynchronously). Then it repackages these UDP packets and sends them to a subscriber via TCP. Now, I can read the UDP stream all day long without the application
1
20639
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
10
8059
by: ColoradoGeiger | last post by:
I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing this and it's gone very well so far. But coming from the blocking world, I want to have some timeouts on my receive routines and I after reading a lot on MSDN and here, I get the impression that once I set one of these Begin... methods a running...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8794
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9272
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9211
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8218
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3286
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 we have to send another system
3
2195
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.