next up previous contents index
Next: Accept Up: Functions and Procedures Previous: Functions and Procedures

Accept

   

Declaration:

Function Accept (Sock:Longint;Var Addr;Var Addrlen:Longint) : Longint;

Description:

Accept accepts a connection from a socket Sock, which was listening for a connection. The accepted 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.

Errors:

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, Connect

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 Linux,Sockets;
const
  SPath='ServerSoc';
  
Var
  FromName : string;
  Buffer   : string[255];
  S        : Longint;
  Sin,Sout : Text;
    
procedure perror (const S:string);
begin
  writeln (S,SocketError);
  halt(100);
end;   



begin
  S:=Socket (AF_UNIX,SOCK_STREAM,0);
  if SocketError<>0 then 
   Perror ('Server : Socket : ');
  UnLink(SPath);
  if not Bind(S,SPath) 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 : ');
  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;
  Unlink(SPath);    
end.



Michael Van Canneyt
Thu Sep 10 13:59:33 CEST 1998