[fpc-pascal]Server - attachment

FPC Mail list reader fpc at cdp.cugir.com
Thu Jan 11 11:04:26 CET 2001


Oh, i'm very sorry, but i forgot to add the attachment for the last reply
to "Re: Server". So here it is ... again, a thousand pardons ...
-------------- next part --------------
uses sockets;
const servport=12345; //or whatever
var servaddr,clientaddr:tinetsockaddr;
    servsock,clientsock,x:longint;
    s:string;
begin
 servsock:=socket(af_inet,sock_stream,0);
 if servsock<0 then halt; //error
 with servaddr do
 begin
  family:=af_inet;
  port:=lo(servport) shl 8+hi(servport);
  addr:=0;
 end;
 if not bind(servsock,servaddr,sizeof(servaddr)) then halt; //error
 if not listen(servsock,5) then halt; //error
 x:=sizeof(clientaddr);
 writeln('ready');
 clientsock:=accept(servsock,clientaddr,x);
 if clientsock<0 then halt; //error
 writeln('accepted');
 s:='Welcome to my server'#13#10;
 send(clientsock,s[1],length(s),0);
 x:=recv(clientsock,s[1],sizeof(s)-1,0);
 if x<=0 then writeln('client closed conn')
 else begin
  s[0]:=chr(x);
  writeln('received: [',s,']');
  shutdown(clientsock,2);
 end;
 shutdown(servsock,2);
end.


More information about the fpc-pascal mailing list