News:

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

WSAAsyncSelect Problem

Started by Bros, June 16, 2006, 02:49:36 PM

Previous topic - Next topic

Bros

Hello

I'm new in this forum and i have a little problem in my asm program.

I'll trying to do a tcp/ip server but i never receive the message from the WM_SOCKET using the WSAAsyncSelect api.

I d'ont found the problem because the application is working (client can connect) but no message are seen.

Could some body help me?

Thanks in advance and sorry for my poor english...  :wink

;--------------------------------------------------------------------------------
;
; TODO :  Connexion Serveur
;
;--------------------------------------------------------------------------------

.386
.model flat,stdcall
option casemap:none

;-- Macro -------------------------------------------------------------------
      return MACRO arg
        mov eax, arg
        ret
      ENDM
     
;-- Includes ----------------------------------------------------------------

include windows.inc
include user32.inc
include kernel32.inc
include advapi32.inc
include comctl32.inc
include comdlg32.inc
include ws2_32.inc

;-- Libraries ---------------------------------------------------------------

includelib user32.lib
includelib kernel32.lib
includelib advapi32.lib
includelib comctl32.lib
includelib comdlg32.lib
includelib ws2_32.lib

;-- Prototypes fonctions ----------------------------------------------------

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
Connect proto :HWND

;-- Constantes --------------------------------------------------------------

.const
IDI_ICON equ 100
MAINDLG equ 1000
IDC_GROUPBOX1002 equ 1002
IDC_EDIT1003 equ 1003
IDC_SBR1 equ 1004
IDC_OPEN equ 1001
MAXSIZE equ 260
WM_SOCKET equ WM_USER+100

;-- Variables initialisees --------------------------------------------------

.data
;MainName db "MAINDLG",0
ClassName db "MAINCLASS",0
AppName db "AutoTest",0
ofn OPENFILENAME <>
wsaData WSADATA <>
sin sockaddr_in <>
sincli sockaddr_in <>
FilePath db MAXSIZE dup(0)
FilterSelect db "Log Files",0,"*.log",0,0
F_Open db "Fichier Ouvert",0
SockErr db "Erreur de création du socket",0
StrList db "En attente ...",0
StrConn db "Connecté ...",0
Port dd 20000

;-- Variables non initialisees ----------------------------------------------

.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hFile HANDLE ?
hStatus dd ?
listSocket SOCKET ?
cliSocket SOCKET ?
temp dd ?

;------------  Debut programme ----------------------------------------------

.code
start:
invoke InitCommonControls

invoke GetModuleHandle, NULL
mov    hInstance,eax

invoke GetCommandLine

invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT

invoke ExitProcess,eax

;-------------------------------- WinMain -----------------------------------

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hDlg:HWND

mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,DLGWINDOWEXTRA
push  hInst
pop   wc.hInstance
mov   wc.hbrBackground,COLOR_BTNFACE+1
    mov   wc.lpszMenuName,NULL
mov   wc.lpszClassName,OFFSET ClassName

invoke LoadIcon,hInst,IDI_ICON
mov   wc.hIcon,eax
mov   wc.hIconSm,eax

invoke LoadCursor,NULL,IDC_ARROW
mov   wc.hCursor,eax

invoke RegisterClassEx, addr wc
invoke CreateDialogParam,hInstance,MAINDLG,NULL,NULL,NULL
mov   hDlg,eax

    INVOKE ShowWindow, hDlg,SW_SHOWNORMAL
INVOKE UpdateWindow, hDlg

      .WHILE TRUE
                INVOKE GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
                invoke IsDialogMessage, hDlg, ADDR msg
                .if eax==FALSE
                        INVOKE TranslateMessage, ADDR msg
                        INVOKE DispatchMessage, ADDR msg
                .endif
.ENDW

mov     eax,msg.wParam
ret
WinMain endp

;-------------------------------- WinProc -----------------------------------

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

LOCAL sbParts[2] :DWORD

.IF uMsg == WM_CREATE
invoke CreateStatusWindow,WS_CHILD+WS_VISIBLE,NULL,hWnd,IDC_SBR1
mov hStatus,eax
mov sbParts,300
    mov [sbParts +  4], -1
    invoke SendMessage,hStatus,SB_SETPARTS,2,ADDR sbParts
    invoke Connect,hWnd
.ELSEIF uMsg == WM_CLOSE || uMsg == WM_DESTROY
invoke WSACleanup
    invoke PostQuitMessage,NULL
    return 0
.ELSEIF uMsg==WM_COMMAND
mov eax,wParam
.IF eax==IDC_OPEN
invoke SetDlgItemText,hWnd,IDC_EDIT1003,NULL ;Boite "Ouvrir..."
mov ofn.lStructSize,SIZEOF ofn
        push hWnd
pop ofn.hWndOwner
push hInstance
pop ofn.hInstance
mov ofn.lpstrFilter,OFFSET FilterSelect
  mov ofn.lpstrFile, OFFSET FilePath
  mov ofn.nMaxFile,MAXSIZE
            invoke GetOpenFileName,ADDR ofn
            .IF eax==TRUE
            invoke CreateFileA,offset FilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,NULL
                cmp eax,0FFFFFFFFh
                jz open_err
                mov hFile,eax
                invoke SendMessage,hStatus,SB_SETTEXT,1,ADDR F_Open ;Fichier ouvert dans la status bar
                invoke SetDlgItemTextA,hWnd,IDC_EDIT1003,ADDR FilePath
            .ENDIF
        .ENDIF
.ELSEIF uMsg==WM_SOCKET           
        mov eax,lParam
        .IF ax==FD_CONNECT           
            shr eax,16
            .IF ax==NULL
                ;Aucune erreur ne s'est produite donc on continue
                stFD_CONNECT db "FD_CONNECT",0
                invoke SendMessage,hStatus,SB_SETTEXT,0,ADDR StrConn
            .ELSE
                ;Une erreur s'est produite. Mettez votre routine de traitement d'erreurs ici
            .ENDIF
        .ELSEIF ax==FD_READ
            shr eax,16
            .IF ax==NULL
            ;Aucune erreur ne s'est produite donc on continue
            stFD_READ db "FD_READ",0
                invoke MessageBox,hWnd,ADDR stFD_READ,ADDR AppName,MB_OK
            .ELSE
                ;Une erreur s'est produite. Mettez votre routine de traitement d'erreurs ici
            .ENDIF
        .ELSEIF ax==FD_CLOSE
            shr eax,16
            .IF ax==NULL
            stFD_CLOSE db "FD_CLOSE",0
                invoke MessageBox,hWnd,ADDR stFD_CLOSE,ADDR AppName,MB_OK
                ;Aucune erreur ne s'est produite donc on continue
            .ELSE
                ;Une erreur s'est produite. Mettez votre routine de traitement d'erreurs ici
            .ENDIF
        .ENDIF
open_err:
invoke CloseHandle,hFile

.ELSE
    invoke DefWindowProc,hWnd,uMsg,wParam,lParam
.ENDIF

ret

WndProc endp


Connect proc hWnd:HWND

invoke WSAStartup,202h,addr wsaData
invoke socket, AF_INET, SOCK_STREAM,0
cmp eax, INVALID_SOCKET
je socket_error
mov listSocket, eax
mov ax,AF_INET
    mov sin.sin_family,ax
    invoke htons,Port
    mov sin.sin_port,ax
    mov eax,INADDR_ANY
    mov sin.sin_addr,eax
    invoke WSAAsyncSelect, listSocket, hWnd,WM_SOCKET, FD_CONNECT+FD_READ+FD_CLOSE
cmp eax, SOCKET_ERROR
je socket_error
invoke bind,listSocket,ADDR sin,SIZEOF sockaddr_in
cmp eax, SOCKET_ERROR
je socket_error
invoke listen, listSocket,1
cmp eax, SOCKET_ERROR
je socket_error
invoke SendMessage,hStatus,SB_SETTEXT,0,ADDR StrList
jmp fin
socket_error:
invoke MessageBox, NULL, addr SockErr, addr AppName,MB_ICONERROR
invoke WSACleanup
fin:

ret

Connect endp

end start

;------------  Fin programme ------------------------------------------------

Eugen

 Hi Bros

I admit i am too lazy to check the code  ::). However, recently i've posted an application that uses asynchronous sockets, you can check it out to see a working example:

http://www.masm32.com/board/index.php?topic=4928.0

Eugen

Bros

Hello Eugen..

I have already see your application and I have compared your code with mine and I have found no error...

I keep your code, and see tomorrrow to implement it in my code if there is a difference.

Thanks for the reply


James Ladd

The client can connect because you have bound the socket (bind), however you then do a
listen but I think you should try and accept first, to accept the connection which will then
give you the socket handle to listen on.

Bros

Hello James

If you look at Iczelion tutorials, you see that he use bind and in the source of eugen too.

http://spiff.tripnet.se/~iczelion/asmsockguide.html

Accept "lock" my prog till no body connect, wssasynselect is more "windows" and multitask I think.

Thanks for the answer

Tedd

bind allocates an address and port for the socket on the local machine. This is necessary for listening sockets to be able to actually have a 'location' to receive on.
listen puts a bound socket into listening mode so that it can receive connections.
accept will accept an incoming connection on a listening socket, if there is one waiting to be accepted.

The functions must be called in this order for it to work. WSAAsyncSelect only changes the way you are notified of these events and whether or not they block, it doesn't change the way they should be used.

[James, please check your advise before posting it :wink]



I'll take a closer look at your code later. For now, a quick glance doesn't show anything obvious - except that you have data that will be executed as code (silly mistake) -- "stFD_CONNECT", "stFD_READ" and "stFD_CLOSE"
No snowflake in an avalanche feels responsible.

P1

Bros,

Welcome Aboard !!!   :U

'Search' and Google are your programming friends.   :dance:

Put sure to go through the Help and tutorials for a jump start on experinced programming.    :cheekygreen:

Regards,  P1  :8)

Tedd

I'm afraid there were quite a few little mistakes in your code, so instead of trying to point them all out, and correct them, etc, I rewrote the important parts into a new program (attached) -- study that to see how it works.
Then, for you own program, start again, but this time do only one thing at a time. So that means make sure the sockets and connections works first, before adding file handling. Then when each part works, you can move on to the next one :U


[attachment deleted by admin]
No snowflake in an avalanche feels responsible.

Bros