views:

728

answers:

3

How do you use network sockets in Pascal?

+2  A: 

Here's an example taken from http://www.bastisoft.de/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.
Mickey
Could you correct you link to this please: http://www.bastisoft.de/programmierung/pascal/pasinet.html ?
Gustavo Carreno
A: 

If you are using Delphi, I highly recommend Indy sockets, a set of classes for easy manipulation of sockets and many other internet protocols (HTTP, FTP, NTP, POP3 etc.)

rix0rrr
And then specially the -10.x version.
Marco van de Voort
A: 

You cannot use OpenSSL with Indy version 10.5 that shippes with Delphi 2007. You have to download version 10,6 from http://www.indyproject.org/ and install it into the IDE.

Note that other packages might use Indy, like RemObjects, and therefore they have to be re-compiled too and this can be tricky due to cross-references.

Karl-Otto Rosenqvist