Recent

Author Topic: fpSelect on TCP socket  (Read 5697 times)

iwilson

  • New Member
  • *
  • Posts: 44
fpSelect on TCP socket
« on: January 11, 2012, 08:33:26 pm »
Hi, I have a multi-threaded program, one thread has a socket which receives connections and then passes the new socket onto a new thread. It can receive fine (blocking recv), however using fpSelect infront of it as usual does not seem to work (always returns -1), even though I know there is a message there and I receive one with the line removed. Here is the code for the socket thread (not the accepting thread)

Code: [Select]
unit socket;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Sockets, BaseUnix, UnixType;

type
  TSocket = Class (TThread)
  private
    FSocket      : LongInt;
    TestInt      : Integer;
    MTFD         : TFDSet;
  protected
    procedure Execute; Override;
  public
    constructor Create(Socket : LongInt; ClientAddr : SockAddr_In);
  end;

implementation

   constructor TSocket.Create(Socket : LongInt; ClientAddr : SockAddr_In);
   begin
     FSocket := Socket;
     fpFD_Zero(MTFD);
     TestInt := fpFD_Set(FSocket, MTFD);
     inherited Create(False);
   end;
   procedure TSocket.Execute;
   var
     Timeout  : TTimeVal;
     RTFD     : TFDSet;
     Bytes_In : Array[0..255] of byte;
     Len      : Word;
   begin
     while(NOT Terminated) do
     begin
       RTFD := MTFD;
       Timeout.tv_usec := 100;
       if(fpSelect(FSocket + 1,@RTFD,nil,nil,@Timeout) > 0) then
       begin
         Len := fpRecv(FSocket,@Bytes_In,256,0);
       end;
       Sleep(1);
     end;
   end;
end.

Any direction would  be helpful, thanks.

iwilson

  • New Member
  • *
  • Posts: 44
Re: fpSelect on TCP socket
« Reply #1 on: January 11, 2012, 08:38:23 pm »
I looked at this all day and after finally posting I changed @Timeout to -1 and all works...  8-)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: fpSelect on TCP socket
« Reply #2 on: January 12, 2012, 12:17:16 pm »
Maybe not setting timeout.tv_sec was the original problem? Stack values are not initialized?

 

TinyPortal © 2005-2018