Can somebody help: CAsyncSOcket class (Client-server networking)

ms visual cpp , casyncsocket class , clientserver networking , cpp York, United Kingdom
  • 12 years ago
    Can anyone help me! I have been developing a Client-Server networking application. In the server application, I have defined a class CMyCAsyncSocket as follows: class CMyCAsyncSocket : public CAsyncSocket { : private: //mine CDialog* m_pWnd; public: //mine void SetParent(CDialog *pWnd) { m_pWnd = pWnd; } protected://mine virtual void OnAccept(int iErrorCode) { if (iErrorCode == 0){//Successful Accept() ((CMyCAsyncSocket*)m_pWnd)->OnAccept(iErrorCode); } else exit(-1); } virtual void OnConnect(int iErrorCode) { if (iErrorCode == 0){ ((CMyCAsyncSocket*)m_pWnd)->OnConnect(iErrorCode); } else exit(-1); } virtual void OnSend(int iErrorCode) {//Successful Send() if (iErrorCode == 0){ ((CMyCAsyncSocket*)m_pWnd)->OnSend(iErrorCode); } else exit(-1); } virtual void OnReceive(int iErrorCode) { if (iErrorCode == 0){ ((CMyCAsyncSocket*)m_pWnd)->OnReceive(iErrorCode); } else exit(-1); } virtual void OnClose(int iErrorCode) { if (iErrorCode == 0){ ((CMyCAsyncSocket*)m_pWnd)->OnClose(iErrorCode); }//if ((CMyCAsyncSocket*)m_pWnd)->Close(); exit(-1); //end this application } }; In the server/client application, I inserted the following statements: CMyCAsyncSocket m_sMySocket; //for my network connection : M_sMySocket.SetParent(this); //as the book of mine instructs me to do!(don’t know why though!”) I have these event functions defined in the CMAsyncSocket class to ensure that, if the network connection ends on the other side (i.e. the client side) for the reason that the client application has crashed without calling Close( ), the event functions of the server notice it and exit( ) call in them will end the server application too. Unfortunately or strangely, when I ran the both of the client and server applications and test it, it just did not happen! What have I done wrong? I know the theory of TCP/IP protocol. My understanding is that , if a network connection on one side ends (whether the application of that side ends its side of the network properly calling Close( ) or it has just crashed terminating itself), the other side will notice it in a while and will close its side of the network connection too and any attempt to use the network connection by Send, Receive, and etc in the application after the close of the network connection will cause errors. If this is correct, I just can not understand why my testing has proved me wrong! Can anybody enlighten me on this issue. Looking forward to hearing from you guys! Thanks Chong.
  • 12 years ago
    Hm.. wouldn't it be easier to keep register of "sessions" at server side, and behave as php does? It means that every session would have expiration time, and every client-access would shift this variable, and check if it is not too late... and also some "global" (not client-access initiated) routine would than check all active sessions for expiration, and if expired, it would "kill unbint listener"...
  • 12 years ago
    But maybe you are correct ... but I doubt if code you snipped could clear out, what conception of such "optimal" server system you meant (or how do you want to implement such listener death)
  • 12 years ago
    Hi Chong, I've not done a practical exercise on socket programming yet(maybe this college term!), but I can say something: How you can convert 'mpWnd' to 'CMyCAsyncSocket' or a pointer of that type, since the type of 'mpWnd' should be CDialog. I think you should call something like this: 'CAsyncSocket::OnAccept(iErrorCode)' that is calling the base class method. For the exit part, maybe you can use some other functions (like OnOK or EndDialog) to do so.
  • 12 years ago
    Dear Mohammad I have tried as you suggested but it has made no difference. For a testing, I have inserted "AfxMessageBox("In now"); in these functions to see if these event notification functions are called at all while the application is running. I must have done something wrong!! Becase thorougout the testing, none of these evnet notification functionns has been called. Help Mohammad! Best regards Chong
  • 12 years ago
    To everyone who used to be encourged to make contributions to this forum, this new desgin of their webpages and restructuring of their services are very disappointing to me, I regret!! I can not even find a way to convey this view of mine to those who run and manage this website, for which I have been grateful for a long time!
  • 11 years ago

    I have found a solution for it myself reading the Help manual. The manual says that the notification functions (i.e. OnReceive, OnSend and etc) of the class CAsyncSocket must be overridden but does not say what happens if you do not override them with your own. With the class CSocket, you don't have to override them. The notification functions of CSocket default to do nothing. What I overlooked was that Rdeceive() function returns zero and Send() returns SOCKET_ERROR when the network connection has been closed. When I incooperated this to my server program, it worked perfectly - when a client crashed, I could make the server exit or quit!!

    Somehing which I have found odd is that the notification functions of the class CAsyncSocket too seem to default to do nothing!

  • 11 years ago

    I have found a solution for it myself reading the Help manual. The manual says that the notification functions (i.e. OnReceive, OnSend and etc) of the class CAsyncSocket must be overridden but does not say what happens if you do not override them with your own. With the class CSocket, you don't have to override them. The notification functions of CSocket default to do nothing. What I overlooked was that Rdeceive() function returns zero and Send() returns SOCKET_ERROR when the network connection has been closed. When I incooperated this to my server program, it worked perfectly - when a client crashed, I could make the server exit or quit!!

    Somehing which I have found odd is that the notification functions of the class CAsyncSocket too seem to default to do nothing!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou