having a problem writing the received data to file
if you compile and execute
blah.exe 66.94.234.13 index.html
(ip is yahoo.com)
it'll scroll the source of index.html into console but the file does writeout properly
yea i know i should change the host from 0.0.0.0
but for now im only concerned about the data writing to file



.386
.model flat, stdcall
option casemap: none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\shell32.inc
include \masm32\include\wsock32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\wsock32.lib
includelib \masm32\lib\masm32.lib


.data
usage      db "                  Assembly WebGetter",13,10,\
              "                      by: illwill ",13,10,13,10,\
              "Usage: webget.exe <ip> <filename>",13,10,\
              "Example: webget 10.0.0.2 index.html",13,10,0
szHost      db "Cannot connect to %s.",0
GetTemplate db "GET /%s HTTP/1.0",0dh,0ah
            db "User-Agent: ASM-WEB-GET",0Dh,0Ah
            db "Host: 0.0.0.0:0",0Dh,0Ah
            db "Connection: Keep-Alive",0Dh,0Ah
            db "Accept: */*",0Dh,0Ah,0dh,0ah,0 
RcvError  db "Socket Error When Receiving Data.",0Dh,0Ah,0       
.data?
IPAddress  db 32 dup(?)
szFile      db 128 dup(?)
Getbuffer  db 512 dup(?)
buffer      db 64 dup(?) 
sock        dd ?
sin        sockaddr_in <;?>
wsadata    WSADATA <;?>
buff_sock  db 128 dup (?)
hFile      dd ?
fwritten  dd ?
.code
start:     
        invoke GetCL, 1, addr IPAddress
        invoke lstrlen,addr IPAddress
          .IF eax>;500
            invoke ExitProcess,1
          .ELSEIF eax==0
            invoke StdOut,addr usage
          invoke ExitProcess,0
          .ENDIF
        invoke GetCL, 2, addr szFile
invoke WSAStartup, 101h, offset wsadata
    cmp eax, 0
    jne start
invoke socket,AF_INET,SOCK_STREAM,0         
      mov sock,eax
      mov sin.sin_family, AF_INET
      invoke htons, 80         
      mov sin.sin_port,ax         
      invoke inet_addr, addr IPAddress
      mov sin.sin_addr,eax
      invoke connect,sock,addr sin,sizeof sin
          .if eax==SOCKET_ERROR 
              invoke wsprintf,addr buffer,addr szHost,addr IPAddress
              invoke StdOut,addr buffer
              jmp Err
          .else
                invoke wsprintf,addr Getbuffer,addr GetTemplate,addr szFile
                invoke lstrlen,addr Getbuffer
                invoke send,sock,addr Getbuffer,eax,0      ; Send GET command to the web server
                invoke StdOut, addr Getbuffer
                invoke CreateFile,addr szFile, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
                mov hFile, eax
              _recvLoop:
                invoke  recv, , addr buff_sock, sizeof buff_sock, 0
              test eax, eax
            mov ecx, offset RcvError
              jz Err
              cmp eax, SOCKET_ERROR
            je _error
      mov , 0
          invoke StdOut, addr buff_sock
              invoke WriteFile, hFile, addr buff_sock, sizeof buff_sock, addr fwritten, 0
              jmp _recvLoop
          .endif

Err:
  invoke CloseHandle, hFile
  invoke WSACleanup
  invoke ExitProcess,0

_error:
invoke StdOut, ecx
  xor ebx, ebx
  jmp Err 
end start
Posted on 2005-03-16 01:06:22 by illwill
You should check how many bytes are in the rec buffer before you write to file.
Apart from that, if you only want to fetch files from web, may as well use wininet.
Much more simple to code and quick results, with less control over the transfer.
The choice is yours, it depends on what your motivation is.
Posted on 2005-03-17 16:22:44 by Homer
well using
mov  , 0
          invoke StdOut, addr buff_sock

stdout shows the html output no problem but the writefile isnt writing it
i figure this is a good way to learn winsock stuff.. urldownloadtofile is just as simple but i like the harder way
Posted on 2005-03-18 23:11:27 by illwill
I'm glad you like the harder way.
My suggestion is to include the ATC macros and my FStream incluide.
Ask me for my most recent version.
It has a Put method which is basically a proc with two params: ptr to data and sizeof data.
It wraps all the disk access stuff to make life a breeze.
FStream is a blend of the IFStream and OFStream classes.
Using oop can be easy and fun, and make asm feel like vb, if you want it to :)
Posted on 2005-03-20 07:57:31 by Homer
Hi,
  Like Iczleion says in his tut, use ioctlsocket with the option as FIONREAD to query the no. of bytes to be read. Then use this value to alloc mem using GlobalAlloc,LocalAlloc whatever. Then Write this buffer to file and free this buffer.

Thomas Antony :P
Posted on 2005-03-28 04:27:56 by thomasantony