1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, winsock, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
function INetAddr(const Host:string):integer;
function SendData(socket:integer; data:pointer; datalen:integer):integer;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.SendData(socket:integer; data:pointer; datalen:integer):integer;
var
p:pchar;
l:integer;
begin
p:=data;
// l:=datalen;
result:=0;
repeat
l:=send(socket,p,datalen,0);
if l<=0 then begin
result:=l;
exit;
end;
inc(result,l);
inc(p,l);
dec(datalen,l);
until datalen=0;
end;
function TForm1.INetAddr(const Host:string):integer;
var
pHost:PChar;
HostEnt:PHostEnt;
begin
if Host='' then begin // juste au cas ou ...
Result:=INADDR_NONE;
end else begin
pHost:=PChar(Host);
Result:=inet_addr(pHost); // est-ce une adresse au format x.y.z.w ?
if Result=INADDR_NONE then begin
HostEnt:=gethostbyname(pHost); // est-ce un nom d'hote ? (résolution DNS)
if HostEnt<>nil then Result:=integer(pointer(HostEnt^.h_addr^)^);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var sock: Tsocket;
inaddr: TInAddr;
in_addr: TSockAddrIn;
wsdata: TWSAData;
BufferOUT, BufferIN, line: string;
//BufferIN: array [0..2048] of char;
i, j, z: integer;
test: pointer;
begin
Memo1.Lines.Clear;
BufferOUT := '';
BufferIN := '';
//ZeroMemory(@BufferIN[0], SizeOf(BufferIn));
WSAStartUp($101, wsdata);
try
in_addr.sin_family := AF_INET;
in_addr.sin_port := htons(StrToInt(Edit2.Text));
in_addr.sin_addr.S_addr := INetAddr(Edit1.Text);
if in_addr.sin_addr.S_addr=INADDR_NONE then Memo1.Lines.Add('No address specified');
Sock := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
IF connect(sock, in_addr, sizeof(in_addr)) = Socket_Error then Memo1.Lines.Add('Connection Error: ' + Edit1.Text + ' on port ' + Edit2.Text + ' error nb: ' + IntToStr(WSAGetLastError));
BufferOUT := 'GET http://www.google.com/intl/fr/about.html HTTP/1.0'#13#10 +
'Host: www.google.com'#13#10 +
'Accept: text/plain'#13#10 +
'Accept-Language: en'#13#10 +
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
'Keep-Alive: 300'#13#10 +
'Connection: Keep-Alive'#13#10
+ #13#10;
{'GET / HTTP/1.1'#13#10 +
'Host: www.google.com'#13#10 +
'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'#13#10 +
'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'#13#10 +
'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'#13#10 +
'Accept-Encoding: gzip,deflate'#13#10 +
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'#13#10 +
'Keep-Alive: 300 '#13#10 +
'Connection: keep-alive'#13#10 +
'Cookie: PREF=ID=9d67be77ff4ba6ae:TM=1214847376:LM=1214847376:S=UEWn7F9t4WKLaZ0z; NID=14=JXX5RlGUB811OBkY0dqv96UndbWKmCe_OgG1AQ3MXg49QjTS5Tmdz6YlNq5jrzhuSo_IOY-zy9lms-DTC6C1Wz67CSgTiw-uzmdr-iWgsr58E0V0fjzGLYZLe-WRrxO8'#13#10
+ #13#10; }
z:=length(BufferOut);
test := @BufferOut[1];
i := send(sock, pchar(BufferOut)^, z, 0);
//i := SendData(sock, test, z);
if i <= 1 then Memo1.Lines.Add('Oops');
sleep(500);
SetLength(BufferIN, 1024);
//test := @BufferIN[1];
For z := 1 to 1024 do
begin
j := recv(sock, BufferIN[z], 1, 0);
if j <= 0 then begin Memo1.Lines.Add('Buggy'); end;
end;
Delete(BufferIN, pos(BufferIN, #13), 1);
Delete(BufferIN, pos(BufferIN, #10), 1);
Memo1.Lines.Add(BufferIN);
CloseSocket(sock);
finally
WSACleanup;
end;
Memo1.Lines.Add('Done');
Memo1.Lines.Add('Sent: ' + IntToStr(i));
Memo1.Lines.Add('Received: ' + IntToStr(j));
end;
end. |