News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Internet question

Started by Farabi, May 29, 2007, 11:18:35 AM

Previous topic - Next topic

Farabi

How to connect to a website and reading its content (The HTML code).


WM_FSOCKET equ WM_USER + 0fh



.data
wsadata WSADATA <>
sin sockaddr_in <>
.data?
sock dd ?
error_code dd ?
.code

fInitInternet proc hWnd:dword
; Mad wizard tutorial

invoke WSAStartup,101h,addr wsadata
.if eax!=0
xor eax,eax
dec eax
ret
.endif


invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov sock,eax
invoke WSAAsyncSelect,sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
.else
invoke MessageBox,hWnd,CADD("Internet connection initialization error"),CADD("Unknown cause"),MB_OK
.endif


ret
fInitInternet endp

fConnect proc uses esi edi lpszHostName:dword,nPortNumber:dword

mov sin.sin_family, AF_INET
invoke htons, nPortNumber
mov sin.sin_port,ax
invoke gethostbyname, lpszHostName
mov eax,[eax+12]

mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
mov eax,[eax]                      ; copy IP address into eax
mov sin.sin_addr,eax

invoke connect,sock,addr sin,sizeof sin
.if eax==SOCKET_ERROR
invoke WSAGetLastError
.endif
ret
fConnect endp


I tried to connect and it is connected, but I cannot read or write anything. Anyone can help me to understand it?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Ghirai

You need to make a request to get the page.

As for sending, you probably want to look at POST/GET methods.
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

Farabi

Quote from: Ghirai on May 29, 2007, 01:33:00 PM
You need to make a request to get the page.

As for sending, you probably want to look at POST/GET methods.

I dont get it. Can you give me an example code?
Why I cant use the send function?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Ghirai

Yeah you use send, but you need to make a request.
The server doesn't send you anything by itself.

Look at RFC2616.

To point you in the right direction, here's the request my browser makes when accessing my site (you will need to send something similar):

QuoteGET / HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; Konqueror/3.5; FreeBSD) KHTML/3.5.6 (like Gecko)
Accept: text/html, image/jpeg, image/png, text/*, image/*, */*
Accept-Encoding: x-gzip, x-deflate, gzip, deflate
Accept-Charset: iso-8859-1, utf-8;q=0.5, *;q=0.5
Accept-Language: en
Host: ghirai.com
Connection: Keep-Alive

The server should respond with code 200, followed by some info, and finally the html or whatever the content is.
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

Farabi

Is what Im I doing was right?

First I initialize the internet and get the socket using this code.

fInitInternet proc hWnd:dword
; Mad wizard tutorial

invoke WSAStartup,101h,addr wsadata
.if eax!=0
xor eax,eax
dec eax
ret
.endif


invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP     ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov sock,eax
invoke WSAAsyncSelect,sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
.else
invoke MessageBox,hWnd,CADD("Internet connection initialization error"),CADD("Unknown cause"),MB_OK
.endif




ret
fInitInternet endp


Second, I connect the socket using this code


fConnect proc uses esi edi lpszHostName:dword,nPortNumber:dword

mov sin.sin_family, AF_INET
invoke htons, nPortNumber
mov sin.sin_port,ax
invoke gethostbyname, lpszHostName
.if eax==0
ret
.endif
mov eax,[eax+12]

mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
mov eax,[eax]                      ; copy IP address into eax
mov sin.sin_addr,eax

invoke connect,sock,addr sin,sizeof sin
.if eax==SOCKET_ERROR
invoke WSAGetLastError
.endif
ret
fConnect endp


After I connected what I must do next is sending the request message? Is it right?

Sorry for reasking, Im confused.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Tedd

When you connect successfully you will receive a FD_CONNECT message (but check the error code!!) - then you should send your html request..

Example:
min_req db "GET / HTTP/1.1",13,10
        db "Host: www.yahoo.com",13,10
        db "Connection: close",13,10
        db "User-Agent: Farabi-test/0.1",13,10
        db 13,10
len_req = $ - OFFSET min_req

..this would be if you connected to "www.yahoo.com" (you should reaplce it with the site you're connecting to)..
"invoke send, sock,ADDR min_req,len_req,NULL"
No snowflake in an avalanche feels responsible.