Sockets

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Sockets

Carsten Bager
If I create a TCP socket with

 fpSocket(AF_INET,SOCK_STREAM,PF_UNSPEC);
 fpBind(lSock, @sAddr, sizeof(sAddr))<0 then
 fpListen(lSock, cMaxConn)<0 thenand

And waits for connections with

fpAccept(lSock, @nSockAddr, @nLen);

and when a connection comes in I wait for a new one (in a new thread)  with
fpAccept(lSock, @nSockAddr, @nLen);

If I have 2 or more connections and one of the older one breaks I canĀ“t detect it. Sending
and the receiving seem OK.
If I break the newest one, I can detect this, and I can detect the other broken lines.
So my problem is I can only detect broken lines if it is the newest.
Is there a way around this?


Carsten



------------------------


Constructor TServer.create(pListenPort:word);
var
  sAddr:TInetSockAddr;
begin
  inherited Create;
  debug:=false;
  CloseWaitForeConnection:=false;
  ConnectNr:=0;
  ListenPort:=pListenPort;
  lSock := fpSocket(AF_INET,SOCK_STREAM,PF_UNSPEC);
  if lSock = -1 then
    SockError('fpSocket:');
  sAddr.Family:=af_inet;
  sAddr.Port:=htons(ListenPort);
  sAddr.Addr:=0;
  if fpBind(lSock, @sAddr, sizeof(sAddr))<0 then
    SockError('Bind: ');
  if fpListen(lSock, cMaxConn)<0 then
    SockError('Listen: ');
  SocketError;
  if debug then
    Writeln('TServer.create:'+inttostr(lSock)+' SocketError:'+IntToStr(SocketError));
End;

Procedure TServer.WaitForeConnection;
Begin
  repeat
    if Debug then
    begin
      if ConnectNr=0 then
        Writeln('Waiting for connections...')
      else Writeln('Waiting for next connection ....');
    end;
    nLen := sizeof(nSockAddr);
    nSockAddr.Family:=af_inet;
    nSockAddr.Port:=htons(ListenPort);
    nSockAddr.Addr:=0;
    nSock := fpAccept(lSock, @nSockAddr, @nLen);
    if nSock = -1 then SockError('Accept: ');
    if Debug then
      Writeln('Accepted connection from ',AddrToStr(nSockAddr.Addr),'
Port:',intToStr(nSockAddr.Port));
    if ConnectNr<cMaxConn then
    begin
      inc(ConnectNr);
      if Debug then
        Writeln('Connection no ',AddrToStr(nSockAddr.Addr),' Port:',intToStr(nSockAddr.Port));
      ConnectData.SockAddr:=nSockAddr;
      ConnectData.Len:=nLen;
      ConnectData.Sock:=nSock;
      ConnectData.ConnectNr:=ConnectNr;
      ConnectDataPointer:=FindTomConnectDataPos(ConnectData);
      if ConnectDataPointer<>0 then

BeginThread(@StartRemoteProgram,self,ConnectDataArray[ConnectDataPointer].Thread_
Execute)
      else Writeln('No space in array');
    end else
    begin
      fpSend(nSock,@NoMoreConnections,length(NoMoreConnections),MSG_DONTWAIT);
      sleep(5000);
      FpShutdown(nSock, 2);
      if debug then
        Writeln(NoMoreConnections);
    end;
  until CloseWaitForeConnection;
End;


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal