Unit 'Sockets' Package
[Overview][Constants][Types][Procedures and functions][Index] [#rtl]

Connect

Open a connection to a server socket (deprecated).

Declaration

Source position: socketsh.inc line 181

function Connect(

  Sock: LongInt;

  const addr: TInetSockAddr;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: TInetSockAddr;

  var SockIn: file;

  var SockOut: file

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: ansistring;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: ansistring;

  var SockIn: file;

  var SockOut: file

):Boolean;

Description

Connect opens a connection to a peer, whose address is described by Addr. AddrLen contains the length of the address. The type of Addr depends on the kind of connection you're trying to make, but is generally one of TSockAddr or TUnixSockAddr.

The forms of the Connect command with the Text or File arguments are equivalent to subsequently calling the regular Connect function and the Sock2Text or Sock2File functions. These functions return True if success full, False otherwise.

The Connect function returns a file descriptor if the call was success full, -1 in case of error.

Errors

On error, -1 is returned and errors are reported in SocketError.

See also

FPListen

  

Listen for connections on a socket.

FPBind

  

Bind a socket to an address.

Accept

  

Accept a connection from a socket (deprecated).

FPAccept

  

Accept a connection from a socket.

Example

Program Client;

{
  Program to test Sockets unit by Michael van Canneyt and Peter Vreman
  Client Version, First Run sock_svr to let it create a socket and then
  sock_cli to connect to that socket
}

uses Sockets;

procedure PError(const S : string);
begin
  writeln(S,SocketError);
  halt(100);
end;


Var
  SAddr    : TInetSockAddr;
  Buffer   : string [255];
  S        : Longint;
  inout : Text;
  i        : integer;

begin
  S:=fpSocket (AF_INET,SOCK_STREAM,0);
  if s=-1 then
   Perror('Client : Socket : ');
  SAddr.inamily:=AF_INET;
  { port 50000 in network order }
  SAddr.inort:=htons(5000);
  { localhost : 127.0.0.1 in network order }
  SAddr.inddr.s_addr:=HostToNet((127 shl 24) or 1);
  if not Connect (S,SAddr,inout) then
   PError('Client : Connect : ');
  Reset(in
  ReWrite(Sout);
  Buffer:='This is a textstring sent by the Client.';
  or:=1 to 10 do
    Writeln(Sout,Buffer);
  Flush(Sout);
  Readln(Inuffer);
  WriteLn(Buffer);
  Close(sout);
end.

Example

program pfinger;

uses sockets,errors;

Var
  Addr : TInetSockAddr;
  S : Longint;
  inout : Text;
  Line : string;

begin
  Addr.inamily:=AF_INET;
  { port 79 in network order }
  Addr.inort:=79 shl 8;
  { localhost : 127.0.0.1 in network order }
  Addr.inddr.s_addr:=((1 shl 24) or 127);
  S:=fpSocket(AF_INET,SOCK_STREAM,0);
  If Not Connect (S,ADDR,INOUT) Then
    begin
    Writeln ('Couldn''t connect to localhost');
    Writeln ('Socket error : ',strerror(SocketError));
    halt(1);
    end;
  rewrite (sout);
  reset(in
  writeln (sout,paramstr(1));
  flush(sout);
  while not ofin) do
    begin
    readln (inine);
    writeln (line);
    end;
  fpShutdown(s,2);
  close (in
  close (sout);
end.

Documentation generated on: Dec 24 2024