help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



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

Accept

Accept a connection from a socket.

Declaration

Source position: line 0

function Accept(

  Sock: LongInt;

  var Addr;

  var Addrlen: LongInt

):LongInt;

function Accept(

  Sock: LongInt;

  var addr: TInetSockAddr;

  var SockIn: file;

  var SockOut: file

):Boolean;

function Accept(

  Sock: LongInt;

  var addr: TInetSockAddr;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Accept(

  Sock: LongInt;

  var addr: String;

  var SockIn: text;

  var SockOut: text

):Boolean;

function Accept(

  Sock: LongInt;

  var addr: String;

  var SockIn: file;

  var SockOut: file

):Boolean;

Description

FPAccept accepts a connection from a socket Sock, which was listening for a connection. If a connection is accepted, a file descriptor is returned. On error -1 is returned. The returned socket may NOT be used to accept more connections. The original socket remains open.

The Accept call fills the address of the connecting entity in Addr, and sets its length in Addrlen. Addr should be pointing to enough space, and Addrlen should be set to the amount of space available, prior to the call.

The alternate forms of the Accept command, with the Text or File parameters are equivalent to subsequently calling the regular Accept function and the Sock2Text or Sock2File functions. These functions return True if successful, False otherwise.

Errors

On error, -1 is returned, and errors are reported in SocketError, and include the following:

SYS_EBADF
The socket descriptor is invalid.
SYS_ENOTSOCK
The descriptor is not a socket.
SYS_EOPNOTSUPP
The socket type doesn't support the Listen operation.
SYS_EFAULT
Addr points outside your address space.
SYS_EWOULDBLOCK
The requested operation would block the process.

See also

Listen

  

Listen for connections on socket.

Connect

  

Open a connection to a server socket.

Bind

  

Bind a socket to an address.

Example

Program server;

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

uses Sockets;

Var
  FromName : string;
  Buffer   : string[255];
  S        : Longint;
  Sin,Sout : Text;
  SAddr    : TInetSockAddr;

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



begin
  S:=Socket (AF_INET,SOCK_STREAM,0);
  if SocketError<>0 then
   Perror ('Server : Socket : ');
  SAddr.sin_family:=AF_INET;
  { port 50000 in network order }
  SAddr.sin_port:=htons(50000);
  SAddr.sin_addr.s_addr:=0;
  if not Bind(S,SAddr,sizeof(saddr)) then
   PError ('Server : Bind : ');
  if not Listen (S,1) then
   PError ('Server : Listen : ');
  Writeln('Waiting for Connect from Client, run now sock_cli in an other tty');
  if not Accept (S,FromName,Sin,Sout) then
   PError ('Server : Accept : '+fromname);
  Reset(Sin);
  ReWrite(Sout);
  Writeln(Sout,'Message From Server');
  Flush(SOut);
  while not eof(sin) do
   begin
     Readln(Sin,Buffer);
     Writeln('Server : read : ',buffer);
   end;
end.

Notes

 No notes exist for this page yet. 





lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505