News:

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

Winsock question

Started by Farabi, November 20, 2007, 02:36:19 AM

Previous topic - Next topic

Farabi

I have read madwizard tutorial and I know a bit thing about winsock.
Here is my code.


include ws2_32.inc

includelib ws2_32.lib

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

fCloseInternet proc

invoke closesocket,sock
invoke WSACleanup

ret
fCloseInternet 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
.if eax==0
invoke PERR
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


The problem is, I cannot use send API. Everytime I use API I got this error message: "A request to send or receive data was disallowed because the  socket is not connected". I have use fConnect to connect the sockert and there is no error message. Anyone can explain what is my mistake and maybe guide me?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Tedd

When "connect" returns it does not mean the connection was successful (or failed) - the socket is not connected yet.
When the connection has completed, you will receive FD_CONNECT in your WM_FSOCKET message - it will say whether the connection was successful.
Read and understand ALL of the comments and explanation for "WSAAsyncSelect"
(Or, don't use it, and your socket will be blocking - so it will wait for connect/read/send to finish before returning.)
No snowflake in an avalanche feels responsible.

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Farabi

Mark:
Thanks.

all:
I think I need a example source code of a working connection. anyone can help?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

jj2007

I stumbled over something odd a day ago: I installed a software package that tried to download an extra file, which triggered a message box by the firewall. Fine so far. What is odd is that on the same virgin pc I had installed my own baby (link in the signature), which also downloads files using urlmon.dll when pressing Control F5. No firewall message box!
Of course I am damn proud that I could cheat the firewall so easily, but still I'd like to know how I did it. I am sure MSIE uses urlmon; is it possible that an app "inherits" access rights from MSIE simply because it uses the same dll? If that is true, I would call it a security risk (or is it "by design"?). I know this is off topic but readers of "winsock question" might be in a position to solve the mystery...


Farabi

Is the waiting accept from my code is right?


mov ecx,SOCKET_ERROR
@@:
push ecx
invoke accept,sock,NULL,NULL
pushad
; invoke PERR
popad
pop ecx
cmp eax,ecx
jz @b


Here is the complete code.

fConnect proc uses esi edi lpszHostName:dword,nPortNumber:dword
LOCAL buff[128]:byte

mov sin.sin_family, AF_INET
invoke htons, nPortNumber
mov sin.sin_port,ax
invoke gethostbyname, lpszHostName
.if eax==0
invoke PERR
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
xor eax,eax
mov sin.sin_addr,eax

invoke bind,sock,addr sin, sizeof sockaddr_in
.if eax!=0
invoke PERR
ret
.endif

invoke listen,sock,1
.if eax!=0
invoke PERR
ret
.endif

mov ecx,SOCKET_ERROR
@@:
push ecx
invoke accept,sock,NULL,NULL
pushad
; invoke PERR
popad
pop ecx
cmp eax,ecx
jz @b


; invoke connect,sock,addr sin,sizeof sin
; .if eax==SOCKET_ERROR
; invoke WSAGetLastError         
; .endif


ret
fConnect endp


Here is how I call the function


invoke fConnect,CADD("www.awardspace.com"),80


please help.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

hoverlees

Hi Farabi,
see client.asm!

[attachment deleted by admin]

Farabi

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Hai hoverlees your client.exe is working well, but Im still cannot get it working when I implement it to my code. I sent my full source code to your email, I hope you can read it.
My email account is realvampire2006@yahoo.co.id please check it.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

hi,
I try to get the main page on internet using this command "GET / HTTP/1.0" and its work, but when I send it for the second time I got a message "an established connection was aborted by the software in your host machine". why is that happen? I think last night it didnot appear.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"