Having finally got a working asynchronous server up (sends and recieves data
fine) i get stuck again.
I'm not quite sure how i can safely disconnect the client. It doesnt really
matter how initiates the disconnect, both sides should be able to do it.
The problem lies in the callback function for reading data, more
specifically, at this point:
bytesRead = handler.EndReceive(AR)
bytesread is an int, handler a socket and AR and IAsyncResult.
When disconnecting it still seems to want to read the termination signals,
but by then the socket is already gone.
Now i currently solve it in a rather crappy manner :) I catch the error, set
bytesread to a negative value and make sure that when bytesread is negative,
all the remaining code does not get run.
Can someone explain how i could catch a termination signal and cleanly solve
it ? 6 4696
On 2003-11-18, Silby <Si***@nothanks.net> wrote: Having finally got a working asynchronous server up (sends and recieves data fine) i get stuck again. I'm not quite sure how i can safely disconnect the client. It doesnt really matter how initiates the disconnect, both sides should be able to do it. The problem lies in the callback function for reading data, more specifically, at this point: bytesRead = handler.EndReceive(AR)
bytesread is an int, handler a socket and AR and IAsyncResult.
When disconnecting it still seems to want to read the termination signals, but by then the socket is already gone. Now i currently solve it in a rather crappy manner :) I catch the error, set bytesread to a negative value and make sure that when bytesread is negative, all the remaining code does not get run.
Can someone explain how i could catch a termination signal and cleanly solve it ?
While I'm not exactly sure what your asking... EndReceive will return 0
when the remote endpoint has shutdown and all data in the buffer has
been recieved.
--
Tom Shelton
MVP [Visual Basic]
"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl... On 2003-11-18, Silby <Si***@nothanks.net> wrote: Having finally got a working asynchronous server up (sends and recieves
data fine) i get stuck again. I'm not quite sure how i can safely disconnect the client. It doesnt
really matter how initiates the disconnect, both sides should be able to do it. The problem lies in the callback function for reading data, more specifically, at this point: bytesRead = handler.EndReceive(AR)
bytesread is an int, handler a socket and AR and IAsyncResult.
When disconnecting it still seems to want to read the termination
signals, but by then the socket is already gone. Now i currently solve it in a rather crappy manner :) I catch the error,
set bytesread to a negative value and make sure that when bytesread is
negative, all the remaining code does not get run.
Can someone explain how i could catch a termination signal and cleanly
solve it ?
While I'm not exactly sure what your asking... EndReceive will return 0 when the remote endpoint has shutdown and all data in the buffer has been recieved.
-- Tom Shelton MVP [Visual Basic]
Basicly, when one of both sockets closes (Socket.Shutdown, Socket.Close) it
will still run the callback function to read data and ofcourse crash then,
because there it still tries to do handler.EndReceive. Catching the error
shows:
Cannot access a disposed object named "System.net.sockets.socket".
So the socket was already terminated by the time it got into the read
callback.
What i want to know is :
is this normal behaviour and am i supposed to handle it with try/catch
(doubtful :)
Is there something like BeginDisconnect that i can use to handle this ?
Hope this clears it up a bit Tom Shelton MVP [Visual Basic] Basicly, when one of both sockets closes (Socket.Shutdown, Socket.Close)
it will still run the callback function to read data and ofcourse crash then, because there it still tries to do handler.EndReceive. Catching the error shows: Cannot access a disposed object named "System.net.sockets.socket". So the socket was already terminated by the time it got into the read callback.
What i want to know is : is this normal behaviour and am i supposed to handle it with try/catch (doubtful :) Is there something like BeginDisconnect that i can use to handle this
? Hope this clears it up a bit
And another update ...
I came to the conclusion i could do the following:
ReadCallBack
numread = socket.EndReceive
if (numread > 0) then
buffer = whatweread
if instr(buffer, "QUIT") then
close_connection
** Do NOT do Beginreceive here ! **
else
socket.BeginReceive
End if
End if
End Sub
this solves the problem of the read call back being called with a terminated
socket.
But now the sockets dont close !!
client side goes into FIN_WAIT2 and server side goes into CLOSE_WAIT.
Closing the socket goes as follows:
close_connection
socket.Shutdown(socketshutdown.both)
socket.close
end sub
"Silby" <Si***@nothanks.net> wrote in message
news:Ma*********************@phobos.telenet-ops.be... Tom Shelton MVP [Visual Basic] Basicly, when one of both sockets closes (Socket.Shutdown, Socket.Close) it will still run the callback function to read data and ofcourse crash
then, because there it still tries to do handler.EndReceive. Catching the
error shows: Cannot access a disposed object named "System.net.sockets.socket". So the socket was already terminated by the time it got into the read callback.
What i want to know is : is this normal behaviour and am i supposed to handle it with
try/catch (doubtful :) Is there something like BeginDisconnect that i can use to handle
this ? Hope this clears it up a bit
And another update ...
I came to the conclusion i could do the following:
ReadCallBack numread = socket.EndReceive if (numread > 0) then buffer = whatweread if instr(buffer, "QUIT") then close_connection ** Do NOT do Beginreceive here ! ** else socket.BeginReceive End if End if End Sub
this solves the problem of the read call back being called with a
terminated socket. But now the sockets dont close !! client side goes into FIN_WAIT2 and server side goes into CLOSE_WAIT.
Closing the socket goes as follows:
close_connection socket.Shutdown(socketshutdown.both) socket.close end sub
And i should learn not to post before trying all my options ...
Seems TcpClient isnt all that, and thats why the socket didnt close down
cleanly. Fixed now, problems solved.
Thanks for taking the time to help.
In article <bG*********************@phobos.telenet-ops.be>, Silby wrote: "Silby" <Si***@nothanks.net> wrote in message news:Ma*********************@phobos.telenet-ops.be... > > Tom Shelton > > MVP [Visual Basic] > > Basicly, when one of both sockets closes (Socket.Shutdown, Socket.Close) it > will still run the callback function to read data and ofcourse crash then, > because there it still tries to do handler.EndReceive. Catching the error > shows: > Cannot access a disposed object named "System.net.sockets.socket". > So the socket was already terminated by the time it got into the read > callback. > > What i want to know is : > is this normal behaviour and am i supposed to handle it with try/catch > (doubtful :) > Is there something like BeginDisconnect that i can use to handle
this ? > > Hope this clears it up a bit > >
And another update ...
I came to the conclusion i could do the following:
ReadCallBack numread = socket.EndReceive if (numread > 0) then buffer = whatweread if instr(buffer, "QUIT") then close_connection ** Do NOT do Beginreceive here ! ** else socket.BeginReceive End if End if End Sub
this solves the problem of the read call back being called with a terminated socket. But now the sockets dont close !! client side goes into FIN_WAIT2 and server side goes into CLOSE_WAIT.
Closing the socket goes as follows:
close_connection socket.Shutdown(socketshutdown.both) socket.close end sub
And i should learn not to post before trying all my options ... Seems TcpClient isnt all that, and thats why the socket didnt close down cleanly. Fixed now, problems solved.
Thanks for taking the time to help.
LOL! I'm glad you solved the issues - but I'm not exactly sure what I
did to help :)
--
Tom Shelton
MVP [Visual Basic]
I am currently Working On MudProject This is the way that we close our
connections
Public Sub CloseClientConenction()
IsConnected = False
Dim NWStream As NetworkStream =Client.GetStream
NWStream.Flush()
NWStream.Close()
Client.Close()
RaiseEvent ClientClose(Me)
End Sub
Also There is a Really Well Done MultiThreaded TCPserver Class File at www.voidRealms.com this is what we based our TCP server off of also
there is a updated Version at http://www.planet-source-code.com/vb...txtCodeId=1885
&lngWId=10
Its worth a Check out It does have a bug with The Above Code that is now
fixed if you replace with the above example
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ... |
last post by:
Does anyone know a good tutorial on asynchronous programming in .net
AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic
I...
|
by: Noel |
last post by:
Hi,
I am a tad confused about if there are any benefits from using
asynchronous vs synchronous network communication.
As my example, I have been writing a dns lookup stack with a network...
|
by: Leo |
last post by:
Version 7 fixpack 9, aix 5.1
In our database we have a table that that has a dedicated bufferpool
for the data, and another for indexes. Actually we have three of
these combinations and they...
|
by: Natalia DeBow |
last post by:
Hi,
I am working on a Windows-based client-server application. I am involved in
the development of the remote client modules. I am using asynchronous
delegates to obtain information from...
|
by: Julian Hershel |
last post by:
Reading about asynchronous programming
(ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno
verview.htm) I could not clarify some doubts. Hope you can help me.
1) Are...
|
by: usenetaccount |
last post by:
In a newly created test app, to maximize client performance I tried to
make two SOAP method calls in tandem (the soap methods execute some
specified query), as each call includes a large amount of...
|
by: dba123 |
last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method
which does an insert but how can I do this Asyncronously so that it does not
affect the load on our public production...
|
by: Bishoy George |
last post by:
Hi,
I have a asp.net 2.0 web application.
I want to implement the asynchronous model through http handler in
web.config
...
|
by: Morgan Cheng |
last post by:
Since ASP.NET 2.0, asynchronous web service client can be implemented
with event-based pattern, instead of original BeginXXX/EndXXX pattern.
However, I didn't find any material about event-based...
|
by: Nicolas Le Gland |
last post by:
Hello everyone here.
This is my first post in this newsgroup, I hope I won't be to much
off-topic. Feel free to redirect me to any better group.
I am getting strange timing issues when...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |