Domanda

Come si utilizzano i socket di rete in Pascal?­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

È stato utile?

Soluzione

Ecco un esempio tratto da http://www.bastisoft.de/programmierung/pascal/pasinet.html

program daytime;

{ Simple client program }

uses
   sockets, inetaux, myerror;

const
   RemotePort : Word = 13;

var
   Sock : LongInt;
   sAddr : TInetSockAddr;
   sin, sout : Text;
   Line : String;

begin
   if ParamCount = 0 then GenError('Supply IP address as parameter.');

   with sAddr do
   begin
      Family := af_inet;
      Port := htons(RemotePort);
      Addr := StrToAddr(ParamStr(1));
      if Addr = 0 then GenError('Not a valid IP address.');
   end;

   Sock := Socket(af_inet, sock_stream, 0);
   if Sock = -1 then SockError('Socket: ');

   if not Connect(Sock, sAddr, sizeof(sAddr)) then SockError('Connect: ');
   Sock2Text(Sock, sin, sout);
   Reset(sin);
   Rewrite(sout);

   while not eof(sin) do   
   begin
      Readln(sin, Line);
      Writeln(Line);
   end;

   Close(sin);
   Close(sout);
   Shutdown(Sock, 2);
end.

Altri suggerimenti

Se stai usando FPC o Lazarus (che è fondamentalmente un fantastico IDE per FPC e un clone di Delphi) potresti usare il Sinapsi libreria di socket.È fantastico.

Se utilizzi Delphi, lo consiglio vivamente Indy socket, un insieme di classi per una facile manipolazione dei socket e di molti altri protocolli Internet (HTTP, FTP, NTP, POP3 ecc.)

Non è possibile utilizzare OpenSSL con Indy versione 10.5 fornita con Delphi 2007.Devi scaricare la versione 10,6 da http://www.indyproject.org/ e installarlo nell'IDE.

Tieni presente che altri pacchetti potrebbero utilizzare Indy, come RemObjects, e quindi anch'essi devono essere ricompilati e questo può essere complicato a causa dei riferimenti incrociati.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top