Avatar of drtester
drtester
 asked on

How to set UseNagle to false in Delphi 7 / Indy 9

Hello,

I have an app written in Delphi 7 that is a TCP server, and must send realtime data immediately out the network port, with no delay.  I had been using a modified socket file, and had the TCP_NODELAY set, and all was well.  I recently switched to using Indy, and can't seem to get this working.  I understand I should set the UseNagle to false, but as you can see below, I *think* I have done this, yet it does not appear to work.

Here are the relevant portions of my code:

procedure TIndyTCPServer.InitServer;
begin
   FServerType := 'TCP';
   FServer := TIdTCPServer.Create(nil);
   with FServer do begin
      MaxConnections := FMaxConnections;
      DefaultPort := DEF_TCP_PORT;
      //TerminateWaitTime := 2000;
      OnConnect := ServerSocketConnect;
      OnDisconnect := ServerSocketDisconnect;
      OnExecute := ServerSocketExecute;
   end;
end;

procedure TIndyTCPServer.CloseServer;
begin
   StopServer;
   FServer.Free;
end;

procedure TIndyTCPServer.StartServer;
begin
   FServer.Active := True;
end;

procedure TIndyTCPServer.StopServer;
var
   Threads: TList;
   i: Integer;
begin
   try
      Threads := FServer.Threads.LockList;
      try
         for i := Threads.Count - 1 downto 0 do
            with TIdPeerThread(Threads.Items[i]).Connection do
               if Connected then
                  Disconnect;
      finally
         FServer.Threads.UnlockList;
      end;
      FServer.Active := False;
   except
   end;
end;

procedure TIndyTCPServer.TransmitBuffer(Connection: TObject; Buffer: Pointer; Count: Integer);
begin
   with TIdPeerThread(Connection) do
      if not Terminated and Connection.Connected then begin
         Connection.Socket.UseNagle:=False; // TCP NO_DELAY!
         Connection.WriteBuffer(Buffer^, Count, True);
         end;
end;

procedure TIndyTCPServer.ServerSocketConnect(AThread: TIdPeerThread);
begin
   // Save for easy access later
   AThread.Connection.Socket.UseNagle:=False; // TCP NO_DELAY!
   AThread.Data := AddRemote(AThread, AThread.Connection.Socket.Binding.PeerIP);
end;

procedure TIndyTCPServer.ServerSocketDisconnect(AThread: TIdPeerThread);
begin
   DeleteRemote(TRemote(AThread.Data));
   AThread.Data := nil;
end;

Open in new window


Ideas?
DelphiProgrammingInternet Protocols

Avatar of undefined
Last Comment
drtester

8/22/2022 - Mon
Thommy

Try flushing your TCP write buffer (TIdTCPConnection.FlushWriteBuffer)...

Perhaps this may help...
Indy Write Buffering / Efficient TCP communication
drtester

ASKER
I just tried adding it here:

ure TIndyTCPServer.TransmitBuffer(Connection: TObject; Buffer: Pointer; Count: Integer);
begin
   with TIdPeerThread(Connection) do
      if not Terminated and Connection.Connected then begin
       //  Connection.Socket.UseNagle:=False; // TCP NO_DELAY!
         Connection.WriteBuffer(Buffer^, Count, True);
         Connection.FlushWriteBuffer();
         end;
end;

...but it generated illegal read of address 0 errors on code following this.  

I did read that article, and it was one of the ones I saw that mentioned the UseNagle thing, but I'm running out of ideas on how to make it work.
ASKER CERTIFIED SOLUTION
drtester

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
drtester

ASKER
No one provided a solution
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck