program server;

uses Sockets, Linux;

const 
 MyPort=9999;
 MaxClients=5;
 Err_TryAgain=11;
 msg='boza';
 
var
 listener,conn,addrlen,rd:longint;
 SAl,SAc:TInetSockAddr;
 s:string;

procedure Error(s:string);
 begin
  writeln('ERROR: ',s);
  halt;
 end; 

function SwapWord(w:word):word;
 begin
  SwapWord:=(w shl 8) or (w shr 8);
 end;
 
begin
 listener:=Socket(AF_INET,SOCK_STREAM,0 {PF_INET});
 if listener<0 then Error('socket');
 fcntl(listener,F_SetFl,Open_NonBlock);
 with SAl do begin
  family:=AF_INET;
  port:=SwapWord(MyPort);
  addr:=0; { allow connections from anywhere }
 end;
 if not Bind(listener,SAl,SizeOf(SAl)) then Error('bind');
 if not Listen(listener,MaxClients) then Error('listen');

 repeat 
  conn:=Accept(listener,SAc,addrlen);
  if (conn<0) and (SocketError<>Err_TryAgain)
   then Error('accept');
 until conn>0;
 fcntl(conn,F_SetFl,Open_NonBlock);
 writeln('New connection !!!');

 fdWrite(conn,msg[1],ord(msg[0]));
 repeat
  rd:=fdRead(conn,s[1],20); 
 until rd>0; 
 s[0]:=chr(rd);
 writeln('Input=[',s,']');
 
end.

