Hi all,
I wrote a little program to check if netmeeting is running. This is a client/server program. The client request the server for the answer. the server has no visible window. The first problem is that, for each client connexion, the memory size of the program in the process list is growing up :confused:
Is there any mistake in the code ? The second problem is to know if I can do the same program without WinMain and WndProc ?
This the code:
Many thx for your help :grin:
I wrote a little program to check if netmeeting is running. This is a client/server program. The client request the server for the answer. the server has no visible window. The first problem is that, for each client connexion, the memory size of the program in the process list is growing up :confused:
Is there any mistake in the code ? The second problem is to know if I can do the same program without WinMain and WndProc ?
This the code:
.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\wsock32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\wsock32.lib
.data
ClassName db "DLGCLASS",0
DlgName db "Form1",0
Request db "GET",0
hSock1 dd 0
.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hDlg dd ?
hMem dd ?
Received dd ?
hSnapshot dd ?
RSBuf db 4 dup(?)
wc WNDCLASSEX <>
wsadata WSADATA <>
msg MSG <>
SA sockaddr_in <>
uProcess PROCESSENTRY32 <>
.const
WM_SOCKET equ WM_USER+16
Netmeeting db "conf.exe",0
Msg_OK db "Netmeeting is RUNNING",0
Msg_NOK db "Netmeeting is NOT RUNNING",0
.code
;########################################################
start:
;########################################################
push NULL
call GetModuleHandle
mov hInstance,eax
call GetCommandLine
mov CommandLine,eax
push NULL
push CommandLine
push NULL
push hInstance
call WinMain
push eax
call ExitProcess
;########################################################
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:dword
;########################################################
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.lpszClassName,offset ClassName
mov wc.lpszMenuName,NULL
invoke WSAStartup,101h,offset wsadata
invoke RegisterClassEx,offset wc
invoke CreateDialogParam,hInstance,offset DlgName,NULL,NULL,NULL
mov hDlg,eax
.while TRUE
invoke GetMessage,offset msg,NULL,NULL,NULL
.break .if (!eax)
push offset msg
call TranslateMessage
push offset msg
call DispatchMessage
.endw
call WSACleanup
mov eax,msg.wParam
ret
WinMain endp
;########################################################
WndProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
;########################################################
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
mov hSock1,eax
mov SA.sin_family,AF_INET
mov SA.sin_addr.S_un.S_addr,INADDR_ANY
invoke htons,7777
mov SA.sin_port,ax
invoke WSAAsyncSelect,hSock1,hWnd,WM_SOCKET,FD_ACCEPT
invoke bind,hSock1,offset SA,sizeof SA
invoke listen,hSock1,100
.if uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.elseif uMsg==WM_SOCKET
mov eax,lParam
and eax,0FFFFh
.if ax==FD_ACCEPT
invoke accept,wParam,NULL,NULL
invoke WSAAsyncSelect,eax,hWnd,WM_SOCKET,FD_READ or FD_CLOSE
.elseif ax==FD_READ
invoke recv,wParam,offset RSBuf,sizeof RSBuf,NULL
;########################################################
invoke lstrcmp,addr Request, offset RSBuf
.if eax==0
mov [uProcess.dwSize], sizeof uProcess
invoke CreateToolhelp32Snapshot, TH32CS_SNAPPROCESS, 0
mov [hSnapshot], eax
invoke Process32First, eax, addr uProcess
.while eax
invoke lstrcmp,addr uProcess.szExeFile, addr Netmeeting
.if eax==0
invoke send,wParam,offset Msg_OK,sizeof Msg_OK,0
ret
.endif
invoke Process32Next, [hSnapshot], addr uProcess
.endw
invoke CloseHandle, [hSnapshot]
invoke send,wParam,offset Msg_NOK,sizeof Msg_NOK,0
.endif
;########################################################
.elseif ax==FD_CLOSE
invoke closesocket,wParam
.endif
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
;########################################################
end start
;########################################################
;########################################################
Many thx for your help :grin:
WinMain is just a convention - you don't really need it. Nothing stops you from skipping the "wndmain" proc and doing everything in your "start" label.
If you don't need a GUI (and don't need to receive messages), you don't need a wndproc - this does also mean you can use WSAAsyncSelect, since you have no hwnd.
About the memory use... is this a one-time hit when a client connects, or does it keep growing and growing and growing when clients connect? Does it return to normal when a client exits (or perhaps after some time)? Try making your app connect a couple of thousand times and see how the memory usage is affected.
I wouldn't be surprised if there's a - perhaps even substantial - one-time hit (for various technical reasons I wont write here, unless you ask me to). Also, there ought to be some per-client overhead... however, if per-client overhead doesn't disappear when clients disconnect, something's wrong.
If you don't need a GUI (and don't need to receive messages), you don't need a wndproc - this does also mean you can use WSAAsyncSelect, since you have no hwnd.
About the memory use... is this a one-time hit when a client connects, or does it keep growing and growing and growing when clients connect? Does it return to normal when a client exits (or perhaps after some time)? Try making your app connect a couple of thousand times and see how the memory usage is affected.
I wouldn't be surprised if there's a - perhaps even substantial - one-time hit (for various technical reasons I wont write here, unless you ask me to). Also, there ought to be some per-client overhead... however, if per-client overhead doesn't disappear when clients disconnect, something's wrong.
Here is the client code:
I don't need WSAAsynchSelect if there is no WndProc ?
.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\ws2_32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\ws2_32.lib
.data
AddrIP db "xxx.xxx.xxx.xxx",0
Request db "GET",0
wsadata WSADATA <>
sin sockaddr_in <>
.data?
sock dd ?
buffer db 255 dup (?)
.code
start:
invoke WSAStartup,0002h,offset wsadata
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_IP
mov sock,eax
mov sin.sin_family,AF_INET
invoke htons,7777
mov sin.sin_port,ax
invoke inet_addr,offset AddrIP
mov sin.sin_addr,eax
invoke connect,sock,addr sin,sizeof sin
invoke send,sock,offset Request,sizeof Request,0
invoke recv,sock,offset buffer,sizeof buffer,0
invoke MessageBox, NULL, addr buffer, NULL, MB_OK
invoke closesocket,sock
call WSACleanup
invoke ExitProcess,eax
end start
I don't need WSAAsynchSelect if there is no WndProc ?
You cannot use WSAAsynchSelect if there's no wndproc :)
You never need WSAAsynchSelect, wndproc or not - it's just one method of doing socket programming (and inefficient, says the resident MadWizard). There's plenty of different models, see www.madwizard.org .
You never need WSAAsynchSelect, wndproc or not - it's just one method of doing socket programming (and inefficient, says the resident MadWizard). There's plenty of different models, see www.madwizard.org .
Ok, thx, I will try to rewrite the server without WSAAsyncSelect ;)
And I will see if the memory problem appears again.....
Thx,
Eric
And I will see if the memory problem appears again.....
Thx,
Eric
Ah, I misunderstood your question - I thought you were connecting to a netmeeting server.
By a quick skim of your code, things seem to look okay. You closehandle the toolhelp snapshot, and you closesocket the socket. no other dynamic memory going on. I might have missed something, but it appears ok.
Doesn't netmeeting have some window, though? Something you could check instead of using toolhelp32? Matter of taste, I guess.
Memory problem shouldn't be because of asyncselect, so you shouldn't rewrite just because of that.
I will have a look at the memory usage of the server app and see if it's something that should be done something about. Oh, when posting code snippets, use the {code} and {/code} tags (replace {} with [])
By a quick skim of your code, things seem to look okay. You closehandle the toolhelp snapshot, and you closesocket the socket. no other dynamic memory going on. I might have missed something, but it appears ok.
Doesn't netmeeting have some window, though? Something you could check instead of using toolhelp32? Matter of taste, I guess.
Memory problem shouldn't be because of asyncselect, so you shouldn't rewrite just because of that.
I will have a look at the memory usage of the server app and see if it's something that should be done something about. Oh, when posting code snippets, use the {code} and {/code} tags (replace {} with [])
Ok, but I would like to do the same thing without any GUI, just a running process. Have any sample code of a server without GUI ? So, if I understood well, without WSAAsyncSelect.... ? :)
Well, for the "FindWindow" thing I meant instead of toolhelp looking for conf.exe, perhaps you could look for a window - but again, it shouldn't really matter, it's more of a personal preference of staying away from toolhelp/psapi unless I have to use them :).
Humm, your program doesn't immediately work on my machine... seems like the client app either gets an empty string back, or perhaps fails connecting. Also, you don't do much error checking, and your socket code isn't 100% correct - go read www.madwizard.org winsock programming stuff, async socket handling requires a bit more than your current code :)
Humm, your program doesn't immediately work on my machine... seems like the client app either gets an empty string back, or perhaps fails connecting. Also, you don't do much error checking, and your socket code isn't 100% correct - go read www.madwizard.org winsock programming stuff, async socket handling requires a bit more than your current code :)
Ok, but have you a sample code of a listenning server without GUI and without WSAAsyncSelect ? To make a hide process..... please :grin:
Please read http://www.asmcommunity.net/board/index.php?topic=12659
This makes it easier on all of us
Thanks,
This makes it easier on all of us
Thanks,