473,549 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does Socket.Shutdown or Socket.Close Block and when?


Context:
C#
System.Net.Sock ets
Socket created with constructor prarmeters Internetwork, Stream and TCP
everything else is left at the default parameters and options except linger
may be changed as I find appropriate.
I am using the socket asynchronously by calling the BeingSend and
BeginReceive calls. I would like to be able to call shutdown and close
asynchronously if possible.
As far as I understand from the MSDN documentation calling
Socket.Shutdown on a socket will ensure that all data in the send buffer will
get sent to the remote host before the socket is closed. I also understand
that the linger socket option can be used to set how long the socket will try
to send any data in the send buffer before giving up and closing the socket.
The MSDN documentation for Socket.Close states:
If you need to call Close without first calling Shutdown, you can ensure
that data queued for outgoing transmission will be sent by setting the
DontLinger Socket option to false and specifying a non-zero time-out
interval. Close will then block until this data is sent or until the
specified time-out expires.

So it appears from the documentation excerpt that a call to Socket.Shutdown
is not necessary as long as linger is enabled and set to the desired timeout
before calling Socket.Close, is this true?
My questions are these:

*Does the linger option affect Socket.Shutdown in any way? If so, what way
or ways?

*Does Socket.Shutdown block while sending the data in the socket's send
buffer or does it simply delegate the blocking to close?
If so, where is the asynchronous method for Socket.Shutdown so I don't have
to waste a thread just to babysit the shutdown.

*If Socket.Close blocks while sending the remaining data in the send buffer
(as indicated by the MSDN excerpt above) how can I call this asynchronously
close a lingering socket without tying up a threadpool thread for 10 seconds
or so (an eternity to be using a threadpool thread in a server application)?
Thanks,

--
- Chris Tanger

Nov 16 '05 #1
4 18093
Hi Chris,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you have several question on linger
options and socket shutdown method. If there is any misunderstandin g,
please feel free to let me know.

As far as I know, Socket.Shutdown method waits until all the data in the
buffer has been sent or received. However, if we only set linger options,
the Socket will shutdown after certain timeout interval. So it is necessary
to call Socket.Shutdown .

I don't think linger option affects Socket.Shutdown , it affects close
method.

Socket.Shutdown blocks while sending the data in the socket's send buffer.
There isn't asynchronous method for Socket.Shutdown in the .net framework
class library. However, I think you can put the socket operations in a
seperate thread from UI thread.

Socket.Close also doesn't have asynchronous methods. You can also try to
put all the socket operation in another thread.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #2
I was hoping for a more definitive answer, but no problem, I was just
wondering if anyone knew for certain. Also, it is a bummer that Shutdown and
Close cannot be called asynchronously. Here is why I wish to call them
asynchronously without using an asynchronous delegate: I have a server that
may have 1000 TCP connections coming into it. The server will expect to
receive a heartbeat message every 5 seconds on each socket (to make sure the
connection is still up to each client). If the server does not receive a
heartbeat within say 10 seconds then the server will proceed to shutdown the
socket. I wish to give the socket about 10 additional seconds to complete
sending any data and to send a disconnect notification message if possible
with the reason for the disconnect(hear tbeat timeout). Let us assume that
the network path that carries all of these client-server connections goes
down (a black hole down, so no nack packets are sent like when a firewall is
blocking). The TCP connections will likely not throw an exception for
20-120+ seconds. The server will notice that it has not received a heartbeat
on each connection within the specified amount of time (10 seconds since the
last heartbeat). The server will send a disconnect imminent message with the
reason (heartbeat timeout) to each connection and then call shutdown on each
connection. I want the server to attempt to send any remaining data in the
socket send buffers for 10 seconds after shutdown is called. If it takes
longer than 10 seconds I want the socket to perform a hard close and throw
away any data in the send buffer. If all connections go down at the same
time calling shutdown on a separate thread for each connection would require
1000 threads. As you can imagine this is not a scalable solution. The
threadpool cannot handle it, and I cannot reasonable start 1000 threads
simultaneously for the task either.

One solution that comes to mind is creating my own shutdown function that
simply waits for 10 seconds after it is called using a System.Threadin g.Timer
and then performs a hard close on the socket. The unfortunate thing about
that particular solution is that the socket will not be closed immediately if
the send buffer is empty or becomes empty before the elapsed timeout.

So this is why I need to determine how these things work.

I am going to write a test program that accepts incoming socket connections
with at 8K receive buffer. The test program will never perform a read on the
socket. There will be a corresponding test program that makes a connection
to the listening program. It will have a 8K send buffer. The sending
program will send 12k of data after connecting. This will cause the listing
program's receive buffer to be completely filled to its 8K limit at which
point the sending programs socket will get blocked leaving its 8K send buffer
half full with 4K of data. Then I will call shutdown on the send programs
socket and see what happens. I will also play around with the various linger
options to determine how the Socket class behaves.

I was posting to this group in the hopes that someone already knew the
answer, but this program should enlighten me.

One other question comes to mind, what if I set the Socket.Blocking = false?
Will this result in Shutdown or close no longer blocking?

Thanks,

-Chris
Nov 16 '05 #3

OK, here are the results for anyone who is interested:
I built and ran a program just as I said I would below. After
experimenting a bit I found the following: Kevin Yu is correct
Socket.Shutdown is unaffected by the LingerOption.
Calling Socket.Shutdown does not block, also if Socket.Shutdown is first
called, then subsequent calls to Socket.Close do not block either. The OS
keeps trying to send the data in the send buffers of the socket until TCP
times out (the timeout varies according to TCP algorithms and parameters).
Enabling linger and setting the timeout to a certain number of seconds
will cause a subsequent call to Socket.Close to block until either all data
in the send buffer has been sent or the timeout has elapsed. If linger is
disabled or Socket.Shutdown has been previously called then Socket.Close will
not block.
The disappointing part of all this is that I have no way to do a
Socket.Shutdown with a timeout. In other words it would be nice to have a
linger option for the Socket.Shutdown . That way I don't have a blocking
problem and I also don't have a problem with sockets remaining open for too
long and saping down system resources while hopelessly trying to send to a
defunct client. If anyone knows how to do a socket.Shutdown type of
operation with a timeout that initiates a hard close of the socket once
elapsed.

-Chris Tanger

"Chris Tanger" wrote:
I was hoping for a more definitive answer, but no problem, I was just
wondering if anyone knew for certain. Also, it is a bummer that Shutdown and
Close cannot be called asynchronously. Here is why I wish to call them
asynchronously without using an asynchronous delegate: I have a server that
may have 1000 TCP connections coming into it. The server will expect to
receive a heartbeat message every 5 seconds on each socket (to make sure the
connection is still up to each client). If the server does not receive a
heartbeat within say 10 seconds then the server will proceed to shutdown the
socket. I wish to give the socket about 10 additional seconds to complete
sending any data and to send a disconnect notification message if possible
with the reason for the disconnect(hear tbeat timeout). Let us assume that
the network path that carries all of these client-server connections goes
down (a black hole down, so no nack packets are sent like when a firewall is
blocking). The TCP connections will likely not throw an exception for
20-120+ seconds. The server will notice that it has not received a heartbeat
on each connection within the specified amount of time (10 seconds since the
last heartbeat). The server will send a disconnect imminent message with the
reason (heartbeat timeout) to each connection and then call shutdown on each
connection. I want the server to attempt to send any remaining data in the
socket send buffers for 10 seconds after shutdown is called. If it takes
longer than 10 seconds I want the socket to perform a hard close and throw
away any data in the send buffer. If all connections go down at the same
time calling shutdown on a separate thread for each connection would require
1000 threads. As you can imagine this is not a scalable solution. The
threadpool cannot handle it, and I cannot reasonable start 1000 threads
simultaneously for the task either.

One solution that comes to mind is creating my own shutdown function that
simply waits for 10 seconds after it is called using a System.Threadin g.Timer
and then performs a hard close on the socket. The unfortunate thing about
that particular solution is that the socket will not be closed immediately if
the send buffer is empty or becomes empty before the elapsed timeout.

So this is why I need to determine how these things work.

I am going to write a test program that accepts incoming socket connections
with at 8K receive buffer. The test program will never perform a read on the
socket. There will be a corresponding test program that makes a connection
to the listening program. It will have a 8K send buffer. The sending
program will send 12k of data after connecting. This will cause the listing
program's receive buffer to be completely filled to its 8K limit at which
point the sending programs socket will get blocked leaving its 8K send buffer
half full with 4K of data. Then I will call shutdown on the send programs
socket and see what happens. I will also play around with the various linger
options to determine how the Socket class behaves.

I was posting to this group in the hopes that someone already knew the
answer, but this program should enlighten me.

One other question comes to mind, what if I set the Socket.Blocking = false?
Will this result in Shutdown or close no longer blocking?

Thanks,

-Chris

Nov 16 '05 #4
Hi Chris,

I think Socket.Blocking only affects on send and receive methods. While
Shutdown will block by default.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5

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

Similar topics

1
6324
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close operation". So, I figured it would be useful to post some code about how socket.close() has an implicit send in it and you can actually gain some...
4
8713
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many...
2
4143
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send I get the last chunk of data and am not able to retrieve it anymore cause the socket will be dead. Loop: { socket.Receive <----------- data...
7
8720
by: e2wugui | last post by:
thread1: while 1: buf = s.read() process(buf) thread2: while 1: buf = getdata() s.write(buf)
8
11003
by: Dinsdale | last post by:
I am trying to write a Tcp "Server" that opens a class that wraps a tcp socket when a new connection is made (Listener.AcceptSocket()). Everything is going swimmingly except when I try to close the socket during a read and I get the following error: <error_msg> An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in...
1
2101
by: PAzevedo | last post by:
Hi there. I called Socket.Select() on a group of sockets, and got one that was writable. On that socket i called Send() and sent a buffer of size smaller then SendBufferSize that way Send() won't block. Now right after that Send() i want to get ride of the socket but since my server model isn't asynchronous i don't want to block. So what...
0
2581
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I have several problems re-establishin connection between peers. Below are my problem cases after closing of the first successfull communication; ...
19
4170
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port); pTemp->Connect(); m_collClients.push_back(pTemp);
0
7459
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...
0
7726
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. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7819
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...
1
5377
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
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
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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...

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.