473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Close a blocked socket

I have a sync socket application. The client is blocked with
Socket.Receive( ...) in a thread, another thread calls Socket.Close(). This
unblock the blocked thread. But the socket server is still connected. Any
idea?

Thanks.
Apr 10 '07 #1
14 5896
Use asynchronous non-blocking sockets. Implementing blocking sockets in
situations where connections may fail is looking for trouble.

Michael

"MikeZ" <Mi***@discussi ons.microsoft.c omwrote in message
news:50******** *************** ***********@mic rosoft.com...
>I have a sync socket application. The client is blocked with
Socket.Receive( ...) in a thread, another thread calls Socket.Close(). This
unblock the blocked thread. But the socket server is still connected. Any
idea?

Thanks.

Apr 10 '07 #2
On Mon, 09 Apr 2007 18:10:00 -0700, MikeZ
<Mi***@discussi ons.microsoft.c omwrote:
I have a sync socket application. The client is blocked with
Socket.Receive( ...) in a thread, another thread calls Socket.Close().
This unblock the blocked thread. But the socket server is still
connected. Any idea?
What does "the socket server is still connected"? Do you mean that the
server's socket for the connection (the one returned in the accept call
when the client connected) doesn't indicate that the connection has been
closed or reset?

If so, you should probably check your linger options. If you use a zero
timeout when closing a socket, then the connection is simply aborted
without a graceful shutdown and the server won't know that the connection
has been closed. IMHO, it's better to use the Shutdown method first, to
explicitly initiate a graceful shutdown rather than relying on the
behavior of the Close method (since what it does depends on a variety of
other things).

All that said, since the connection could be aborted for other reasons
without the server being notified, you still logic in the server to deal
with that condition. A graceful shutdown is nice, especially to ensure
that pending data that has been enqueued for sending really gets sent.
But there's no way to guarantee one, so your server needs to be prepared
to deal with the case when it doesn't happen.

Pete
Apr 10 '07 #3
On Mon, 09 Apr 2007 19:50:20 -0700, Michael Rubinstein
<mSPAM_REMOVEr@ m®ubinstein.co mwrote:
Use asynchronous non-blocking sockets. Implementing blocking sockets
in situations where connections may fail is looking for trouble.
I disagree. There are a variety of reasons to use non-blocking sockets
rather than blocking, but the criteria "where connections may fail" is not
one of them (and is meaningless anyway...since any connection can fail,
using that as a reason to not use blocking sockets would mean no one would
ever use blocking sockets for connection-oriented protocols, which is
obviously not the case).

Pete
Apr 10 '07 #4
Pete, you are right. Mike did not mention failed connections.

Michael

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Mon, 09 Apr 2007 19:50:20 -0700, Michael Rubinstein
<mSPAM_REMOVEr@ m®ubinstein.com wrote:
> Use asynchronous non-blocking sockets. Implementing blocking sockets
in situations where connections may fail is looking for trouble.

I disagree. There are a variety of reasons to use non-blocking sockets
rather than blocking, but the criteria "where connections may fail" is not
one of them (and is meaningless anyway...since any connection can fail,
using that as a reason to not use blocking sockets would mean no one would
ever use blocking sockets for connection-oriented protocols, which is
obviously not the case).

Pete

Apr 10 '07 #5
Peter,

In Server, the Socket.Connecte d property is TRUE even this socket is closed
by client.

I implemented a socket pool in server side, I need to know a socket status,
and close the socket, so other client can open a new socket when pool reach
the max number.

The server side is Async socket, and client is Sync socket. I found when
server is sending data to client, and client close the socket, server know
the socket is disconnected. When server does not send any data to client, and
client close the connection, server still think the socket is connected.

Anyway, I am using the last sending time to control the socket pool. It is
not perfect.

Thanks.

Apr 10 '07 #6
On Tue, 10 Apr 2007 05:07:35 -0700, Michael Rubinstein
<mSPAM_REMOVEr@ m®ubinstein.co mwrote:
Pete, you are right. Mike did not mention failed connections.
Well, what I meant was that even if he did mention failed connections,
that's not an argument against blocking sockets.

You may not be in agreement with that opinion. :)
Apr 10 '07 #7
On Tue, 10 Apr 2007 09:48:01 -0700, MikeZ
<Mi***@discussi ons.microsoft.c omwrote:
[...]
The server side is Async socket, and client is Sync socket. I found when
server is sending data to client, and client close the socket, server
know the socket is disconnected. When server does not send any data to
client, and client close the connection, server still think the socket
is connected.
As I wrote in my previous message, you may want to look at the linger
options for the socket, and/or simply use Shutdown before calling Close on
the socket.

If you just close the socket on the client side without doing a graceful
shutdown of the connection, the server isn't notified and you wind up with
exactly the situation you're talking about.

Pete
Apr 10 '07 #8
Pete, my opinion is that blocking sockets should be used only when there
is a compelling reason for doing so. I must admit, I can't name a single
one. Must be my ignorance <g>. I suspect the popularity of blocking sockets
is more due to the fact that earlier (8 years or so) MSDN examples use
blocking sockets, while non-blocking Winsock samples were published later
and are less known.

Michael

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Tue, 10 Apr 2007 05:07:35 -0700, Michael Rubinstein
<mSPAM_REMOVEr@ m®ubinstein.com wrote:
> Pete, you are right. Mike did not mention failed connections.

Well, what I meant was that even if he did mention failed connections,
that's not an argument against blocking sockets.

You may not be in agreement with that opinion. :)

Apr 10 '07 #9
On Tue, 10 Apr 2007 12:58:12 -0700, Michael Rubinstein
<mSPAM_REMOVEr@ m®ubinstein.co mwrote:
Pete, my opinion is that blocking sockets should be used only when
there is a compelling reason for doing so.
I can agree with that. But "compelling reason" is in the eye of the
beholder.
I must admit, I can't name a single one.
Personally, I see no reason to not use blocking sockets if one is dealing
with a very simple situation (say, peer-to-peer application where you only
ever have one connection). Before .NET, using plain old Winsock, I would
extend this to be a simple situation in which there's no window message
pump, since WSAAsyncSelect is a pretty easy and convenient way to handle
socket i/o without adding a new thread.

In .NET, the async use of Sockets is quite nice and, even more important,
scales very well due to its use of IOCP. But it may be easier for a
programmer to conceptualize his i/o algorithm by dedicating a thread or
two to the socket i/o and using blocking sockets. It is much more
important for the code to be written correctly than for it to be written
using some particular paradigm, and if using blocking sockets advances
this goal of correctness, then it seems to me that's a good reason to use
blocking sockets.

In the simple scenario I mention above, I certainly see no real advantage
to using the Begin/End pattern over straight blocking sockets.

But my main point is that whatever one thinks about blocking versus
non-blocking, I really don't see how the question of whether a connection
can fail or not comes into it.

Pete
Apr 10 '07 #10

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

Similar topics

0
1607
by: Daniel T. | last post by:
The code below fails. Socket.accept blocks inside the thread and doesn't let go, even after the socket was closed. From the error presented, the socket never actually closes. I realize that the below is a basic Java idiom but how do you do the same thing in Python? import unittest import socket import threading
3
9080
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP...
9
11322
by: AA | last post by:
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;
6
4096
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: Connect Receive response Send Connect ACK
4
18093
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets 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...
12
2116
by: MuZZy | last post by:
Hi, Sorry for a repeated post but i didn't receive an answer and will try to re-phrase my question: How do i close an additional thread from the main thread, if this additional thread is stuck waiting for a blocking operation, eg. if in this additional thread i wait for a Tcp connection: Socket s = tcpListener.AcceptSocket();
4
11361
by: Haim | last post by:
it is very strange for me that a simple event of closing socket that was in the the winsock object of vb6 , i didn't found yet in the vb.net the only way i found is to try to send something to the socket and if i got error then the socket is closed. there must be a way to get this event without trying to send something , since it is in...
3
2497
by: Diego F. | last post by:
Hello. I don't know how to force the closing of a blocked socket. I have an server application that listens to a port. It works as it is very simple, but I'm not sure about how to close the socket. I use a thread to run the socket part, so I don't block the user interface. If I close the application, and open again, the port is still in...
0
7507
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...
0
7435
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
7698
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. ...
1
7461
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...
0
6030
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...
1
5361
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
3492
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.