       Socks  unit for Windows socket operations.

This   unit   was  developed  for  using   with   console
applications under WinNT 4.0. I was not tested  with  any
form-based  applications. Also not  tested  under  Win95.
This  is  beta version. If you want to be notified  about
updates, please send e-mail to akorud@polynet.lviv.ua  or
visit http://www.polynet.lviv.ua/~akorud for updates.

Special  thanks to Dmitry Streblechenko (dmitrys@asu.edu)
for his unit for working with Asynchronous File I/O.

Warning!!!   All  functions  return  values  which   mean
nothing. You should not make any decision using this data
(exception  are  functions TClientSocket.WaitForDataTime,
read,  write and TserverSocket.AcceptConnection  see  in
text).

Classes:
  TServerSocket  implement master socket that can listen
for connections;
   TClientSocket  implement client socket which  can  be
connected to any other.

TServerSocket = class(TObject)
  protected
    sock: tSocket;
    portn: word;
  public
    constructor Create(PORT: string);
    function Listen: Word;
    function WaitForConnection: Word;
    function AcceptConnection: TClientSocket;
  end;

constructor  Create(port:string)  create  master  socket
and  try  to bind it to port. Port can be name of service
or  number of port. For example this to fragments of code
are equivalent:
  TServerSocket.Create(telnet);
  TServerSocket.Create(23);
Procedure  first  try  to resolve service  name  to  port
number  using local services file; if conversion fail  it
will  try  to  interpreter port as port number.  If  this
fail, error is occured.

function Listen  try to put socket in listen mode.

function  WaitForConnection    wait  for  connection  to
socket.  Socket  must be set in listen  mode  by  calling
Listen before.

function AcceptConnection  when WailForConnection finish
successfully  you  should  call  AcceptConnection     it
perform  connection  procedure and  return  TClientSocket
object  which  can  be used for reading  and  writing  to
connection;  after AcceptConnection return you  can  call
WaitForConnection to wait for next client connection.
Main cycle of your server program may look like this:

var  A:TServerSocket;
     C:TClientSocket;
begin
 A:=TserverSocket.Create(telnet);
 A.Listen;
 while not Terminate do
  begin
   A.WaitForConnection;
   C:=A.AcceptConnection;
   StartClientThread(C);
  end;
end.

TClientSocket    General-purpose  class  for  read/write
socket operations.

TClientSocket = class(TObject)
  protected
    sock: tSocket;
    portn:word;
  public
    Data: TStream;
    constructor Create;
    function Connect(adr: string; port: string): Word;
    function WaitForData: Word;
    function WaitForDataTime(A: Word): Word;
    function read (var Buf; len: Word): Word;
    function write (var Buf; len: Word): Word;
  end;

constructor Create  do nothing;

function  Connect(adr:string;  port:string)     try   to
allocate   socket  and  cpnnect  it  given  address/port.
Adsress  can  be DNS name or IP, port  service  name  or
number. These four examples are equivalent:
 Connect(192.168.0.2,7);
 Connect(192.168.0.2,echo);
 Connect(netsurfer.lp.lviv.ua,7);
 Connect(netsurfer.lp.lviv.ua,echo);
after  connect return you can read/write to  this  socket
using read/write methods.

function read(var Buf; len:Word):Word  read from  socket
to  buf len bytes; len must be less then Buf size; return
number of actually read bytes.

function write(var Buf; len:Word):Word  write to  socket
from  buf  len  bytes; len must be less  then  Buf  size;
return number of actually written bytes.

function  WaitForData  wait for data on socket; function
is  blocking  if no are on socket for an hour,  function
wont return until it see data.

function  WaitForDataTime(A:Word)    wait  for  data  on
socket;  if  no data will come until A sec function  will
return with result=1 else result will be 0.



Logging procedures in Global unit.

All procedures implemented in this unit support Log their
activity and debug messages.
You  can  also use included log procedures for  your  own
purposes.
To  start  activity  logging you  should  call  procedure
StartLog in Global unit.

procedure  StartLog(logname:string;  logcons:boolean)   
start  logging.  Log  is writtend  to  file  logname.  If
logcons is True then unit will try to open console window
(if not yet opened) and all log will be dublicated there.

procedure StopLog  stop logging and close log file;

procedure  Log(msg:string)  Log msg  to  logfile  opened
with  StartLog  (and)  to console. Current  Date/Time  is
automatically added to message.


Example of simple console application

{$APPTYPE Console}
program socketTest;
uses socks,global;

var C:TClientSocket;
    s:string;

begin
 StartLog(test.log,False);
 C:=TClientSocket.Create;
 C.Connect(192.168.0.3,echo);
 s:=Hello world;
 C.write(s[1],length(s));
 StopLog;
end.



How to contact me:

Andrij Korud

E-mail: akorud@polynet.lviv.ua
WWW: http://www.polynet.lviv.ua/~akorud


