var
Form1: TForm1;
AnWSAData:TWSADATA;
sock:TSOCKET;
sin:SOCKADDR_IN;
AURL:String;
Command:String;
implementation
{$R *.dfm}
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
gethostname (hostName, sizeof (hostName));
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If wsaStartup(MAKEWORD(2,0), AnWSAData)<>0 Then Showmessage('Erreur d''initialisation de la dll');
sock := socket(AF_INET,SOCK_STREAM,0);
If Sock=INVALID_SOCKET Then Showmessage('ERREUR dans la création du socket');
HostToIP(host_edit_.Text,AURL);
sin.sin_addr.s_addr:= inet_addr(PChar(AURL));
If (sin.sin_addr.s_addr=INADDR_NONE) Or (sin.sin_addr.s_addr=INADDR_ANY) Then
Showmessage('erreur d''adresse');
sin.sin_family:= AF_INET;
sin.sin_port:= htons(80);
bind(sock, sin, sizeof(sin));
if Winsock.Connect(sock, sin, SizeOf(sin)) = Socket_Error then
Showmessage('erreur de connection');
If wsaAsyncSelect(sock, Handle,wm_asynch_select,FD_CONNECT+ FD_READ+ FD_WRITE+ FD_CLOSE)<>0 Then
ShowMessage('Erreur pour asynchrone');
end;
procedure TForm1.Message_Connection(ASocket: Integer);
Begin
Showmessage('Votre socket '+IntToStr(ASocket)+' est connecté');
// permet d'être notifié de l'ouverture d'une connection
End;
procedure TForm1.Message_Write(ASocket: Integer);
Begin
// FD_WRITE : permet d'être notifié lorsque le socket peut être écrit
End;
procedure TForm1.Message_Read(ASocket: Integer);
Var
Buff:array[0..2047] of Char;
Begin
Recv(Sock,Buff,SizeOF(Buff),0);
Showmessage(Buff);
// FD_READ : permet d'être notifié de la présence de données dans le tampon de lecture associé au socket
End;
procedure TForm1.Message_Close(ASocket: Integer);
Begin
Showmessage('fermée');
// permet d'être notifié de la fermeture d'une connexion.
End;
procedure TForm1.Message_wm_async_select(var Msg: TMessage);
var
Param: Integer;
Notif: Integer;
Socket: Integer;
Begin
Param:=Msg.LParam;
Socket:=Msg.WParam;
Notif:= wsaGetSelectEvent(Param);
If wsaGetSelectError(Param)<=wsaBaseErr Then
Case Notif of
FD_CONNECT: Message_Connection(Socket);
FD_WRITE: Message_Write(Socket);
FD_READ: Message_Read(Socket);
FD_CLOSE: Message_Close(Socket);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Command:='GET '+ page_edit_.Text+ ' HTTP/1.0'+#1310
+ 'Host: '+ host_edit_.Text+#1310+#1310;
If Send(sock,Command,SizeOf(Command),0)=SOCKET_ERROR Then Showmessage('Erreur dans la requette');
end;
end.
Pouvez vous me dire ce que vous récupérez comme texte ?
cs_cantador
Messages postés4720Date d'inscriptiondimanche 26 février 2006StatutModérateurDernière intervention31 juillet 202113 30 nov. 2008 à 12:21
salut Francky,
j'ai récupéré ton code et ça compile nickel mais il faudrait pour le test soit correct que tu précises les infos à taper dans les deux edits.
var
Form1: TForm1;
AnWSAData:TWSADATA;
sock:TSOCKET;
sin:SOCKADDR_IN;
AURL:String;
Command:String;
implementation
{$R *.dfm}
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
gethostname (hostName, sizeof (hostName));
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If wsaStartup(MAKEWORD(2,0), AnWSAData)<>0 Then Showmessage('Erreur d''initialisation de la dll');
sock := socket(AF_INET,SOCK_STREAM,0);
If Sock=INVALID_SOCKET Then Showmessage('ERREUR dans la création du socket');
HostToIP(host_edit_.Text,AURL);
sin.sin_addr.s_addr:= inet_addr(PChar(AURL));
If (sin.sin_addr.s_addr=INADDR_NONE) Or (sin.sin_addr.s_addr=INADDR_ANY) Then
Showmessage('erreur d''adresse');
sin.sin_family:= AF_INET;
sin.sin_port:= htons(80);
bind(sock, sin, sizeof(sin));
if Winsock.Connect(sock, sin, SizeOf(sin)) = Socket_Error then
Showmessage('erreur de connection');
If wsaAsyncSelect(sock, Handle,wm_asynch_select,FD_CONNECT+ FD_READ+ FD_WRITE+ FD_CLOSE)<>0 Then
ShowMessage('Erreur pour asynchrone');
end;
procedure TForm1.Message_Connection(ASocket: Integer);
Begin
Showmessage('Votre socket '+IntToStr(ASocket)+' est connecté');
// permet d'être notifié de l'ouverture d'une connection
End;
procedure TForm1.Message_Write(ASocket: Integer);
Begin
// FD_WRITE : permet d'être notifié lorsque le socket peut être écrit
End;
procedure TForm1.Message_Read(ASocket: Integer);
Var
Buff:Array [0..1023] Of Char;
Begin
Recv(Sock,Buff[0],SizeOf(Buff),0);
Showmessage(Buff);
// FD_READ : permet d'être notifié de la présence de données dans le tampon de lecture associé au socket
End;
procedure TForm1.Message_Close(ASocket: Integer);
Begin
Showmessage('fermée');
// permet d'être notifié de la fermeture d'une connexion.
End;
procedure TForm1.Message_wm_async_select(var Msg: TMessage);
var
Param: Integer;
Notif: Integer;
Socket: Integer;
Begin
Param:=Msg.LParam;
Socket:=Msg.WParam;
Notif:= wsaGetSelectEvent(Param);
If wsaGetSelectError(Param)<=wsaBaseErr Then
Case Notif of
FD_CONNECT: Message_Connection(Socket);
FD_WRITE: Message_Write(Socket);
FD_READ: Message_Read(Socket);
FD_CLOSE: Message_Close(Socket);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Command:='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;
If Send(sock,Pointer(Command)^,Length(Command),0)=SOCKET_ERROR Then Showmessage('Erreur dans la requette');
end;
Je fais une requette HTTP pour récupérer la page de www.iprelax.fr/http/. Je pensais que le probleme venait de mon FAI (Utilisation d'un proxy). Je vais faire un serveur avec TSocket pour comprendre ce qui ne va pas
J'avais pas vu le lien : Oui le Paul semble etre plutot une pointure . Par contre lui il utilise des sockets bloquants : Ca m'embete d'utiliser des threads pour cela .