CAsyncSocket and broken network cable

Posted on 1998-06-10
Last Modified: 2013-11-20
I have created a client/server application using CAsyncSocket class.
It works very fine. But my question is :

1°) I make a connection to the server side.
2°) When the connection is establish, I 'cut' the physical network cable.
3°) How can these applications know that the connection is broken ?

Because when I send a packet to the other side (with CAsyncSocket::Send), the function
returns OK, send succefull.

How to know it ?

Thanks for response
Question by:mike_marquet
  • 5
  • 2
LVL 32

Accepted Solution

jhance earned 20 total points
ID: 1317821
CAsyncSocket::Send() will only return an error if you are trying to send to an invalid socket or otherwise pass it bad parameters.  Since you have validly opened the CAsyncSocket, Send() believes it's OK to accept the data.  It has no way of knowing that you have cut the cable.  The only way to detect this problem is to use a timer.  After some reasonable amount of time (which you determine), if the OnSend() callback has not been received, then you determine that the connection has failed and notify the user, retry, give up, or whatever corrective action you decide to use.

Author Comment

ID: 1317822
But there is an other error, when I send my data, the OnSend handler is never posted !
But the remote computer has received the datas.
I become an OnSend data the first time the socket is connected to remote computer and that of both side.
Why ?

Must I set an option some where, and if yes where ?

LVL 32

Expert Comment

ID: 1317823
I don't understand.  How can your remote computer receive the data if you've cut the network cable?  Please explain.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 32

Expert Comment

ID: 1317824
I don't understand.  How can your remote computer receive the data if you've cut the network cable?  Please explain.
LVL 32

Expert Comment

ID: 1317825
You haven't posted any code but, in order to use the OnSend virtual function of CAsyncSocket, you must derive your own class from CAsyncSocket so that you can define your own OnSend class.  You then would add an instance of myCAsyncSocket to your application and it's OnSend member function would get called.
LVL 32

Expert Comment

ID: 1317826
BTW, is there a particular reason why you are using CAsyncSocket instead of CSocket?  I find that it's often easier to use the blocking CSocket calls and put them in a thread and then use WaitforSingleObject on the thread than to manage CAsyncSocket callback messages.  It gives you greater control over what you are waiting for.  If you put your Send in a thread, the call will hang until the data is sent.  Now your WaitForSingleObject can wait on the thread until it completes or your timeout happens.  If a timeout occurs, you can assume the network is down and cancel the send operation and recover grecefully.

Author Comment

ID: 1317827
When the cable is broken, the send function says that he has send data succefully.
It seams that the socket hasn't sea that the connection is broken.
He sends the data, but I never became an error ? (such as OnSend with error code).

When I says I using CAsyncSocket, I meen that I have derived from this class.
Replace all CAsyncSocket by CMyAsyncSocket.

I'm using CAsyncSocket for the callbacks functions and for non blocking socket.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
Looking for a specific application/software 2 114
sumNumber challenge 16 138
JQuery serialize and unserialize 8 135
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question