title: read source of a webpage in fasm or nasm
body: <p>I have this c++ code to read the source of a webpage.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;winsock2.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #pragma comment(lib,"ws2_32.lib")
  
  using namespace std;
  
  int main(){
      WSADATA wsaData;
  
      if (WSAStartup(MAKEWORD(2, 2), &amp;wsaData) != 0) {
          cout &lt;&lt; "WSAStartup failed.\n";
          system("pause");
          return 1;
      }
  
      SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      struct hostent *host;
      host = gethostbyname("www.last.fm");
  
      SOCKADDR_IN SockAddr;
      SockAddr.sin_port = htons(80);
      SockAddr.sin_family = AF_INET;
      SockAddr.sin_addr.s_addr = *((unsigned long*)host-&gt;h_addr);
  
      cout &lt;&lt; "Connecting...\n";
      if (connect(Socket, (SOCKADDR*)(&amp;SockAddr), sizeof(SockAddr)) != 0){
          cout &lt;&lt; "Could not connect";
          system("pause");
          return 1;
      }
      cout &lt;&lt; "Connected.\n";
  
      send(Socket, "GET /music/Taylor+swift/+albums?order=reach&amp;page=1 HTTP/1.1\r\nHost: www.last.fm\r\nConnection: close\r\n\r\n", strlen("GET /music/taylor+swift/+albums?order=reach&amp;page=1 HTTP/1.1\r\nHost: www.cplusplus.com\r\nConnection: close\r\n\r\n"), 0);
      char buffer[10000];
  
      int nDataLength;
      while ((nDataLength = recv(Socket, buffer, 10000, 0)) &gt; 0){
          int i = 0;
          while (buffer[i] &gt;= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
               cout &lt;&lt; buffer[i];         
              i += 1;
          }
      }
  
      closesocket(Socket);
      WSACleanup();
  
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>I tried to convert this to fasm assembly but it did not work. could someone help me convert it to assembly? thanks (note I have never worked with sockets in asm before so I am not sure if this code is close or not, I think it connects, but it gives a blank messagebox instead of the webpage sourcecode)</p>
  
  <pre><code>format PE GUI 4.0
  entry start
  
  include '\Fasm\INCLUDE\win32ax.inc'
  
  section '.data' data readable writeable
    IPPROTO_TCP  = 6
  
  
  wsadata WSADATA
   _caption db 'Client application',0
   _igang db 'The client has started very well.',13,10,'It is now going to connect to your own computer',0
   _hostname db 'Wrong hostname',0
  
    hostname db 'www.lastfm.com',0
    hSock dd ?
    saddr sockaddr_in
    sizesaddr = $-saddr
  
  
    buffer rb 0x3000
  
    sender db 'GET /music/Taylor+swift/+albums?order=reach&amp;page=1 HTTP/1.1\r\nHost: www.last.fm\r\nConnection: close\r\n\r\n',13,10
           rb 0x100
  
  section '.code' code readable executable
  start:
  
         invoke WSAStartup,0101h,wsadata  ; initialiserer winsock-bibliotek
  
         invoke  ws_gethostbyname,hostname
         or     eax,eax
         jz     bad_hostname
                virtual at eax
                 .host hostent
                end virtual
         mov    eax,[.host.h_addr_list]
         mov    eax,[eax]
         mov    eax,[eax]
         mov     [saddr.sin_addr],eax
         invoke MessageBox,0,_igang,_caption,0
         mov    al,00
         mov    ah,80          ; port 80
         mov     [saddr.sin_port],ax
         mov     [saddr.sin_family],AF_INET
         invoke  ws_socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
         mov     [hSock], eax
         xchg    eax, esi
         invoke  ws_connect, esi, saddr, sizesaddr
         .if eax = 0
         invoke MessageBox,0, "connected", _caption,0
         .endif
         .if eax &lt;&gt; 0
         invoke MessageBox,0, "not connected", _caption,0
         .endif
         mov     ebx, buffer
         invoke  ws_send,esi,sender,109,0
         invoke  ws_recv, esi, ebx, 1000, 0
         invoke MessageBox,0, buffer, _caption,0
         .connectSucceeded:
         invoke ws_closesocket,esi
         invoke WSACleanup
         jmp stopp
  
  bad_hostname:
          invoke MessageBox,0,_hostname,_caption,0
          jmp stopp
  
  stopp:
          invoke ExitProcess,0
  
  
  
  section '.idata' import data readable writeable
  
    library kernel,'KERNEL32.DLL',\
            winsock,'WSOCK32.DLL',\
            user,'USER32.DLL'
  
    import kernel,\
           ExitProcess,'ExitProcess'
  
    import winsock,\
          WSAStartup,'WSAStartup',\
          ws_socket,'socket',\
          ws_connect,'connect',\
          ws_gethostbyname,'gethostbyname',\
          ws_send,'send',\
          ws_recv,'recv',\
          ws_closesocket,'closesocket',\
          WSACleanup,'WSACleanup'
  
    import user,\
          MessageBox,'MessageBoxA'
  </code></pre>