20.2 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. 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.

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 (668), Connect (663)

Listing: sockex/socksvr.pp


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 : '+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;
  Unlink(SPath);
end.

Accept

Declaration:
Function Accept (Sock:longint;var addr:string;var SockIn,SockOut:text) : Boolean;
Description:
This is an alternate form of the Accept (660) command. It is equivalent to subsequently calling the regular Accept (660) function and the Sock2Text (671) function. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Accept (660).
See also:
Accept (660)

Accept

Declaration:
Function Accept (Sock:longint;var addr:string;var SockIn,SockOut:File) : Boolean;
Description:
This is an alternate form of the Accept (660) command. It is equivalent to subsequently calling the regular Accept (660) function and the Sock2File (671) function. The Addr parameter contains the name of the unix socket file to be opened. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Accept (660).
See also:
Accept (660)

Accept

Declaration:
Function Accept (Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:File) : Boolean;
Description:
This is an alternate form of the Accept (660) command. It is equivalent to subsequently calling the regular Accept (660) function and the Sock2File (671) function. The Addr parameter contains the parameters of the internet socket that should be opened. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Accept (660).
See also:
Accept (660)

Bind

Declaration:
Function Bind (Sock:Longint;Var Addr;AddrLen:Longint) : Boolean;
Description:
Bind binds the socket Sock to address Addr. Addr has length Addrlen. The function returns True if the call was succesful, False if not.
Errors:
Errors are returned in SocketError and include the following:
SYS__EBADF
The socket descriptor is invalid.
SYS__EINVAL
The socket is already bound to an address,
SYS__EACCESS
Address is protected and you don’t have permission to open it.

More arrors can be found in the Unix man pages.

See also:
Socket (671)

Bind

Declaration:
Function Bind (Sock:longint;const addr:string) : boolean;
Description:
This is an alternate form of the Bind command. This form of the Bind command is equivalent to subsequently calling Str2UnixSockAddr (672) and the regular Bind (662) function. The function returns True if successfull, False otherwise.
Errors:
Errors are those of the regular Bind (662) command.
See also:
Bind (662)

Connect

Declaration:
Function Connect (Sock:Longint;Var Addr;Addrlen:Longint) : Longint;
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 Connect function returns a file descriptor if the call was successfull, -1 in case of error.

Errors:
On error, -1 is returned and errors are reported in SocketError.
See also:
Listen (668), Bind (662),Accept (660)

Listing: sockex/sockcli.pp


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,Linux;

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


Var
  Saddr    : String[25];
  Buffer   : string [255];
  S        : Longint;
  Sin,Sout : Text;
  i        : integer;
begin
  S:=Socket (AF_UNIX,SOCK_STREAM,0);
  if SocketError<>0 then
   Perror('Client : Socket : ');
  Saddr:='ServerSoc';
  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.

Connect

Declaration:
Function Connect (Sock:longint;const addr:string;var SockIn,SockOut:text) : Boolean;
Description:
This is an alternate form of the Connect (663) command. It is equivalent to subsequently calling the regular Connect (663) function and the Sock2Text (671) function. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Connect (663).
See also:
Connect (663)

Connect

Declaration:
Function Connect (Sock:longint;const addr:string;var SockIn,SockOut:file) : Boolean;
Description:
This is an alternate form of the Connect (663) command. The parameter addr contains the name of the unix socket file to be opened. It is equivalent to subsequently calling the regular Connect (663) function and the Sock2File (671) function. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Connect (663).
See also:
Connect (663)

Connect

Declaration:
Function Connect (Sock:longint;const addr: TInetSockAddr;var SockIn,SockOut:file) : Boolean;
Description:
This is another alternate form of the Connect (663) command. It is equivalent to subsequently calling the regular Connect (663) function and the Sock2File (671) function. The Addr parameter contains the parameters of the internet socket to connect to. The function returns True if successfull, False otherwise.
Errors:
The errors are those of Connect (663).
See also:
Connect (663)

Listing: sockex/pfinger.pp


program pfinger;

uses sockets,errors;

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

begin
  Addr.family:=AF_INET;
  { port 79 in network order }
  Addr.port:=79 shl 8;
  { localhost : 127.0.0.1 in network order }
  Addr.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.

GetPeerName

Declaration:
Function GetPeerName (Sock:Longint;Var Addr;Var Addrlen:Longint) : Longint;
Description:
GetPeerName returns the name of the entity connected to the specified socket Sock. The Socket must be connected for this call to work. Addr should point to enough space to store the name, the amount of space pointed to should be set in Addrlen. When the function returns succesfully, Addr will be filled with the name, and Addrlen will be set to the length of Addr.
Errors:
Errors are reported in SocketError, and include the following:
SYS__EBADF
The socket descriptor is invalid.
SYS__ENOBUFS
The system doesn’t have enough buffers to perform the operation.
SYS__ENOTSOCK
The descriptor is not a socket.
SYS__EFAULT
Addr points outside your address space.
SYS__ENOTCONN
The socket isn’t connected.
See also:
Connect (663), Socket (671), connect (2)

GetSocketName

Declaration:
Function GetSocketName (Sock:Longint;Var Addr;Var Addrlen:Longint) : Longint;
Description:
GetSockName returns the current name of the specified socket Sock. Addr should point to enough space to store the name, the amount of space pointed to should be set in Addrlen. When the function returns succesfully, Addr will be filled with the name, and Addrlen will be set to the length of Addr.
Errors:
Errors are reported in SocketError, and include the following:
SYS__EBADF
The socket descriptor is invalid.
SYS__ENOBUFS
The system doesn’t have enough buffers to perform the operation.
SYS__ENOTSOCK
The descriptor is not a socket.
SYS__EFAULT
Addr points outside your address space.
See also:
Bind (662)

GetSocketOptions

Declaration:
Function GetSocketOptions (Sock,Level,OptName:Longint;Var OptVal;optlen:longint) : Longint;
Description:
GetSocketOptions gets the connection options for socket Sock. The socket may be obtained from different levels, indicated by Level, which can be one of the following:
SOL__SOCKET
From the socket itself.
XXX
set Level to XXX, the protocol number of the protocol which should interprete the option.

For more information on this call, refer to the unix manual page getsockopt (2) .

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__EFAULT
OptVal points outside your address space.
See also:
GetSocketOptions (667)

Listen

Declaration:
Function Listen (Sock,MaxConnect:Longint) : Boolean;
Description:
Listen listens for up to MaxConnect connections from socket Sock. The socket Sock must be of type SOCK_STREAM or Sock_SEQPACKET. The function returns True if a connection was accepted, False if an error occurred.
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.
See also:
Socket (671), Bind (662), Connect (663)

Recv

Declaration:
Function Recv (Sock:Longint;Var Addr;AddrLen,Flags:Longint) : Longint;
Description:
Recv reads at most Addrlen bytes from socket Sock into address Addr. The socket must be in a connected state. Flags can be one of the following:
1
: Process out-of band data.
4
: Bypass routing, use a direct interface.
??
: Wait for full request or report an error.

The functions returns the number of bytes actually read from the socket, or -1 if a detectable error occurred.

Errors:
Errors are reported in SocketError, and include the following:
SYS__EBADF
The socket descriptor is invalid.
SYS__ENOTCONN
The socket isn’t connected.
SYS__ENOTSOCK
The descriptor is not a socket.
SYS__EFAULT
The address is outside your address space.
SYS__EMSGSIZE
The message cannot be sent atomically.
SYS__EWOULDBLOCK
The requested operation would block the process.
SYS__ENOBUFS
The system doesn’t have enough free buffers available.
See also:
Send (669)

Send

Declaration:
Function Send (Sock:Longint;Var Addr;AddrLen,Flags:Longint) : Longint;
Description:
Send sends AddrLen bytes starting from address Addr to socket Sock. Sock must be in a connected state. The function returns the number of bytes sent, or -1 if a detectable error occurred. Flags can be one of the following:
1
: Process out-of band data.
4
: Bypass routing, use a direct interface.
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__EFAULT
The address is outside your address space.
SYS__EMSGSIZE
The message cannot be sent atomically.
SYS__EWOULDBLOCK
The requested operation would block the process.
SYS__ENOBUFS
The system doesn’t have enough free buffers available.
See also:
Recv (668), send (2)

SetSocketOptions

Declaration:
Function SetSocketOptions (Sock,Level,OptName:Longint;Var OptVal;optlen:longint) : Longint;
Description:
SetSocketOptions sets the connection options for socket Sock. The socket may be manipulated at different levels, indicated by Level, which can be one of the following:
SOL__SOCKET
To manipulate the socket itself.
XXX
set Level to XXX, the protocol number of the protocol which should interprete the option.

For more information on this call, refer to the unix manual page setsockopt (2) .

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__EFAULT
OptVal points outside your address space.
See also:
GetSocketOptions (667)

Shutdown

Declaration:
Function Shutdown (Sock:Longint;How:Longint) : Longint;
Description:
ShutDown closes one end of a full duplex socket connection, described by Sock. How determines how the connection will be shut down, and can be one of the following:
0
: Further receives are disallowed.
1
: Further sends are disallowed.
2
: Sending nor receiving are allowed.

On succes, the function returns 0, on error -1 is returned.

Errors:
SocketError is used to report errors, and includes the following:
SYS__EBADF
The socket descriptor is invalid.
SYS__ENOTCONN
The socket isn’t connected.
SYS__ENOTSOCK
The descriptor is not a socket.
See also:
Socket (671), Connect (663)

Sock2File

Declaration:
Procedure Sock2File (Sock:Longint;Var SockIn,SockOut:File);
Description:
Sock2File transforms a socket Sock into 2 Pascal file descriptors of type File, one for reading from the socket (SockIn), one for writing to the socket (SockOut).
Errors:
None.
See also:
Socket (671), Sock2Text (671)

Sock2Text

Declaration:
Procedure Sock2Text (Sock:Longint;Var SockIn,SockOut: Text);
Description:
Sock2Text transforms a socket Sock into 2 Pascal file descriptors of type Text, one for reading from the socket (SockIn), one for writing to the socket (SockOut).
Errors:
None.
See also:
Socket (671), Sock2File (671)

Socket

Declaration:
Function Socket (Domain,SocketType,Protocol:Longint) : Longint;
Description:
Socket creates a new socket in domain Domain, from type SocketType using protocol Protocol. The Domain, Socket type and Protocol can be specified using predefined constants (see the section on constants for available constants) If succesfull, the function returns a socket descriptor, which can be passed to a subsequent Bind (662) call. If unsuccesfull, the function returns -1.
Errors:
Errors are returned in SocketError, and include the follwing:
SYS__EPROTONOSUPPORT
The protocol type or the specified protocol is not supported within this domain.
SYS__EMFILE
The per-process descriptor table is full.
SYS__ENFILE
The system file table is full.
SYS__EACCESS
Permission to create a socket of the specified type and/or protocol is denied.
SYS__ENOBUFS
Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.
See also:
SocketPair (672), socket (2)

for an example, see Accept (660).

SocketPair

Declaration:
Function SocketPair (Domain,SocketType,Protocol:Longint;var Pair:TSockArray) : Longint;
Description:
SocketPair creates 2 sockets in domain Domain, from type SocketType and using protocol Protocol. The pair is returned in Pair, and they are indistinguishable. The function returns -1 upon error and 0 upon success.
Errors:
Errors are reported in SocketError, and are the same as in Socket (671)
See also:
Str2UnixSockAddr (672)

Str2UnixSockAddr

Declaration:
Procedure Str2UnixSockAddr(const addr:string;var t:TUnixSockAddr;var len:longint)
Description:
Str2UnixSockAddr transforms a Unix socket address in a string to a TUnixSockAddr structure which can be passed to the Bind (662) call.
Errors:
None.
See also:
Socket (671), Bind (662)