Hi!

I try to use a dynamic buffer for recv data from a socket.
Here is what I use:



.data
szIP db "123.123.123.123", 0
nPort dd 2222

szStatus db "1122331122", 0

.data?
wsa WSADATA <>
sin SOCKADDR_IN <>

nSocket dd ?
hMem dd ?
pMem dd ?
nSize dd ?

.code

start:

invoke WSAStartup, 101h, addr wsa

invoke socket, AF_INET, SOCK_DGRAM, 0
mov nSocket, eax

mov sin.sin_family, AF_INET
invoke htons, nPort
mov sin.sin_port, ax
invoke inet_addr, addr szIP
mov sin.sin_addr, eax
invoke connect, nSocket, addr sin, sizeof sin

invoke send, nSocket, addr szStatus, 11, 0

invoke ioctlsocket, nSocket, FIONREAD, addr nSize

invoke GlobalAlloc, GHND, nSize
mov hMem, eax
invoke GlobalLock, eax
mov pMem, eax

invoke recv, nSocket, pMem, nSize, 0

invoke MessageBox, 0, pMem, 0, 0

invoke GlobalUnlock, pMem
invoke GlobalFree, hMem

invoke closesocket, nSocket
call WSACleanup

invoke ExitProcess, 0

end start


But this code always fails :(

If I use a static buffer, it works fine:



.data
szIP db "123.123.123.123", 0
nPort dd 2222

szStatus db "1122331122", 0

.data?
wsa WSADATA <>
sin SOCKADDR_IN <>

nSocket dd ?

buffer db 2048 dup (?)


.code

start:

invoke WSAStartup, 101h, addr wsa

invoke socket, AF_INET, SOCK_DGRAM, 0
mov nSocket, eax

mov sin.sin_family, AF_INET
invoke htons, nPort
mov sin.sin_port, ax
invoke inet_addr, addr szIP
mov sin.sin_addr, eax
invoke connect, nSocket, addr sin, sizeof sin

invoke send, nSocket, addr szStatus, 11, 0

invoke recv, nSocket, addr buffer, 2048, 0

invoke MessageBox, 0, addr buffer, 0, 0

invoke closesocket, nSocket
call WSACleanup

invoke ExitProcess, 0

end start


Anyone has a idea where the problem is?
Posted on 2002-07-12 16:26:31 by bazik
No one? (Thomas where are you??) :(
Posted on 2002-07-12 20:15:10 by bazik
Checking return values from functions would help. :)
Also, invoke MessageBox, 0, pMem, 0, 0. You set caption string to 0?
Posted on 2002-07-12 22:26:13 by comrade
>Checking return values from functions would help. :)

Nah, REAL MEN don't need any error checking code :tongue:


lpCaption
Pointer to a null-terminated string that contains the dialog box title. If this parameter is NULL, the default title Error is used.


:grin:
Posted on 2002-07-12 22:49:08 by bazik
Well then its obvious when you have a dynamic buffer and you IMMEDIATLY query the socket for incoming data right after send there is no data available. With a static buffer, read waits for 2KB of data or until the socket is closed. Solution is to wait some time for data to arrive (when you use ioctlsocket), so I guess even Sleep can do the job.
Posted on 2002-07-12 23:04:32 by comrade
bAZiK, what does ioctlsocket return? SOCKET_ERROR?
Maybe there isnt just any data available yet (which is very likely)
I encountered this problem as well a few years ago.. i also fixed it with a Sleep as comrade suggested.. but thats very bad :)
infact peeking how much data there is avaible with ioctlsocket+FIONREAD is also considered bad.

this should be more proper:
mov bytesread, 1


.while bytesread
invoke recv,hsock,..,..
mov bytesread, eax

;realloc buffersize etc

.endw
Posted on 2002-07-13 11:19:09 by savage
Hi, sorry to hijack this thread, but is this correct?



invoke WSACreateEvent
mov [hEvent], eax
invoke WSAEventSelect, esi, eax, FD_READ or FD_CLOSE
WaitNextEvent:
lea eax, [hEvent]
invoke WSAWaitForMultipleEvents,1,eax,FALSE,-1,FALSE
lea eax, [myevent]
invoke WSAEnumNetworkEvents,esi,[hEvent],eax
test [myevent.lNetworkEvents], FD_READ
jz .notfdread
invoke ioctlsocket,esi,FIONREAD,cbdata
inc eax
jz WaitNextEvent
call [GetProcessHeap]
invoke HeapAlloc,eax,0,[cbdata]
xchg edi, eax
invoke ws_recv, esi, eax, [cbdata],0
inc eax
jz @F
mov ah, [edi+6]
mov al, [edi+7] ;family
movzx eax, ax
mov ch, [edi+8]
mov cl, [edi+9] ;command
movzx ecx, cx
stdcall snac, edi,eax, ecx,[lparam]
@@:
call [GetProcessHeap]
invoke HeapFree, eax, 0, edi
.notfdread:
test [myevent.lNetworkEvents], FD_CLOSE
jz WaitNextEvent
EndEvent:
invoke WSACloseEvent,[hEvent]

This codes sometimes fail and cause a gpf
Posted on 2004-02-26 21:41:54 by roticv
Stupid mistake on my part... Fixed it now..
Posted on 2004-02-28 01:05:34 by roticv