News:

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

winsock simple application won`t work - please help!

Started by Zap, May 23, 2009, 02:46:26 PM

Previous topic - Next topic

Zap

Just following Iczelion`s tutorial,checked 100 times,but the connect func wont work, returns  "Bad address.The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr)."

The code:

.DATA
bIPAddress db  "127.0.0.1",0                                         
wPort      dw   80
ws  WSADATA <>
sin sockaddr_in  <>

.data? 
dSock dd  ?
------------
              invoke WSAStartup,101,addr ws
.if eax
              MsgBox "WSAStartup failed.",0
              invoke ExitProcess, NULL
.endif   
 
              invoke socket,PF_INET,SOCK_STREAM,0     
             
.if eax == INVALID_SOCKET 
              MsgBox "socket failed.",0
              jmp EXIT
.endif             
         
              mov dSock,eax
             
              mov sin.sin_family, AF_INET
              invoke htons, wPort                   
              mov sin.sin_port,ax                 
              invoke inet_addr, addr bIPAddress   
              mov sin.sin_addr,eax
              invoke connect,dSock,addr sin,sizeof sin 
.if eax != 0
              invoke WSAGetLastError
              MsgBox "connect failed with error - %d.",eax
              jmp EXIT         
.endif               

              MsgBox "connect OK.",0


ToutEnMasm


Quote
invoke WSAStartup,202h,ADDR  something

Bad version, 202h instead of 101


dedndave

i found quite a few of Iczelions examples not to work properly
i think many of the problems are library differences
that may give you a clue where to look

ToutEnMasm


Bad libraries could be good answer.
I have write an anti-spam and to make it work i have dowloaded dynamically the ws2_32.dll.

dedndave

funny you mention that file
a few days ago, i had a spammer program hooked in - it replaced ws2_32.dll somehow
but - it acted halfway like a virus, not just ad-ware
i would go to a site for yahoo, and it would take me to msn - very weird
malwarebytes anti-malware did not find it
but, gmer told me which file it was
i booted up in safe mode and expanded the one from I386 folder and the problem was gone
(replaced the one in system32 and system32\dllcache)

ToutEnMasm


here is a file to make a dynamic load of the dll ws2_32.dll
take care with the  WSADATA structure



[attachment deleted by admin]

Tedd

I think the problem will come from your call to "htons" -- with wPort being a word, but invoke likes to push dwords for parameters (there's an masm bug, of which this may be a case.)
Also, you'll have trouble connecting to localhost unless you actually have a program waiting to receive connections - but then you'd get "connection refused."
Anyway, a working example is attached - have a play with it.


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

Zap

You`re right,Tedd,it started working  OK when i changed wPort to dPort.Point is i got confused by the MSDN,it says htons takes a 16-bit number as an argument,whereas wsock32.inc  clearly defines it as htons PROTO :DWORD.That`s weired.
Anyway,thanx a lot,should`ve looked better myself.

Tedd

It's not entirely weird, just something you need to watch out for.
(Almost) All arguments to API functions are taken as dwords, no matter what their actual size/type should be.
So, although htons takes a 16-bit argument, it does so in the form of a dword with its range limited to 16-bits (because that's the valid range of port numbers.) Equally, 4 byte arguments would be pushed as 4 separate dwords with the lowest byte set to the correct value in each, and not simply 1 dword containing the 4 bytes.
So for htons, you still only give 16 bits, but inside a dword (the upper half is ignored.)
No snowflake in an avalanche feels responsible.