News:

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

File sending on the network

Started by akna, February 02, 2010, 05:20:33 PM

Previous topic - Next topic

akna

Hi

Im writing this code but does not work.Always send 3 string.


.386
.model flat, stdcall
option casemap :none 
include windows.inc

includelib kernel32.lib
includelib user32.lib
includelib ws2_32.lib

include kernel32.inc
include user32.inc
include ws2_32.inc

.data
wsaData WSADATA<>
sin     sockaddr_in <>
szIP db "127.0.0.1",0
filename db "c:\listen.pls",0

.data?
sock    dd ?
hfile dd ?
fsize dd ?
hgmem dd ?
nbyte dd ?

.code
start:
invoke CreateFile,addr filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov hfile,eax
invoke GetFileSize,eax,NULL
mov fsize,eax
invoke GlobalAlloc,GMEM_FIXED,size
mov hgmem,eax
invoke ReadFile,hfile,addr hgmem,addr fsize,nbyte,NULL
invoke WSAStartup,0002h,ADDR wsaData
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
mov sock,eax
mov sin.sin_family,AF_INET
invoke htons,1234
mov sin.sin_port,ax
invoke inet_addr,addr szIP
mov sin.sin_addr,eax
invoke connect,sock,addr sin,sizeof sin
invoke send,sock,addr hgmem,sizeof hgmem,0
invoke closesocket,sock
invoke WSACleanup
invoke ExitProcess,0
end start

PBrennick

Shoud this ...

invoke GlobalAlloc,GMEM_FIXED,size

be this?

invoke GlobalAlloc,GMEM_FIXED,fsize

If so make it be fsize+1, also.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

qWord

an other problem:
Quote from: akna on February 02, 2010, 05:20:33 PM
invoke ReadFile,hfile,addr hgmem,addr fsize,nbyte,NULL ;<--- order of parameters is false - nbytes comes first, then 'ADDR fsize'
...
invoke send,sock,addr hgmem,sizeof hgmem,0
sizeof is not an runtime operator - it returns the size of given qualifier. In case of hgmem it returns 4. You must use fsize at this point.
Also you are passing the address to hgmem instead of the content -> remove the ADDR-operator.
FPU in a trice: SmplMath
It's that simple!

akna

Thanx the help,i have done.

This is the correct code:

.386
.model flat, stdcall
option casemap :none 
include windows.inc

includelib kernel32.lib
includelib user32.lib
includelib ws2_32.lib

include kernel32.inc
include user32.inc
include ws2_32.inc

.data
wsaData WSADATA<>
sin     sockaddr_in <>
szIP db "127.0.0.1",0
filename db "c:\listen.pls",0

.data?
sock    dd ?
hfile dd ?
fsize dd ?
hgmem dd ?

.code
start:
invoke CreateFile,addr filename,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov [hfile],eax
invoke GetFileSize,[hfile],NULL
mov [fsize],eax
invoke GlobalAlloc,GMEM_FIXED,[fsize]
mov [hgmem],eax
invoke ReadFile,[hfile],[hgmem] ,[fsize],addr fsize, NULL
invoke CloseHandle,[hfile]
invoke WSAStartup,0002h,ADDR wsaData
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
mov sock,eax
mov sin.sin_family,AF_INET
invoke htons,1234
mov sin.sin_port,ax
invoke inet_addr,addr szIP
mov sin.sin_addr,eax
invoke connect,sock,addr sin,sizeof sin
invoke send,sock,hgmem,[fsize],0
invoke closesocket,sock
invoke WSACleanup
invoke GlobalFree,[hgmem]
invoke ExitProcess,0
end start

hoverlees

You must confirm all bytes are sended out before close the socket.
And I suggest you do not send a big data one time.

Maybe your code at 3# will not send all data to the server when the file is too big or the network is slowly.

japheth

Quote from: hoverlees on February 05, 2010, 06:37:39 AM
Maybe your code at 3# will not send all data to the server when the file is too big or the network is slowly.

AFAIU it is ok because closesocket() does a "graceful shutdown" as default ( see socket option value SO_DONTLINGER ).