我有这个 c++ 代码来读取网页的源代码。
#include "stdafx.h"
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "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->h_addr);
cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
return 1;
}
cout << "Connected.\n";
send(Socket, "GET /music/Taylor+swift/+albums?order=reach&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&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)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
system("pause");
return 0;
}
我试图将其转换为 fasm 程序集,但没有成功。有人可以帮我把它转换成汇编吗?谢谢(注意我以前从未在 asm 中使用过套接字,所以我不确定这段代码是否关闭,我认为它连接了,但它给出了一个空白消息框而不是网页源代码)
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&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 <> 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'