Socket dont really close. Please help microsoft MVP! | | |
This is making me crazy!!
Please, if some body can help me.
I'm testing a ver simple socket client.
In my test I just open and close a connection (in a loop) to my local IIS
server (port 80)
using System.Net.Sockets;
for (int I = 0; I < 5000; I++)
{
TcpClient myClient = new TcpClient();
try
{
myClient.Connect("127.0.0.1", 80)
}
catch (Exception ex) {MessangeBox.Show(ex.ToString()); }
myClient.Close()
} //End for
Everything go fine, thousands of connections are opened and closed until
this error appear....
[ System.Net.Sockets.SocketException: Only one usage of each socket address
(protocol/network address/port) is normally permitted ]
After this error, I execute the netstat -a command and the lines (below)
appear thousands of times, lines below are just a very little part of the
complete list.
TCP asusis-dsgn:3505 localhost:80 TIME_WAIT
TCP asusis-dsgn:3506 localhost:80 TIME_WAIT
TCP asusis-dsgn:3507 localhost:89 TIME_WAIT
TCP asusis-dsgn:3508 localhost:89 TIME_WAIT
TCP asusis-dsgn:3509 localhost:89 TIME_WAIT
TCP asusis-dsgn:3510 localhost:89 TIME_WAIT
TCP asusis-dsgn:3511 localhost:89 TIME_WAIT
TCP asusis-dsgn:3512 localhost:89 TIME_WAIT
TCP asusis-dsgn:3513 localhost:89 TIME_WAIT
TCP asusis-dsgn:3514 localhost:89 TIME_WAIT
TCP asusis-dsgn:3515 localhost:89 TIME_WAIT
TCP asusis-dsgn:3516 localhost:89 TIME_WAIT
TCP asusis-dsgn:3517 localhost:89 TIME_WAIT
TCP asusis-dsgn:3518 localhost:89 TIME_WAIT
TCP asusis-dsgn:3519 localhost:89 TIME_WAIT
TCP asusis-dsgn:3520 localhost:89 TIME_WAIT
TCP asusis-dsgn:3521 localhost:89 TIME_WAIT
TCP asusis-dsgn:3522 localhost:89 TIME_WAIT
TCP asusis-dsgn:3523 localhost:89 TIME_WAIT
TCP asusis-dsgn:3524 localhost:89 TIME_WAIT
TCP asusis-dsgn:3525 localhost:89 TIME_WAIT
TCP asusis-dsgn:3526 localhost:89 TIME_WAIT
TCP asusis-dsgn:3527 localhost:89 TIME_WAIT
I think that for some reason the .net framework is not really closing the
socket, so it still alive with the TIME_WAIT status
And after thousands of TIME_WAIT connections, the OS reject connections.
Pleaseeeeee help to solve it!!
Thanks | | | | re: Socket dont really close. Please help microsoft MVP!
AA,
I found the following in an article about SQL server and connecting to
it:
Note that the SQL Server network library specifically does not enable the
SO_REUSEADDR TCP/IP socket option for security reasons. When SO_REUSEADDR is
enabled, a malicious user can hijack a client port to SQL Server and use the
credentials that the client supplies to gain access to the computer that is
running SQL Server. By default, because the SQL Server network library does
not enable the SO_REUSEADDR socket option, every time you open and close a
socket through the SQL Server network library on the client side, the socket
enters a TIME_WAIT state for four minutes. If you are rapidly opening and
closing SQL Server connections over TCP/IP with pooling disabled, you are
rapidly opening and closing TCP/IP sockets. In other words, each SQL Server
connection has one TCP/IP socket. If you rapidly open and close 4000 sockets
in less than four minutes, you will reach the default maximum setting for
client anonymous ports, and new socket connection attempts fail until the
existing set of TIME_WAIT sockets times out.
While you are not using SQL server, it is the same problem you are
having. What you can do is set the following value in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTim
edWaitDelay
You can set the value to the number of seconds to wait before the socket
is usable again.
However, setting this can have detrimental effects. Is there a need to
open a few thousand client connections? Can it be done a different, and
more efficient way? Setting the above value is dangerous because it is
machine-wide.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com
"AA" <aa@personal.net.py> wrote in message
news:OUreRodjDHA.2364@TK2MSFTNGP11.phx.gbl...[color=blue]
> This is making me crazy!!
>
> Please, if some body can help me.
>
> I'm testing a ver simple socket client.
> In my test I just open and close a connection (in a loop) to my local IIS
> server (port 80)
>
> using System.Net.Sockets;
>
> for (int I = 0; I < 5000; I++)
> {
> TcpClient myClient = new TcpClient();
> try
> {
> myClient.Connect("127.0.0.1", 80)
> }
> catch (Exception ex) {MessangeBox.Show(ex.ToString()); }
> myClient.Close()
> } //End for
>
>
> Everything go fine, thousands of connections are opened and closed until
> this error appear....
>
> [ System.Net.Sockets.SocketException: Only one usage of each socket[/color]
address[color=blue]
> (protocol/network address/port) is normally permitted ]
>
> After this error, I execute the netstat -a command and the lines (below)
> appear thousands of times, lines below are just a very little part of the
> complete list.
>
> TCP asusis-dsgn:3505 localhost:80 TIME_WAIT
> TCP asusis-dsgn:3506 localhost:80 TIME_WAIT
> TCP asusis-dsgn:3507 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3508 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3509 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3510 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3511 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3512 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3513 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3514 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3515 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3516 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3517 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3518 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3519 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3520 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3521 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3522 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3523 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3524 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3525 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3526 localhost:89 TIME_WAIT
> TCP asusis-dsgn:3527 localhost:89 TIME_WAIT
>
> I think that for some reason the .net framework is not really closing the
> socket, so it still alive with the TIME_WAIT status
>
> And after thousands of TIME_WAIT connections, the OS reject connections.
>
> Pleaseeeeee help to solve it!!
>
>
> Thanks
>
>[/color] | | | | re: Socket dont really close. Please help microsoft MVP!
Excellent.
Thank you very much Nicholas, this is the explain that I was searched for
long time.
AA
"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote
in message news:%23cnwz0djDHA.3316@TK2MSFTNGP11.phx.gbl...[color=blue]
> AA,
>
> I found the following in an article about SQL server and connecting to
> it:
>
> Note that the SQL Server network library specifically does not enable the
> SO_REUSEADDR TCP/IP socket option for security reasons. When SO_REUSEADDR[/color]
is[color=blue]
> enabled, a malicious user can hijack a client port to SQL Server and use[/color]
the[color=blue]
> credentials that the client supplies to gain access to the computer that[/color]
is[color=blue]
> running SQL Server. By default, because the SQL Server network library[/color]
does[color=blue]
> not enable the SO_REUSEADDR socket option, every time you open and close a
> socket through the SQL Server network library on the client side, the[/color]
socket[color=blue]
> enters a TIME_WAIT state for four minutes. If you are rapidly opening and
> closing SQL Server connections over TCP/IP with pooling disabled, you are
> rapidly opening and closing TCP/IP sockets. In other words, each SQL[/color]
Server[color=blue]
> connection has one TCP/IP socket. If you rapidly open and close 4000[/color]
sockets[color=blue]
> in less than four minutes, you will reach the default maximum setting for
> client anonymous ports, and new socket connection attempts fail until the
> existing set of TIME_WAIT sockets times out.
>
> While you are not using SQL server, it is the same problem you are
> having. What you can do is set the following value in the registry:
>
>[/color]
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTim[color=blue]
> edWaitDelay
>
> You can set the value to the number of seconds to wait before the[/color]
socket[color=blue]
> is usable again.
>
> However, setting this can have detrimental effects. Is there a need[/color]
to[color=blue]
> open a few thousand client connections? Can it be done a different, and
> more efficient way? Setting the above value is dangerous because it is
> machine-wide.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - nick(dot)paldino=at=exisconsulting<dot>com
>
> "AA" <aa@personal.net.py> wrote in message
> news:OUreRodjDHA.2364@TK2MSFTNGP11.phx.gbl...[color=green]
> > This is making me crazy!!
> >
> > Please, if some body can help me.
> >
> > I'm testing a ver simple socket client.
> > In my test I just open and close a connection (in a loop) to my local[/color][/color]
IIS[color=blue][color=green]
> > server (port 80)
> >
> > using System.Net.Sockets;
> >
> > for (int I = 0; I < 5000; I++)
> > {
> > TcpClient myClient = new TcpClient();
> > try
> > {
> > myClient.Connect("127.0.0.1", 80)
> > }
> > catch (Exception ex) {MessangeBox.Show(ex.ToString()); }
> > myClient.Close()
> > } //End for
> >
> >
> > Everything go fine, thousands of connections are opened and closed until
> > this error appear....
> >
> > [ System.Net.Sockets.SocketException: Only one usage of each socket[/color]
> address[color=green]
> > (protocol/network address/port) is normally permitted ]
> >
> > After this error, I execute the netstat -a command and the lines (below)
> > appear thousands of times, lines below are just a very little part of[/color][/color]
the[color=blue][color=green]
> > complete list.
> >
> > TCP asusis-dsgn:3505 localhost:80 TIME_WAIT
> > TCP asusis-dsgn:3506 localhost:80 TIME_WAIT
> > TCP asusis-dsgn:3507 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3508 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3509 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3510 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3511 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3512 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3513 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3514 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3515 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3516 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3517 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3518 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3519 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3520 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3521 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3522 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3523 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3524 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3525 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3526 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3527 localhost:89 TIME_WAIT
> >
> > I think that for some reason the .net framework is not really closing[/color][/color]
the[color=blue][color=green]
> > socket, so it still alive with the TIME_WAIT status
> >
> > And after thousands of TIME_WAIT connections, the OS reject[/color][/color]
connections.[color=blue][color=green]
> >
> > Pleaseeeeee help to solve it!!
> >
> >
> > Thanks
> >
> >[/color]
>
>[/color] | | | | re: Socket dont really close. Please help microsoft MVP!
Another question.. Is possible that, programatically (in .NET) to set this
property? Not for all applications, for a specific application that I want?
Because, When I use the Microsoft Application Center Test, I look that he
open and close thousands of connection, but when i execute the netstat I saw
just 1 record
It will be excellent for me
Thanks a lot
AA
"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote
in message news:%23cnwz0djDHA.3316@TK2MSFTNGP11.phx.gbl...[color=blue]
> AA,
>
> I found the following in an article about SQL server and connecting to
> it:
>
> Note that the SQL Server network library specifically does not enable the
> SO_REUSEADDR TCP/IP socket option for security reasons. When SO_REUSEADDR[/color]
is[color=blue]
> enabled, a malicious user can hijack a client port to SQL Server and use[/color]
the[color=blue]
> credentials that the client supplies to gain access to the computer that[/color]
is[color=blue]
> running SQL Server. By default, because the SQL Server network library[/color]
does[color=blue]
> not enable the SO_REUSEADDR socket option, every time you open and close a
> socket through the SQL Server network library on the client side, the[/color]
socket[color=blue]
> enters a TIME_WAIT state for four minutes. If you are rapidly opening and
> closing SQL Server connections over TCP/IP with pooling disabled, you are
> rapidly opening and closing TCP/IP sockets. In other words, each SQL[/color]
Server[color=blue]
> connection has one TCP/IP socket. If you rapidly open and close 4000[/color]
sockets[color=blue]
> in less than four minutes, you will reach the default maximum setting for
> client anonymous ports, and new socket connection attempts fail until the
> existing set of TIME_WAIT sockets times out.
>
> While you are not using SQL server, it is the same problem you are
> having. What you can do is set the following value in the registry:
>
>[/color]
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTim[color=blue]
> edWaitDelay
>
> You can set the value to the number of seconds to wait before the[/color]
socket[color=blue]
> is usable again.
>
> However, setting this can have detrimental effects. Is there a need[/color]
to[color=blue]
> open a few thousand client connections? Can it be done a different, and
> more efficient way? Setting the above value is dangerous because it is
> machine-wide.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - nick(dot)paldino=at=exisconsulting<dot>com
>
> "AA" <aa@personal.net.py> wrote in message
> news:OUreRodjDHA.2364@TK2MSFTNGP11.phx.gbl...[color=green]
> > This is making me crazy!!
> >
> > Please, if some body can help me.
> >
> > I'm testing a ver simple socket client.
> > In my test I just open and close a connection (in a loop) to my local[/color][/color]
IIS[color=blue][color=green]
> > server (port 80)
> >
> > using System.Net.Sockets;
> >
> > for (int I = 0; I < 5000; I++)
> > {
> > TcpClient myClient = new TcpClient();
> > try
> > {
> > myClient.Connect("127.0.0.1", 80)
> > }
> > catch (Exception ex) {MessangeBox.Show(ex.ToString()); }
> > myClient.Close()
> > } //End for
> >
> >
> > Everything go fine, thousands of connections are opened and closed until
> > this error appear....
> >
> > [ System.Net.Sockets.SocketException: Only one usage of each socket[/color]
> address[color=green]
> > (protocol/network address/port) is normally permitted ]
> >
> > After this error, I execute the netstat -a command and the lines (below)
> > appear thousands of times, lines below are just a very little part of[/color][/color]
the[color=blue][color=green]
> > complete list.
> >
> > TCP asusis-dsgn:3505 localhost:80 TIME_WAIT
> > TCP asusis-dsgn:3506 localhost:80 TIME_WAIT
> > TCP asusis-dsgn:3507 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3508 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3509 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3510 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3511 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3512 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3513 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3514 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3515 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3516 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3517 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3518 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3519 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3520 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3521 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3522 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3523 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3524 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3525 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3526 localhost:89 TIME_WAIT
> > TCP asusis-dsgn:3527 localhost:89 TIME_WAIT
> >
> > I think that for some reason the .net framework is not really closing[/color][/color]
the[color=blue][color=green]
> > socket, so it still alive with the TIME_WAIT status
> >
> > And after thousands of TIME_WAIT connections, the OS reject[/color][/color]
connections.[color=blue][color=green]
> >
> > Pleaseeeeee help to solve it!!
> >
> >
> > Thanks
> >
> >[/color]
>
>[/color] | | | | re: Socket dont really close. Please help microsoft MVP!
[snap][color=blue]
> While you are not using SQL server, it is the same problem you are
> having. What you can do is set the following value in the registry:
>
>[/color]
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTim[color=blue]
> edWaitDelay
> You can set the value to the number of seconds to wait before the[/color]
socket[color=blue]
> is usable again.[/color]
With respect for the workaround, this is the typical kind of quick fix I
think often introduces perhaps more misery than it fixes. It's kind of hard
to swallow it is NOT possible to close the connection from the calling
class. Regardless of whether it's a best practice approach, isn't at least
STRANGE the connection can not be closed by the process initiating it ?
Don't get me wrong, I am not arguing your probably perfectlly valid
solution, I just think it is a bit typical of how Windows programs are often
quite unstable and/or disrupt other programs' execution. The term
end-of-pipe-solution comes to mind...
Best!,
Stu | | | | re: Socket dont really close. Please help microsoft MVP!
"AA" <aa@personal.net.py> wrote in message
news:OUreRodjDHA.2364@TK2MSFTNGP11.phx.gbl...[color=blue]
> I'm testing a ver simple socket client.
> In my test I just open and close a connection (in a loop) to my local IIS
> server (port 80)
>
> using System.Net.Sockets;
>
> for (int I = 0; I < 5000; I++)
> {
> TcpClient myClient = new TcpClient();
> try
> {
> myClient.Connect("127.0.0.1", 80)
> }
> catch (Exception ex) {MessangeBox.Show(ex.ToString()); }
> myClient.Close()
> } //End for
>
>
> Everything go fine, thousands of connections are opened and closed until
> this error appear....
>
> [ System.Net.Sockets.SocketException: Only one usage of each socket[/color]
address[color=blue]
> (protocol/network address/port) is normally permitted ][/color]
[color=blue]
> I think that for some reason the .net framework is not really closing the
> socket, so it still alive with the TIME_WAIT status
>
> And after thousands of TIME_WAIT connections, the OS reject connections.[/color]
I think your problem is that you should also be closing the underlying
stream:
for (int i = 0; i < 5000; i++) {
TcpClient myClient = new TcpClient();
try {
myClient.Connect("127.0.0.1", 80)
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
} finally {
if (myClient.GetStream() != null) myClient.GetStream().Close(); //
Does a Dispose().
myClient.Close(); // Does a Dispose().
}
}
-- Alan | | | | re: Socket dont really close. Please help microsoft MVP!
Alan Pretre <no@spam> wrote:[color=blue]
> I think your problem is that you should also be closing the underlying
> stream:[/color]
<snip>
I don't think so. From the docs for TcpClient.Close():
<quote>
The Close method closes the TCP connection. It calls the Dispose method
passing a true value to release all managed and unmanaged resources
associated with the TcpClient. These resources include the underlying
Socket used for connecting with the remote host, and the NetworkStream
used to send and receive data.
</quote>
I think it's actually perfectly natural - when a connection is closed,
it *does* go into TIME_WAIT. I think everything's behaving fine here,
although I'll admit to not understanding the real details of TCP/IP as
well as I'd like. See http://www.developerweb.net/sock-faq/detail.php?id=13
for more details.
--
Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too | | | | re: Socket dont really close. Please help microsoft MVP!
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.19ef1f5ae4f7d246989831@msnews.microsoft.c om...[color=blue]
> Alan Pretre <no@spam> wrote:[color=green]
> > I think your problem is that you should also be closing the underlying
> > stream:[/color]
>
> I don't think so. From the docs for TcpClient.Close():
>
> <quote>
> The Close method closes the TCP connection. It calls the Dispose method
> passing a true value to release all managed and unmanaged resources
> associated with the TcpClient. These resources include the underlying
> Socket used for connecting with the remote host, and the NetworkStream
> used to send and receive data.
> </quote>[/color]
I've seen this firsthand in my own code. You would like to think that
myClient.Close() is sufficient but it isn't. I don't know why. Perhaps
this is a bug?
See also, for example: http://groups.google.com/groups?hl=e...gbl%26rnum%3D3
-- Alan | | | | re: Socket dont really close. Please help microsoft MVP!
Alan Pretre <no@spam> wrote:[color=blue]
> I've seen this firsthand in my own code. You would like to think that
> myClient.Close() is sufficient but it isn't. I don't know why. Perhaps
> this is a bug?[/color]
It certainly sounds like it. I wonder whether it closes the stream *if*
you've fetched it, but doesn't do everything necessary if you haven't
used the stream at all...
--
Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too | | | | re: Socket dont really close. Please help microsoft MVP!
In article <u9PrU0mjDHA.2080@TK2MSFTNGP10.phx.gbl>, Alan Pretre wrote:[color=blue]
> You would like to think that
> myClient.Close() is sufficient but it isn't. I don't know why.[/color]
TIME_WAIT is there to ensure that any late arriving packets have
arrived before closing the connection. It's necessary as the next
application using that local port should be perturbed if it receives a
late packet from the previous connection. http://tangentsoft.net/wskfaq/articl...gging-tcp.html is a nice
summary.
Mike |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|