[fpc-pascal] SocketError
Carsten Bager
carsten at beas.dk
Mon Jul 7 15:42:05 CEST 2008
> Ask your OS vendor. These are OS errors.
> But:
> 11 is iirc EAGAIN, which means "try again", which is due to a potential
> deadlock in the kernel not being handled properly.
You say that SocketErrors are OS errors but some influence the FPC
compiler must have.
I modified my program a bit. It now works with the 222 compiler. but still not
with the 220 compiler. That is because I get a SocketError 104 from the 222
compiled program, when sending to a broken connection. The 220
compiled program aborts without any warning when sending to a broken
connection.
The FpRecv returns 0 when the connection is broken (and -1 when ok)
when receive buffer is empty.
I cannot find this behavior documented anywhere.
So I am still locking for a reliable way to detect a broken connection.
-----------------------------------------------------------
program tcp2ser;
{ A simple TCP program. }
uses
sockets, inetaux, myerror,
sysutils; {System}
const
ListenPort : Word = 5000;
MaxConn = 1;
var
lSock, uSock : LongInt;
sAddr : TInetSockAddr;
SocketError_,FpRecv_,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
FpRecv_:=FpRecv(uSock, at c,1,MSG_DONTWAIT);
while FpRecv_>0 do
begin
Writeln('-',c);
FpRecv_:=FpRecv(uSock, at c,1,MSG_DONTWAIT);
end;
sleep(1000);
if c='s' then fpSend(uSock, at WelcomeStranger,18,MSG_DONTWAIT);
SocketError_:=SocketError;
Writeln('->',SocketError_,FpRecv_:10);
until (c=chr(27)) or ((SocketError_<>0) and ((SocketError_<>11))) or
(FpRecv_=0);
FpShutdown(uSock, 2);
Say('Connection closed.');
until False;
end.
Med venlig hilsen
Carsten Bager
BEAS A/S
Brørupvænget 10
DK-7650 Bøvlingbjerg
Tlf. : +45 9788 5222 Fax : +45 9788 5434
www.beas.dk
More information about the fpc-pascal
mailing list