Hi,
I am creating a web server that will, when finished, serve WAP pages. The problem is when I run what I have so far, I get the 'This program has caused an illegal operation' (invalid page fault in module MSAFD.DLL). Using OllyDbg, I found it happens when the 'accept' function is called. I will post the program below. I've got the feeling that I'm missing something stupidly obvious.


.386
.model flat, stdcall
option casemap:none

incboth macro incl
include E:\masm32\include\incl.inc
includelib E:\masm32\lib\incl.lib
endm
include E:\masm32\include\windows.inc
incboth kernel32
incboth user32
incboth wsock32

addstr macro strng
LOCAL nustr
.data
nustr db strng, 0
.code
exitm <addr nustr>
endm

ServerThread proto :DWORD
ClientThread proto :DWORD

.data
AppName db "JMTServerB", 0

.data?
ThreadID dd ?
ThreadID2 dd ?

.code
start:
invoke CreateThread, 0, 0, ADDR ServerThread, 0, NORMAL_PRIORITY_CLASS, ADDR ThreadID
invoke CloseHandle, eax
invoke MessageBox, 0, addstr("Click OK to stop the server"), ADDR AppName, 0
invoke ExitProcess, 0


ServerThread proc Param:DWORD

LOCAL wsadata:WSADATA
LOCAL sin:sockaddr_in
LOCAL sfr:sockaddr_in
LOCAL hServerSock:DWORD
LOCAL hClientSock:DWORD
invoke WSAStartup, 101h, ADDR wsadata
invoke socket, AF_INET, SOCK_STREAM, 0
mov hServerSock, eax
mov sin.sin_family, AF_INET
invoke htons, 4523
mov sin.sin_port, ax
mov sin.sin_addr.S_un.S_addr, INADDR_ANY
invoke bind, hServerSock, ADDR sin, SIZEOF sin
invoke listen, hServerSock, SOMAXCONN
.while TRUE
invoke accept, hServerSock, ADDR sfr, SIZEOF sfr
mov hClientSock, eax
invoke CreateThread, 0, 0, ADDR ClientThread, hClientSock, NORMAL_PRIORITY_CLASS, ADDR ThreadID2
.endw
invoke closesocket, hServerSock
invoke WSACleanup
ret

ServerThread endp

ClientThread proc Param:DWORD

invoke send, Param, ADDR AppName, SIZEOF AppName-1, 0
invoke Sleep, 2000
invoke closesocket, Param
ret

ClientThread endp

end start

Posted on 2005-08-18 19:49:35 by programinator
The "size" parameter to accept() is not an integer, it's a pointer to an integer. So you must create a variable, and pass "addr sizethingy". In the PlatformSDK it is specified as an parameter, so you need not fill the variable with the struct size before the call.
Posted on 2005-08-18 20:50:54 by f0dder
Changing

invoke accept, hServerSock, ADDR sfr, SIZEOF sfr
to
invoke accept, hServerSock, ADDR sfr, 0
has done it. Thank you f0dder.  :)
Posted on 2005-08-18 22:07:29 by programinator
That should work, too... I'd prefer doing the paranoid version of passing "addr thesize" and "cmp sizeof sfr, thesize", but that's up to you, really...

Rule of thumb when dealing with network programming: NEVER make any assumptions.
Posted on 2005-08-19 01:15:35 by f0dder