Reliable Unidirectional Messages with Indy

I have a client application that should send notifications to an optional server application. The client should not be affected if the server application exists or not. He should try to connect to the server application and send a notification message, and in case of errors, he should just silently ignore all errors and continue working.

I use Indy for TCP communication, but all attempts to avoid error messages (for example, when the server application is closed when connected to the client) failed.

Is there any way to make this reliable?

The current code is as follows:

if (not Client.Connected) then
  begin
  Client.Host := ServerName;
  Client.Port := ServerPort;
  Client.ConnectTimeout := ConnectTimeout;
  try
    Client.Connect;
  except
    Exit;
  end;
  end
try
  Client.IOHandler.WriteLn ('NOTIFYCHANGE "' + Param + '"');
  Client.IOHandler.WriteBufferFlush;
except
  try
    Client.Disconnect;
  except
    { ignore errors while disconnecting }
  end;
  try
    Client.Connect;
  except
    { ignore errors while connecting }
  end;
end;
+3
source share
5

? , . , ? . :

  • " ", - .
  • . ( Indy, .)
  • , .
  • .

, , , , . , , , , , . , , ( ) , , Indy, EAccessViolation EOutOfMemory.

+3

TCP- Synapse , , Indy, " " .

:

procedure SendMessage(m: string);
var
  sock : TTCPBlockSocket;
  response : string;
begin
  Sock := TTCPBlockSocket.Create;
  try
    Sock.SetTimeout(200);
    Sock.Connect(PrinterServerAddr, IntToStr(PrinterServerPort));

    Sock.SendString(m);
    response := Sock.RecvString(1000);
  finally
    Sock.Free;
  end;
end;

..
try
  SendMessage(NewMessage);
except
 //..handle exception..
end;

TThread, .

+2

, UDP. " ", , , . , - .

0

Harriv , UDP-. .

0

, Indy 9 10. 9, , .

?:

procedure Send(Target: String; Port: Integer; S: String);
var
  C: TIdTcpClient;
begin
  C := TIdTcpClient.Create(nil);
  try
    try
      C.Host := Target;
      C.Port := Port;
      C.Connect;
      try
        C.Write(S);
      finally
        C.Disconnect;
      end;
    except
      // Ignore Indy exceptions
      on EIdException do;
    end;
  finally
    C.Free;
  end;
end;

, TIdTcpClient.

0

Source: https://habr.com/ru/post/1710337/


All Articles