Question about the proper use of CAsyncSocket 
Author Message
 Question about the proper use of CAsyncSocket

Hi all!

I am using MFC's CAsyncSocket class, trying to handle the situation where a
server (happens to be a message logger) crashes and is brought up a while
later.  I do not want to lose any messages from my numerous client
applications.  I have had quite a bit of success so far, but am not sure why
I am not receiving an OnConnect notification when the server is connected a
second time (bear with me, I will explain).

In the following sequence the calls are to classes derived from
CAsyncSocket.  The sequence I have had success with is:

Client                                               Server
------                                               -------
Create()                                            Create()
                                                         Listen()
Connect()-------------------------->OnAccept()
OnConnect()<-----------------------     Accept()

After the above sequence, I am up and running just fine.  Note that
OnConnect() notification seems to be triggered either by the Connect()
call iteself or by the Accept that is done by the Server.  I am not sure
which.  Messages can be sent and received, etc.  I want to simulate a
power out on the server side, so I abruptly terminate (kill) the server
process.  When I do so, the client process receives an OnClose()
notification (perfect!).  In my Client's OnClose() method, I call Close,
which closes the socket.

If I then restore the server client, the following sequence occurs:

Client                                               Server
------                                               -------
Create()                                            Create()
                                                         Listen()
Connect()-------------------------->OnAccept()
                                                             Accept()

BUT, I get two problems:  1.) I never receive an OnConnect() notification on
the client side
                                         2.) my server process seems to hang

Since I am setting a state variable in my OnConnect notification handler,
I end up in a bad state.

What am I not understanding/doing wrong????

    -- Dave



Sat, 17 Dec 2005 23:27:45 GMT  
 Question about the proper use of CAsyncSocket

Quote:

> Hi all!

> I am using MFC's CAsyncSocket class, trying to handle the situation where a
> server (happens to be a message logger) crashes and is brought up a while
> later.  I do not want to lose any messages from my numerous client
> applications.  I have had quite a bit of success so far, but am not sure why
> I am not receiving an OnConnect notification when the server is connected a
> second time (bear with me, I will explain).

> In the following sequence the calls are to classes derived from
> CAsyncSocket.  The sequence I have had success with is:

> Client                                               Server
> ------                                               -------
> Create()                                            Create()
>                                                          Listen()
> Connect()-------------------------->OnAccept()
> OnConnect()<-----------------------     Accept()

> After the above sequence, I am up and running just fine.  Note that
> OnConnect() notification seems to be triggered either by the Connect()
> call iteself or by the Accept that is done by the Server.  I am not sure
> which.  Messages can be sent and received, etc.  I want to simulate a
> power out on the server side, so I abruptly terminate (kill) the server
> process.  When I do so, the client process receives an OnClose()
> notification (perfect!).  In my Client's OnClose() method, I call Close,
> which closes the socket.

> If I then restore the server client, the following sequence occurs:

> Client                                               Server
> ------                                               -------
> Create()                                            Create()
>                                                          Listen()
> Connect()-------------------------->OnAccept()
>                                                              Accept()

> BUT, I get two problems:  1.) I never receive an OnConnect() notification on
> the client side
>                                          2.) my server process seems to hang

> Since I am setting a state variable in my OnConnect notification handler,
> I end up in a bad state.

> What am I not understanding/doing wrong????

>     -- Dave

One possibility to check: If you are reusing the same client socket
object, try creating a new one each time instead.  I.e. use new and
delete so you get a new CAsyncSocket object for each connection.  The
CAsyncSocket::ShutDown documentation implies that reusing a closed
socket can be a problem.

Another possibly-helpful technique: Before calling Close, do this to
flush out any queued data:

int nbytes;
BYTE buf[4096];
CAsyncSocket::ShutDown(1); // SD_SEND
do { nbytes = CAsyncSocket::Receive(buf, 4096);

Quote:
} while (nbytes != SOCKET_ERROR && nbytes > 0);

CAsyncSocket::Close();

--
Scott McPhillips [VC++ MVP]



Sun, 18 Dec 2005 09:14:52 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Proper way to deal with a CAsyncSocket?

2. why i can connect server by using CSocket , and it failed when using CAsyncSocket

3. Why i can connect server using CSocket , but fail by using CAsyncSocket

4. proper way of releasing all objects and used resources when exiting MFC app

5. proper programming style: 2 questions

6. Proper Group for Custom Control Questions?

7. VC5 Question: Is strange getline() behavior proper?

8. Problem using MFC CAsyncSocket in ATL Service project

9. Bi-directional File Transfers Using CAsyncSockets

10. transfer rate using CASyncSocket

11. Using CAsyncSocket in multiple threads

12. Ping using CAsyncSocket

 

 
Powered by phpBB® Forum Software