Re: Sockets and Notification
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 03/02/04
- Next message: Joseph M. Newcomer: "Re: popup blocker for IE"
- Previous message: Joseph M. Newcomer: "Re: Sockets and Notification"
- In reply to: Ali R.: "Re: Sockets and Notification"
- Next in thread: Joseph M. Newcomer: "Re: Sockets and Notification"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Joseph M. Newcomer: "Re: popup blocker for IE"
- Previous message: Joseph M. Newcomer: "Re: Sockets and Notification"
- In reply to: Ali R.: "Re: Sockets and Notification"
- Next in thread: Joseph M. Newcomer: "Re: Sockets and Notification"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|