Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi2007 - TIdTCPClient/Server - Error during transmission of data

Status
Not open for further replies.

niobi

Programmer
Sep 8, 2010
1
IT
Hello,
I'm a problem with InDy components, that I do not know well, becaus I used Socket components (Delphi 5) and during migration to Delphi7 they are vanished. I know that is possible to install package dclSocket, but I prefer to migrate to this unfamiliar components, but is evident that I've not understand nothing.

This is the source (an extract, that I think is sufficient).
The connection is on 127.0.0.1, the same computer, two distinct procedures, the port > 50000.

procedure client
- - - - - - - - - - - - - - - - -
(components TIdTCPClient + TIdAntiFreeze)
Here, I create a file on disk, containing data to send to the server, and send it dimension: Size := Length(File)
. Client.WriteInteger(Size); // dimension packet
. TmpStream := TMemoryStream.Create;
. TmpStream.LoadFromFile(IndirProc+'\REPORT\TMS.DAT' ); // file created on local disk
. Client.OpenWriteBuffer;
. try
. Client.WriteStream(TmpStream);
. except
. on E: Exception do
. MesServ('Transazione non eseguita: +#10+'"'+ E.Message+'"'+
. #10+'Riprovare...');
. end;
. Client.CloseWriteBuffer;
. FreeAndNil(TmpStream);


procedure Server
- - - - - - - - - - - - - - - - - - - -
(components TIdTCPServer + TIdThreadMgrDefault + TIdAntiFreeze.)
here, we are in event Server.OnExecute:

. try
. TmpStream := TFileStream.Create(IndirArch+'\Trans7',fmCreate);
. AThread.Connection.ReadStream(TmpStream,-1,False);
. except
. on E: Exception do begin
. MesServ(3,'Errore in lettura dati: '+E.Message); Ok := False;
. end;
. end;
. FreeAndNil(TmpStream);

Problem !!!
- - - - - - - - -
In my intention, at the end of the operation, on the local disk (of the server) now there is a file named "Trans7", but this is not.
The server is blocked at row "AThread.Connection.ReadStream...", and the client remains waiting of an answer, and the two procedures remain suspended.

Question:
The server reads on the buffer the data that I've sent with the client,
and it writes the data on the TFileStream file, from position 0 to position Size: is this just?

I've tested so:

. try
. TmpStream := TMemoryStream.Create;
. AThread.Connection.ReadStream(TmpStream,-1,False);
. TmpStream.SaveToFile(IndirArch+'\Trans7');
. except
. on E: Exception do begin
. MesServ(3,'Errore in lettura dati: '+E.Message); Ok := False;
. end;
. end;
. FreeAndNil(TmpStream);

In this case, the procedure server raise an error of Memory Overflow, at line "AThread.Connection.ReadStream...", the flow continues in "Except" and return to "try", in an infinite loop. The client remain blocked on line "Client.WriteStream(TmpStream);", and I must close the two procedures via TaskManager of the Windows.

I cannot to use ReadLn or WriteLn methods (but in this case, all functions), because in my data there are numerous "#10" and "#13".

Where is the bug?
Thanks very much for answers and help, and I hope that my english is at least comprensible.

Antonio
 
I know your post is 3 weeks old already, but in case you didn't solve the problem yet, the reason for your problem is because you aren't handling the Client.WriteInteger command at the server.

So basically you're writing two things to the server (the integer and the stream), but the server is reading only the stream.

Indy sockets are synchronous (blocking), which means that you have to handle read/write commands in sequence. So if you write 5 things to the server, your server must be designed to read 5 things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top