keep getting 'Connection timed out' errors with this sockets code.

Started by Rainstorm, September 15, 2008, 10:36:16 PM

Previous topic - Next topic

Rainstorm

Hi,

I keep getting "Connection timed out" errors with this code of mine while trying to connect to sites - why is that ?
Also, can someone confirm for me whether i need bswap or not after using invoke inet_addr
to make sure the ip numbers are in the proper order & not reversed, & that its getting used correctly & not reversed in the  connect function.
this byte order thing kinda confuses me.
I think i don't need bswap to correct it.. so i've commented the line out in the code.

many thanks.
-

    .data

        winsock2_info      WSADATA       <>

    align 4
        server_socket      sockaddr_in   <>
;       server_ipaddress   in_addr       <>

    align 4

        local_socket             dd   0

        sockcreation_error_code  dd   0

        server_socket_name       db   "server_socket",0

        site_ipaddress           db   "209.85.173.103",0


;; 209.85.173.103  64.233.187.99  207.46.16.248  207.46.197.32
       
; -----------------------------------------------------------------------
    .code

start:


    ; --------------------------------------------
    ;   initialize winsock2
    ; -------------------------------------------- 
 
      mov bl, 2
      mov bh, 2

      invoke WSAStartup, ebx, addr winsock2_info
     

    ; -----------------------------------------------
    ;   Create a socket
    ;  ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    ;   local_socket  =  unconnected socket
    ; -----------------------------------------------

      invoke socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
      cmp eax, INVALID_SOCKET
      je @F                                 ; if there is an error in creating the socket

      mov local_socket, eax                 ; returns a descriptor referencing the new socket


    ; ---------------------------------------------------------------------
    ;   connect to the server
    ;  ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    ;   connects to the socket on the server
    ;   The sockaddr_in structure specifies the address family, IP address,
    ;   and port of the server to be connected to.
    ; ----------------------------------------------------------------------

    ; === fill in the sockaddr.in structure ======

      mov server_socket.sin_family, AF_INET          ; Internet address family
      mov server_socket.sin_port, 80                 ; port number associated with the server that the client will connect to

      invoke inet_addr, addr site_ipaddress          ; converts into binary 32 bit form
      cmp eax, INADDR_NONE                           ; check for a bad IP address
      je bad_ip_address
;     bswap eax                                      ; reverses order d.c.b.a --> a.b.c.d
      mov server_socket.sin_addr.S_un, eax           ; remote IP address of the server that
                                                     ; the client will connect to.

      movzx ebx, byte ptr [server_socket.sin_addr.S_un]
      fn MessageBox, 0, ustr$(ebx),"site address", MB_OK

     
    ; =============================================

  connect_to_server:
   
      invoke connect, local_socket, addr server_socket, sizeof server_socket

      cmp eax, SOCKET_ERROR              ; eax = 0 = no error 
      je connect_error                   ; eax = SOCKET_ERROR

      fn MessageBox, 0, "Connected to the Server !","Successfull Connection", MB_OK
      jmp exit_


  connect_error:
      invoke WSAGetLastError                      ;  connection refused
          cmp eax, WSAECONNREFUSED
          jne @F
      fn MessageBox, 0, "Connection Refused", "connect function - Error", MB_OK
;         jmp connect_to_server
          jmp exit_         

      @@:                   
          cmp eax, WSAENETUNREACH                 ;  network is unreachable
          jne @F
      fn MessageBox, 0, "Network is unreachable", "connect function - Error", MB_OK
;         jmp connect_to_server
          jmp exit_
         
      @@:   
          cmp eax, WSAETIMEDOUT                   ;  connection timed out
      fn MessageBox, 0, "connection timed out", "connect function - Error", MB_OK

;         jmp connect_to_server
          jmp exit_


  bad_ip_address:
     
      fn MessageBox, NULL, addr site_ipaddress, "Bad IP Address", MB_OK
      jmp exit_

  socket_creation_error:
      invoke WSAGetLastError
      mov sockcreation_error_code, eax
      fn MessageBox, NULL, ustr$(sockcreation_error_code), "Socket Creation Error", MB_OK
      jmp exit_

  exit_:

      invoke WSACleanup
      invoke ExitProcess, 0

end start



six_L

Quote  mov server_socket.sin_port, 80                 ; port number associated with the server that the client will connect to
==>
   invoke   htons,80
   mov   server_socket.sin_port,ax
;-----------------------------------------------------
; EDITTEXT IDC_SERVER, 63, 5, 116, 12
      invoke   RtlZeroMemory,addr server_socket,sizeof server_socket
      invoke   GetDlgItemText,hWinMain,IDC_SERVER,addr @szBuffer,sizeof @szBuffer
      invoke   inet_addr,addr @szBuffer
      .if   eax ==   INADDR_NONE
         jmp   _Error
      .endif
      mov   server_socket.sin_addr,eax
;----------------------------------------------------------
;CONTROL "",IDC_SERVER,"SysIPAddress32",0x50010000,140,2,70,14,0x00000000

   invoke   GetDlgItem,hMain,IDC_SERVER
   mov   ebx,eax
   invoke   SendMessage,ebx,IPM_GETADDRESS,0,addr @ip
   mov   eax,@ip         
   bswap   eax
   mov   server_socket.sin_addr,eax


regards

Rainstorm

whoooo!! thanks that worked!!  :U . . .& it connected.

just some related questions though.....

in the sdk i found this related to network & host byte order.. - but it talks about bits being reversed. . . what does that mean ? isn't byte-order supposed to refer to the bytes being reversed , & not the bits.. which are supposed to be in the same order ? (as this quote seems to suggest) th title of the page is 'LPM Byte Reordering'.

many thanks.

QuoteThe RSVP protocol submits policy information in network-byte order, as illustrated in the following example:
----------------------
0 1 2 3 4 5 6 7
----------------------
As you can see from the example, in network-byte ordering, the bits in any given byte are ordered such that the first bit (base zero) is bit zero, and the following bits are ordered sequentially through the last bit (bit position seven, base zero).

The Microsoft LPM, Msidlpm.dll, reorders these bits in host-byte order. This is an important distinction because bit-based flags can be used to designate certain values in the PCM-LPM interaction. The following example illustrates host-byte ordering:
-----------------------
7 6 5 4 3 2 1 0
----------------------
Host-byte ordering begins with the last bit in the byte (bit position seven, base zero) and progresses sequentially through the byte to bit position zero.