[Overview][Constants][Types][Procedures and functions][Variables] Reference for unit 'Sockets' (#rtl)

Connect

Open a connection to a server socket.

Declaration

Source position: line 0

function Connect(

  Sock: LongInt;

  const Addr;

  Addrlen: LongInt

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: TInetSockAddr;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: TInetSockAddr;

  var SockIn: ;

  var SockOut:

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: String;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Connect(

  Sock: LongInt;

  const addr: String;

  var SockIn: ;

  var SockOut:

):Boolean;

Description

Connectopens a connection to a peer, whose address is described by Addr. AddrLencontains the length of the address. The type of Addrdepends on the kind of connection you're trying to make, but is generally one of TSockAddror TUnixSockAddr.

The forms of the Connectcommand with the Textor Filearguments are equivalent to subsequently calling the regular Connectfunction and the Sock2Textor Sock2Filefunctions. These functions return Trueif successfull, Falseotherwise.

The Connectfunction returns a file descriptor if the call was successfull, -1in case of error.

Errors

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

See also

Listen

  

Listen for connections on socket.

Bind

  

Bind a socket to an address.

Accept

  

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;
  Sin,Sout : Text;
  i        : integer;

begin
  S:=Socket (AF_INET,SOCK_STREAM,0);
  if SocketError<>0 then
   Perror('Client : Socket : ');
  SAddr.sin_family:=AF_INET;
  { port 50000 in network order }
  SAddr.sin_port:=htons(50000);
  { localhost : 127.0.0.1 in network order }
  SAddr.sin_addr.s_addr:=HostToNet((127 shl 24) or 1);
  if not Connect (S,SAddr,Sin,Sout) then
   PError('Client : Connect : ');
  Reset(Sin);
  ReWrite(Sout);
  Buffer:='This is a textstring sent by the Client.';
  for i:=1 to 10 do
   Writeln(Sout,Buffer);
  Flush(Sout);
  Readln(SIn,Buffer);
  WriteLn(Buffer);
  Close(sout);
end.

Example

program pfinger;

uses sockets,errors;

Var
  Addr : TInetSockAddr;
  S : Longint;
  Sin,Sout : Text;
  Line : string;

begin
  Addr.sin_family:=AF_INET;
  { port 79 in network order }
  Addr.sin_port:=79 shl 8;
  { localhost : 127.0.0.1 in network order }
  Addr.sin_addr.s_addr:=((1 shl 24) or 127);
  S:=Socket(AF_INET,SOCK_STREAM,0);
  If Not Connect (S,ADDR,SIN,SOUT) Then
    begin
    Writeln ('Couldn''t connect to localhost');
    Writeln ('Socket error : ',strerror(SocketError));
    halt(1);
    end;
  rewrite (sout);
  reset(sin);
  writeln (sout,paramstr(1));
  flush(sout);
  while not eof(sin) do
    begin
    readln (Sin,line);
    writeln (line);
    end;
  Shutdown(s,2);
  close (sin);
  close (sout);
end.