Re: Sockets and Notification

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 03/02/04


Date: Mon, 01 Mar 2004 20:27:03 -0500

No need to use threads, events, or anything else that complex. Use CAsyncSocket, and when
your OnReceive handler determines that a complete message has been received, it does a
PostMessage to the GUI thread which contains the necessary information for the message
handler to deal with the message. No polling, no waits, no threads. It is really simple.
Think pure "event-driven" and most of the problems simply evaporate.

Read my essay on worker threads on my MVP Tips site. While you don't actually need a
thread, the technique of using PostMessage would apply to a CAsyncSocket OnReceive
handler.
                                        joe

On Mon, 01 Mar 2004 20:11:47 GMT, "Ali R." <nospam@nospam.com> wrote:

>
>"Trevor" <trevor@nospam.com> wrote in message
>news:OxRvWt7$DHA.552@TK2MSFTNGP11.phx.gbl...
>> "Ali R." <nospam@nospam.com> wrote in message
>> news:ZVJ0c.2970$BI5.884544640@newssvr11.news.prodigy.com...
>> > Hi everyone,
>> >
>> > I have a client/server application talking to each other using streaming
>> > sockets.
>> > I have a class that is the communication class, which owns the socket,
>> > sends, and receives messages.
>> > Anyway, Lets say I want to call a method in the communication class,
>tell
>> > it to send a message to the server and wait for a response.
>> > So the client will send a message to the server. Wait for a message
>from
>> > the server. If it doesn't receive the reply back within 2 or 3 seconds,
>it
>> > raises an error.
>> >
>> > I am having a bit of problem designing the wait part.
>> >
>> > Any ideas would be appreciated
>> >
>> > Thanks
>> > Ali R.
>> >
>> >
>>
>> Here are two solutions:
>>
>> 1) Save the current time to some variable. Periodically subtract the new
>> current time with the old current time to find out the difference. time_t
>> and time() work great for this sort of thing.
>> 2) Use windows events (CreateEvent, ResetEvent, SetEvent, CloseHandle) and
>> wait (WaitForSingleObject) for 3 seconds or until the event is signaled.
>>
>>
>As far as solution 1 goes, I am not really sure where you were going with
>that.
>I myself was looking at solution number 2. Which I guess would require me
>to have a seperate thread for each of the messages that I will be waiting
>on.
>
>Here is what I had come up with,
>
>Create an event
>Then create a worker thread, and pass it the event.
>All the read would do is WaitForSingleObject for 3 or 4 seconds.
>The main thread is still processing messages from the server.
>And once it gets the correct response, it would signal the event.
>now meanwhile the thread is waiting for the event or a timeout,
>and depending on which one he will send an appropriate message back to the
>main thread.
>Now since I don't want my orginal function (with in the main thread) to
>return, I would have to do a message loop until I get something back from
>the worker thread.
>
>This seems like too much. I am hoping to find a better solution than this
>
>Ali R.
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • Re: Must call RemoveHandler after AddHandler?
    ... Marina Levit [MVP] wrote: ... AddHandler is just a programmatic way of attaching the handler ... the user control or form, when that user control or form is gone, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: MFC80+WinHelp
    ... Jochen Kalmbach [MVP] schrieb: ... möchten sie selbst nach ihr suchen?->std handler. ...
    (microsoft.public.de.vc)
  • Re: Receive large data pack by CAsyncSocket
    ... >> It's generally a mistake to issue more than one single call to Receive ... >> from inside the OnReceivehandler for CAsyncSocket. ... >> Issuing more than one Receive call in the OnReceive notification ...
    (microsoft.public.vc.mfc)
  • Re: How to disable the menuitem?
    ... That becomes the condition of your handler. ... Assume there is a boolean expression which I ... the class CWhatever is whatever class knows the menu item should be enabled, ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Disabling / enabling entire menu and toolbar if child window open and has focus
    ... YOu would put an ON_UPDATE_COMMAND_UI handler in whatever ... if you want to override enablement, you would put a handler in your ... all the menus are disabled and the toolbar is ... MVP Tips:http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)