I'm working on a project in which I want my GUI applicaiton to be able to send data to a browser in HTML format. I've read through the tutorials written by Thomas and am using the source code hosted at http://www.sockaddr.com/ as a starting point, but I have run into a problem.

If I launch my program and access it through a browser everything works fine. If I were to then close my program, relaunch it, and refresh my browser I get a FD_WRITE message prior to a FD_READ message. My program doesn't know what to write (since, as far as it knows, there wasn't a read beforehand) and the browser is waiting for some content without resending a FD_WRITE message. What do I do in this case?

The only thing I thought of is to repost the FD_WRITE message (using PostMessage) and hope that a FD_READ is somewhere in the queue before I encounter it again. Is this acceptable?

Thanks for your help.
Spara
Posted on 2006-11-20 12:40:36 by Sparafusile
Hmm, this sounds a bit peculiar.

Can you post the program (and source) here? Would be interesting playing around a bit with this, including perhaps some traffic sniffing.

Ultimately I'd advise you to move away from WSAAsyncSelect - then again, it's not that big of an issue if you don't plan on having a lot of simultaneous connections.
Posted on 2006-11-20 17:27:38 by f0dder
The following procedure may be useful to you.


; Since there may be WM_SOCKET messages left in the message queues,
; we must get rid of them before we create new socket.
ClearMessages PROC
LOCAL MyMSG:MSG
mov eax,TRUE
.while eax==TRUE                                       
invoke PeekMessage,addr MyMSG,hWnd,WM_SOCKET,WM_SOCKET,PM_REMOVE
.endw
ret
ClearMessages ENDP

Posted on 2006-11-20 22:08:49 by Homer
I know this is an assembly forum, but in the interest of brevity and readability I rewrote part of my application in C.  The source is attached (thanks Spook).

When I launch the program, start the server, access the program from a browser (http://localhost:8081/ in my case) I will get something like the following output:

Starting server on port 8081...
  Connection accepted!
    FD_READ message
    FD_WRITE message
    FD_READ message
    FD_WRITE message
  Connection closed!
  Connection accepted!
    FD_READ message
    FD_WRITE message
    FD_READ message
    FD_WRITE message
  Connection closed!
  Connection accepted!
    FD_WRITE message
    FD_READ message
  Connection closed!
  Connection accepted!
    FD_WRITE message
    FD_WRITE message
    FD_READ message
  Connection closed!
  Connection accepted!
    FD_WRITE message
    FD_READ message
  Connection closed!
  Connection accepted!
    FD_READ message
    FD_WRITE message
    FD_READ message
    FD_WRITE message
  Connection closed!
  Connection accepted!
    FD_READ message
    FD_WRITE message
  Connection closed!
  Connection accepted!
    FD_READ message
    FD_WRITE message
    FD_READ message
    FD_WRITE message
  Connection closed!
Server stopped.


As you can hopefuly see there are several times when I get a FD_WRITE prior to an FD_READ after connecting. Please shed some light on what I'm doing wrong.

Edit: I took Homer's advice and cleared the buffer before creating a new socket, but didn't have any effect.

Spara
Attachments:
Posted on 2006-11-21 11:14:46 by Sparafusile
For future reference, any significant and/or large block of code, such as the one in the previous post, should be attached to the thread as a text file instead.

People tend to ignore posts with large chunks of code in them, and it makes the thread harder to read and flow thorugh. Thanks. -SpooK
Posted on 2006-11-21 13:20:06 by SpooK
As I see it, FD_WRITE just means "you can write something now" - and it seems like you might need to rework your design a bit (haven't looked at the C source yet). So basically, on FD_WRITE, try sending data if you got some, otherwise ignore the message (not sure when it will be re-posted, though, so you might need to keep a "is writable" state flag).
Posted on 2006-11-21 17:19:47 by f0dder