• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

CSocket Getting Network failures

I've got a Server app where n clients can connect. I'm using a CMySocket class (derived from CSocket). I've implemented the CMySocket::OnClose( int nErrorCode ) handler. there i got notifications whenever the client or the server are gone (not normally terminated)
My question: how can i get a notification when i.e. the network is down? i did the following: on computer B I run
a client and connected it to the Server (running on comp. A) on comp. A I pulled out the network connector (simulate network failure). None of my two callback functions (either OnReceive(int nErrorCode) nor OnClose( int nErrorCode ) have been called. so how can i get notified about this event?
  • 2
1 Solution

I did the same thing as you did, except I derived from class CAsyncSocket.  When I broke the network connection, function OnClose was called.
I'm not sure why it wouldn't do the same for any class derived from CSocket.

Could you post your class MySocket definition (if it isn't exceedingly large) so we can take a look?
mruffAuthor Commented:
hello VEngineer,
here's the class interface definition:

// clntsock.h : interface of the CClientSocket class

#ifndef __CLNTSOCK_H__
#define __CLNTSOCK_H__

class CMsg;
class CWallPanelServerDoc;

class CClientSocket : public CSocket
      CClientSocket(const CClientSocket& rSrc);         // no implementation
      void operator=(const CClientSocket& rSrc);  // no implementation

// Construction
      CClientSocket(CWallPanelServerDoc* m_pDoc);

// Attributes
      int m_nMsgCount;
      CSocketFile* m_pFile;
      CArchive* m_pArchiveIn;
      CArchive* m_pArchiveOut;
      CWallPanelServerDoc* m_pDoc;
      BOOL IsAborted() { return m_pArchiveOut == NULL; }

// Operations
      void Init();
      void Abort();
      void SendMsg(CMsg* pMsg);
      void ReceiveMsg(CMsg* pMsg);

// Overridable callbacks
      virtual void OnReceive(int nErrorCode);
      virtual void OnClose( int nErrorCode );

// Implementation
      virtual ~CClientSocket();

#ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;


#endif // __CLNTSOCK_H__
I think that the problem lies in the implementation of class CSocket (which is beyond your control).  CSocket switches between non-blocking (asyncronous) and blocking mode (syncronous) under certain circumstances.  This blocking in class CSocket means that the socket keeps waiting for a message and does not send a notification until it recieves one (and it doesn't since it disconnects).

What you have to do is either fool around with CSocket's attributes until you get it to do pure non-blocking, or an easier solution, derive your class from CAsyncSocket, which is known to work properly for your desired application.

I don't work with CSocket a lot, so I'm not sure how to resolve your problem if you want to modify CSocket attributes.  Maybe someone else out here can help you with that if that is your desired track.

If you have further questions, go ahead and stick more comments to the end of this and I'll try to answer them as best I can.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now