Re: Async socket & active connections

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.LingerState = new LingerOption(true, 0);
socket.Shutdown(SocketShutdown.Both);
socket.Close(0);

See http://msdn2.microsoft.com/en-us/library/system.net.sockets.socket.lingerstate.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...@xxxxxxxxxxxxx> wrote:
On 28 Mar, 12:58, "atlaste" <atla...@xxxxxxxxx> wrote:

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...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
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
"isconnected" variable correctly (SocketException 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.msc"
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 -


.



Relevant Pages

  • Re: Async socket & active connections
    ... of those implementations. ... writing a wrapper around the Socket class in an attempt to make it ... support the async mechanism - rather than the 'begin' and 'end' ... 300K connections, enough to start worrying... ...
    (microsoft.public.dotnet.languages.csharp)
  • Asynchronous Socket Server data
    ... The socket server knows what type of data it expects due to the interface ... I can have 1 databuffer only for each datatype to handle multiple connections? ... int bytesRead = handler.EndReceive; ... packetIndex, bytesRead); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ping =?ISO-8859-1?Q?kr=E1ft=E9=E9_=3F_Advice_needed_?= =?ISO-8859-1?Q?to_trace_w
    ... He has two telephone extensions leading from the master socket and his ... presume that there is a fault in his extension wiring. ... If it's twisted pair ensure that it's not been connected split pair, in other words the blue white with white blue, orange white with white orange, if you get my drift, & the connections you should use are 2&5 ...
    (uk.telecom.broadband)
  • Re: !EventConnect Problem
    ... the June roll-up is available somewhere, although there's not been the usual ... The socket is not in a listening state. ... The incoming connection queue has no room for connections. ...
    (microsoft.public.windowsce.app.development)
  • Re: !EventConnect Problem
    ... the June roll-up is available somewhere, ... The socket is not in a listening state. ... The incoming connection queue has no room for connections. ...
    (microsoft.public.windowsce.app.development)