I'm setting up a new server, and I have a deadlock on waiting to receive data from the server.
On the client-side, I add a new thread to handle net program and on execute segment, first create a TIdTCPClient and connect to the server. Sending login data and when the connection has done sending data and receives ack sign. All is fine, but on connection loss or dead when I waiting to receive ack from the server my code is blocked.
Here is my sample code:
if sending_buf(Buffer_to_send) then//send my data to server
//wait to receive ack from server
try
In_buffer:= nil;
IdTCPClient1.IOHandler.ReadBytes(In_buffer,-1,false);// here is my problem!!!
if In_buffer <> nil then
begin
//received ack answer do other prosses...
end
else
begin
//on timeout
end;
except on e : Exception do
begin
SendHis('Error= ' + e.Message);//send error to main thread
raise;
end;
How to determine if the connection is lost or dead on the ReadBytes()
line?
And here is my short server code:
try
exc2_getdata1:
if AContext.Connection.Connected then
begin
byte_buffer:= nil;
with AContext.Connection.IOHandler do
begin
if CheckForDataOnSource(5000) then
begin
if not InputBufferIsEmpty then
begin
InputBuffer.ExtractToBytes(byte_buffer);
end
else
begin
goto exc2_getdata1;
end;
end
else
begin
//timeout
goto exc2_getdata1;
end;
end;
end;
except
on e : Exception do
begin
//do something
raise;
end;
end;
//check validate and use data, after prosses send ack to client
SetLength(Buffer_to_send,1 + sizeof(integer));//1 is the ack sign short integer and other is received packet id
Buffer_to_send[0]:= ack_s;//is short integer ack sign
move(byte_buffer[2],Buffer_to_send[1],sizeof(integer));//copy the received id to out buffer
try
if not CheckBox1.Checked then //add this line to skip sending ack to client for simulate error
AContext.Connection.IOHandler.write(Buffer_to_send); //send ack to client
except
//do something
raise;
end;