473,382 Members | 1,302 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,382 software developers and data experts.

Asynchronous TCP/IP

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 ?
Nov 20 '05 #1
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]
Nov 20 '05 #2

"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
Nov 20 '05 #3
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
Nov 20 '05 #4

"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.
Nov 20 '05 #5
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]
Nov 20 '05 #6
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!
Nov 21 '05 #7

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

Similar topics

6
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...
1
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...
2
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...
1
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...
1
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...
3
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...
1
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...
0
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 ...
4
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...
2
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...
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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
0
BarryA
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...
1
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...
0
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...

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.