Question

I have a Delphi program which sits as a server receiving data and sending back small acknowledgement packets. I previously used this code (edited for brevity)

procedure TdmServer.OnExecuteTCPServer(AThread: TIdPeerThread);
var
  IncomingPacket : string;
  ResponsePacket : string;
begin
  IncomingPacket := AThread.Connection.Readln(#$03);

  if IncomingPacket <> '' then
  begin
    ResponsePacket := ProcessTelegram(IncomingPacket);

    AThread.Connection.writeln(ResponsePacket);
  end;

  AThread.Connection.Disconnect;
end;

This almost works fine, apart from appending an end of line CRLF as it sends, which the client (not under my control) doesn't like.

So I changed it to:

AThread.Connection.Write(ResponsePacket);

and nothing is received on the client.

Then I tried

AThread.Connection.WriteBuffer(ResponsePacket, length(ResponsePacket), true);

to try and get it to write immediately, but it still doesn't send at all.

I have put in delays, tried opening the buffer, flushing and closing it again (like the help file), but still no joy and any time FlushWriteBuffer is called I get an AV.

I'm stuck. Please could anyone offer any words of wisdom?

Était-ce utile?

La solution

ReadLn() strips off the terminator from its Result. Is your ProcessTelegram() accounting for that? If it needs the terminator, you will have to add it back in manually, eg:

ProcessTelegram(IncomingPacket + #03)

Is ResponsePacket being formatted correctly for what the client is expecting? If the client is not expecting a terminating CRLF, then using Write() instead of WriteLn() is the correct thing to do.

If you use WriteBuffer(), you must dereference a String in order to pass the correct data address to WriteBuffer(), eg:

WriteBuffer(ResponsePacket[1], Length(ResponsePacket), True)

Or:

WriteBuffer(PChar(ResponsePacket)^, Length(ResponsePacket), True)

If the client is still not receiving the response correctly, then either you are not sending anything at all, or you are not sending the terminator that the client is expecting. Use a packet sniffer, such as Wireshark, to see what the client is actually receiving, if anything.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top