[fpc-pascal] SocketError

Carsten Bager carsten at beas.dk
Mon Jul 7 11:35:40 CEST 2008


I have a simple TCP program and I want to see when the client breaks the 
line.
With the 205 compiler this is not a problem SocketError tells me, but with 
the 220 compiler nothing happens. Do I have to do something else?

When compiled to 386 SocketError always returns 0, but when compiled to 
ARM SocketError always returns 11.
Why the difference.

Carsten


--------------------------------------------
program tcp2ser;

{ A simple TCP program. }

uses
   sockets, inetaux, myerror,
   sysutils;    {System}

const
   ListenPort : Word = 5000;
   MaxConn = 1;

var
   lSock, uSock : LongInt;
   sAddr : TInetSockAddr;
   Len : LongInt;
   c:char;
   WelcomeStranger:array[0..17] of char='Welcome, stranger!';

begin
   lSock := fpSocket(AF_INET,SOCK_STREAM,PF_UNSPEC);
   if lSock = -1 then SockError('Socket: ');

   with sAddr do
   begin
      Family := af_inet;
      Port := htons(ListenPort);
      Addr := 0;
   end;

   if fpBind(lSock, @sAddr, sizeof(sAddr))<0 then SockError('Bind: ');
   if fpListen(lSock, MaxConn)<0 then SockError('Listen: ');

   repeat
      Say('Waiting for connections...');
      Len := sizeof(sAddr);
      uSock := fpAccept(lSock, @sAddr, @Len);
      if uSock = -1 then SockError('Accept: ');
      Say('Accepted connection from ' + AddrToStr(sAddr.Addr));
      fpSend(uSock, at WelcomeStranger,18,MSG_DONTWAIT);
      repeat
        while FpRecv(uSock, at c,1,MSG_DONTWAIT) >0 do
        begin
          Writeln('-',c);
        end;
        sleep(1000);
        Writeln('->',SocketError);
      until c=chr(27);
      FpShutdown(uSock, 2);
      Say('Connection closed.');
   until False;
end.




More information about the fpc-pascal mailing list