News:

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

winsock problem

Started by cysio, August 29, 2006, 12:25:52 AM

Previous topic - Next topic

cysio

Hi i have code:

.486p
.model flat,stdcall
option casemap:none
include c:\masm32\include\windows.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\wsock32.inc
include c:\masm32\include\kernel32.inc

includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\wsock32.lib
includelib c:\masm32\lib\kernel32.lib

      WSAStartup PROTO wVersionRequired:DWORD, LPWSADATA:DWORD

.data
ClassName db "Kunthrandam",0
AppName   db "SPARCz",0
blad1 db "Blad tworzenia WINSOCKA",0
blad2 db "Blad tworzenia SOCKETA",0
blad3 db "Blad wyboru trybu socketa",0

wsadata WSADATA <>

.data?
;sock dd ?
sock SOCKET ?
hwnd dd ?

.const
WM_SOCKET equ WM_USER+100    ; create a custom window message. You can use any name not
                                                                     ; limited to WM_SOCKET and you can use any number not
                                                                     ; limited to 100.

.code

start:


invoke WSAStartup, 101h,addr wsadata    ;startuje z wsa
.if eax!=NULL
;    dostalem errora  dalej nie idziemy panowie
mov eax,offset blad1
jmp error

.else

; -------- TWORZE SOCKETA -----


invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov ax,AF_INET
    mov sock,eax ; SOCKET UTWORZONY
.else
    invoke WSAGetLastError
    ;  BLAD TWORZENIA SOCKETA
    mov eax,offset blad2
    jmp error
.endif

; -------- TWORZE SOCKETA ----


; -------- USTAWIAM SOCKETA ----
invoke WSAAsyncSelect, socket, hwnd,WM_SOCKET, FD_CONNECT+FD_READ+FD_CLOSE


.if eax==SOCKET_ERROR
    mov eax,offset blad3
    jmp error
.endif
; --_----- USTAWIAM SOCKETA
;    <The initialization is successful. You may proceed with other winsock calls>
.endif





;_----ERROR
error: ;jesli napotkam errora
;nic nie robie
invoke MessageBox, NULL,eax,eax, MB_OK
ret
;_----ERRROR


end start


But why i see error WSAasync ? pls help me

Synfire

WM_SOCKET needs to be within a WinProc. As for the WSAAsyncSelect() error, you don't have a window. You must have a window to recieve the WM_SOCKET messages. You're current program is using an undefined hwnd, this should be the return value of a CreateWindowEx call (not an unitialized value). Try reading the following pages.

http://win32assembly.online.fr/asmsockguide.html
http://www.madwizard.org/view.php?page=tutorials.networking.contents&lang=asm
http://www.megasecurity.org/Sources/asm_server.htm
http://www.megasecurity.org/Sources/asm_client.htm

Regards,
Bryant Keller

ToutEnMasm

Hello,

I cannot say you what is wrong in your code but i can show you a different method that use winsock2,replace only winsock by invoke and don't take care of this.


;========================================================================
;                           ConnectSocket
;========================================================================
;  Here we create our socket, we initialize it to non-blocking mode
;  convert the Provider URL with htons, and get in contact with the
;  remote Host - then we wait for the FD_READ.
;========================================================================
ConnectSocket proc _THIS:DWORD
Local addrinfo:ADDRINFOA
Local ppaddrinfo:DWORD
Local adrsockaddr_in:DWORD
Local retour:DWORD
ZEROLOCALES retour

mov retour,0 ;
;pas d'autres choix
invoke socket, PF_INET, SOCK_STREAM,0
.if eax == INVALID_SOCKET
Winsock ShowError,SADR ("socket") ; why did the socket creation fail?
jmp FindeConnectSocket
.endif
mov paramail.IDsock,eax

mov addrinfo.ai_family,PF_INET
mov addrinfo.ai_socktype,SOCK_STREAM
mov addrinfo.ai_protocol,NULL ;IPPROTO_TCP
invoke getaddrinfo,addr paramail.popserveur,addr paramail.numeroport,addr addrinfo,addr ppaddrinfo
.IF eax != 0
Winsock ShowError,SADR ("getaddrinfo")
Winsock CloseMySocket          ; no host? why did we fail? anyway... show & close
jmp FindeConnectSocket
.ENDIF
mov eax,ppaddrinfo
mov eax,(ADDRINFOA ptr [eax]).ai_addr
mov adrsockaddr_in,eax
;C:\Program Files\Microsoft Platform SDK\Include\WinSock2.h
;When one of the nominated network events occurs on the specified
;socket s, the application window hWnd receives message wMsg.
;The wParam parameter identifies the socket on which a network
;event has occurred. The low word of lParam specifies the network
;event that has occurred. The high word of lParam contains
;any error code. The error code be any error as defined in
;Winsock2.h.




;---------- instal the WM_SOCKET message --------------
invoke WSAAsyncSelect,paramail.IDsock,HwndMere,WM_SOCKET,\  ;Setting up what messages we're interested in
FD_CONNECT or FD_READ or FD_CLOSE

.IF eax==NULL  ;successfull
invoke connect,paramail.IDsock,adrsockaddr_in, SIZEOF sockaddr_in ;ADDR paramail.SOCKADDR_IN adrsockaddr_in
.IF eax != 0 ;SOCKET_ERROR
invoke WSAGetLastError
.IF eax!=WSAEWOULDBLOCK
Winsock ShowError,SADR ("connect")
Winsock CloseMySocket          ; no host? why did we fail? anyway... show & close
.ENDIF
.ENDIF
mov retour,1
.ELSE
Winsock ShowError,SADR ("WSAAsyncSelect")
Winsock CloseMySocket          ; no host? why did we fail? anyway... show & close
.ENDIF
FindeConnectSocket:
mov eax,retour
ret
ConnectSocket endp
;################################################################