473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Gracefully disconnecting a TCP server

Hello,

I'm building a small TCP based server which uses the Async commands. When
a new connection is made to the server, I store the new socket in a
hashtable,
using the client IP as the key.

Then I have a method in my server which is called Stop(). Now I would like
for
some advice on how to close down the server in a gracefull manner. I tried
the
following (Note: pesudo code not VB.NET ;)

ForEach ClientSocket In The HashTable
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
Remove the ClientSocket from the HashTable
End ForEach

Server.Shutdown(SocketShutdown.Both)
Server.Close()

However when I do this, the Server.Shutdown(SocketShutdown.Both) call throws
an exception telling me I tried to do something on a socket which is already
connected.

Suggestions, solutions etc for a gracefull shutdown schema is welcome =)

//Andreas
Nov 15 '05 #1
7 8163

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl...
Hello,

I'm building a small TCP based server which uses the Async commands. When
a new connection is made to the server, I store the new socket in a
hashtable,
using the client IP as the key.

Then I have a method in my server which is called Stop(). Now I would like
for
some advice on how to close down the server in a gracefull manner. I tried
the
following (Note: pesudo code not VB.NET ;)

ForEach ClientSocket In The HashTable
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
Remove the ClientSocket from the HashTable
End ForEach

Server.Shutdown(SocketShutdown.Both)
Server.Close()

However when I do this, the Server.Shutdown(SocketShutdown.Both) call throws an exception telling me I tried to do something on a socket which is already connected.

Suggestions, solutions etc for a gracefull shutdown schema is welcome =)

//Andreas


I am having the same problem with my C++ TCP server. I get error
WSAENOTCONN (10057) when I call shutdown on my listener socket. I do not
get the error on other sockets though, only the listener socket. I am
interested in seeing any replies to this thread.

PS-
You should reconsider your shutdown technique above. The correct way to do
it is to shutdown the sends and then call recv on the socket until there is
no more data. Your technique has a potential to lose data.
Nov 15 '05 #2

"Trevor" <tr****@nospam.com> skrev i meddelandet
news:uy*************@TK2MSFTNGP10.phx.gbl...

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl...
Hello,

I'm building a small TCP based server which uses the Async commands. When a new connection is made to the server, I store the new socket in a
hashtable,
using the client IP as the key.

Then I have a method in my server which is called Stop(). Now I would like for
some advice on how to close down the server in a gracefull manner. I tried the
following (Note: pesudo code not VB.NET ;)

ForEach ClientSocket In The HashTable
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
Remove the ClientSocket from the HashTable
End ForEach

Server.Shutdown(SocketShutdown.Both)
Server.Close()

However when I do this, the Server.Shutdown(SocketShutdown.Both) call throws
an exception telling me I tried to do something on a socket which is

already
connected.

Suggestions, solutions etc for a gracefull shutdown schema is welcome =)

//Andreas


I am having the same problem with my C++ TCP server. I get error
WSAENOTCONN (10057) when I call shutdown on my listener socket. I do not
get the error on other sockets though, only the listener socket. I am
interested in seeing any replies to this thread.

PS-
You should reconsider your shutdown technique above. The correct way to

do it is to shutdown the sends and then call recv on the socket until there is no more data. Your technique has a potential to lose data.


Should I do this for each "ClientSocket" in the HashTable or only on the
listener
socket and skip the entire "ForEach ClientSocket In The HashTable" section?

//Andreas
Nov 15 '05 #3

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...


Should I do this for each "ClientSocket" in the HashTable or only on the
listener
socket and skip the entire "ForEach ClientSocket In The HashTable" section?
//Andreas


Andreas,

Yes, you should do this for each "ClientSocket" in the HashTable. The
listener socket should not have any data being sent or received on it so it
is not an issue on the listener socket. You just want to make sure that the
socket is empty (read & sends) before you forget about it completely or you
may lose data.
Nov 15 '05 #4

"Trevor" <tr****@nospam.com> skrev i meddelandet
news:%2***************@TK2MSFTNGP11.phx.gbl...

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...


Should I do this for each "ClientSocket" in the HashTable or only on the
listener
socket and skip the entire "ForEach ClientSocket In The HashTable" section?

//Andreas


Andreas,

Yes, you should do this for each "ClientSocket" in the HashTable. The
listener socket should not have any data being sent or received on it so

it is not an issue on the listener socket. You just want to make sure that the socket is empty (read & sends) before you forget about it completely or you may lose data.


Any suggestions on how to wait in a correct mannor? If the recieve is done
in
an async mannor then you can't be quite sure when data which has been
transmitted before the Shutdown.Send has been ordered will arrive. One of
the reasons for this has to be the ThreadPool which is used by the async
socket operations internally. Since the ThreadPool is per process, if the
process uses a lot of pooled threads then you won't know when there will
be a free threat to handle the pending recieve operation(s), especially if
the
system is under load and the ThreadPool is limiting the number of threads
in the pool to reduce the overhead when context switching ?

Have I totally missunderstood this? How the heck do I wait for an unknown
of queued async recives before closing the socket? Especially since my
Stop() method is called async? Do I need to include an async Stop() call
(BeginStop/EndStop) ?

//Andreas
Nov 15 '05 #5
"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...

"Trevor" <tr****@nospam.com> skrev i meddelandet
news:%2***************@TK2MSFTNGP11.phx.gbl...

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...


Should I do this for each "ClientSocket" in the HashTable or only on the listener
socket and skip the entire "ForEach ClientSocket In The HashTable"

section?

//Andreas


Andreas,

Yes, you should do this for each "ClientSocket" in the HashTable. The listener socket should not have any data being sent or received on it so

it
is not an issue on the listener socket. You just want to make sure that

the
socket is empty (read & sends) before you forget about it completely or

you
may lose data.


Any suggestions on how to wait in a correct mannor? If the recieve is done
in
an async mannor then you can't be quite sure when data which has been
transmitted before the Shutdown.Send has been ordered will arrive. One of
the reasons for this has to be the ThreadPool which is used by the async
socket operations internally. Since the ThreadPool is per process, if the
process uses a lot of pooled threads then you won't know when there will
be a free threat to handle the pending recieve operation(s), especially if
the
system is under load and the ThreadPool is limiting the number of threads
in the pool to reduce the overhead when context switching ?

Have I totally missunderstood this? How the heck do I wait for an unknown
of queued async recives before closing the socket? Especially since my
Stop() method is called async? Do I need to include an async Stop() call
(BeginStop/EndStop) ?

//Andreas

Andreas,
I'm sorry I can't help you here. I have never programmed sockets with
..NET. However, I can tell you that there is a native function to check if a
socket has incoming / outgoing data queued up. If it helps, the native
function is called "select" and is a implementation of the BSD socket select
function. If you can't figure out a ".NET way" to do it then you can always
P/Invoke this function (as long as you have access to the SOCKET handle in
..NET).
Nov 15 '05 #6
Seems that the Socket.Available property will tell you if there is
any recieved data to read from a socket. However I have yet to
figure out how to check if there are any data waiting to be sent
from the server to the client before the socket is closed.

There is a static function called Socket.Select but I couldn't find
anything which would help. Another method was Socket.Poll
but it didn't either seem to help.
//Andreas

"Trevor" <tr****@nospam.com> skrev i meddelandet
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...

"Trevor" <tr****@nospam.com> skrev i meddelandet
news:%2***************@TK2MSFTNGP11.phx.gbl...

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...
>
>
> Should I do this for each "ClientSocket" in the HashTable or only on the > listener
> socket and skip the entire "ForEach ClientSocket In The HashTable"
section?
>
> //Andreas
>
>

Andreas,

Yes, you should do this for each "ClientSocket" in the HashTable. The listener socket should not have any data being sent or received on it
so it
is not an issue on the listener socket. You just want to make sure
that the
socket is empty (read & sends) before you forget about it completely
or you
may lose data.
Any suggestions on how to wait in a correct mannor? If the recieve is done in
an async mannor then you can't be quite sure when data which has been
transmitted before the Shutdown.Send has been ordered will arrive. One of the reasons for this has to be the ThreadPool which is used by the async
socket operations internally. Since the ThreadPool is per process, if the process uses a lot of pooled threads then you won't know when there will
be a free threat to handle the pending recieve operation(s), especially if the
system is under load and the ThreadPool is limiting the number of threads in the pool to reduce the overhead when context switching ?

Have I totally missunderstood this? How the heck do I wait for an unknown of queued async recives before closing the socket? Especially since my
Stop() method is called async? Do I need to include an async Stop() call
(BeginStop/EndStop) ?

//Andreas

Andreas,
I'm sorry I can't help you here. I have never programmed sockets with
.NET. However, I can tell you that there is a native function to check if

a socket has incoming / outgoing data queued up. If it helps, the native
function is called "select" and is a implementation of the BSD socket select function. If you can't figure out a ".NET way" to do it then you can always P/Invoke this function (as long as you have access to the SOCKET handle in
.NET).

Nov 15 '05 #7
"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
Seems that the Socket.Available property will tell you if there is
any recieved data to read from a socket. However I have yet to
figure out how to check if there are any data waiting to be sent
from the server to the client before the socket is closed.

There is a static function called Socket.Select but I couldn't find
anything which would help. Another method was Socket.Poll
but it didn't either seem to help.
//Andreas


Andreas,

I took a quick look at Socket.Select and it is indeed the .NET
implementation of select() that I was referring to in my previous post. If
I am reading this documentation correctly, you need to create two IList
objects, both of which contain the same socket (client socket who is
interested in disconnecting) and pass them to the checkRead & checkWrite
parameters of Socket.Select.

PSEUDO code:
--------------
IList read, write, err;
// Copy Socket handle to each IList
read = new IList(thesocket);
write = new IList(thesocket);
err = new IList(); // not interested in error, but the function requires
that you do not pass it NULL so give it an empty IList.
Socket.Select(read, write, err, 1000000); // block thread for 1 sec at the
most.
if (read.Count == 0)
{
// no more incoming RX data.
}
if (write.Count == 1)
{
// no more incoming TX data.
}

That should give you a rough idea of how it works. Keep in mind that I am
not a .NET programmer and this is pseudo code so don't expect it to work in
your compiler. You will need to loop until there is no more incoming RX
data and there is no more outgoing TX data.
Nov 15 '05 #8

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

Similar topics

0
by: phoenix | last post by:
Hi, i'm using a dsl connection to surf the internet. I use the following code to connect to the internet : // Check if already connected if (InternetAttemptConnect(0) != 0) return false;...
0
by: amita | last post by:
I have a Client (a TcpClient object) established connection with the Server(a TcpListener). I want that when the Server goes down, the TcpClient detects this disconnection and when the Server is back...
6
by: Mark | last post by:
Hello, I'm trying to handle HttpRequestValidationException. If a hacker enters certain values into a textbox, like "<script>", it will trigger this error. I understand why .Net has this, but I...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
6
by: John J. Hughes II | last post by:
I have an application that connects to an SQL server. I need to be able to disconnect all connection that I opened. I am closing and disposing the connection but the connection pool is keeping...
7
by: Frank | last post by:
Hi, I have a console app that I want to stop gracefully by another program. The console app already has code to intercept control-c and logoff and so on. But how do I send a control-c to that...
0
by: Macca | last post by:
Hi, I would like to write a "server" application that can run without a required GUI and just processes data. I would like to allow multiple "client" GUI winform apps to be able to "attach" to...
1
by: salvagedog | last post by:
We use xp_smtp_sendmail for all emailing from SQL Server. Occasionally it fails, generating the following message: Error: disconnecting from server mail.mydomain.com failed The failures are...
3
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi All, I have a ASP/C# application that connect to Oracle database . After issuing my SQL query if I close the browser or move into another page ( ie whle executing in the databse serevr) will...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.