Link to home
Start Free TrialLog in
Avatar of kc2592
kc2592

asked on

SendBuf and ReceiveBuf

Hello, here is part of my program,

function Tdhskmus1.HostIPIO(data: PChar; len: integer): integer;
var
      iphostcmd, respcmd: TIPHOSTCMD;
Begin
  result := 0;
  HostIP_connect; //open the socket connection
  ....
  rc := fmtIPCmd(Desrec, iphostcmd); //return number of bytes to be sent
  showmessage(format('I AM ALIVE ()', []));
  rc := host_socket.socket.SendBuf(iphostcmd, rc);
  showmessage(format('rc = %d', [rc]));
  host_socket.socket.ReceiveBuf(respcmd, sizeof(respcmd));
  ...
  host_socket.close;
end;

My question is I have to insert those 2 "showmessage" code for the program to work properly, otherwise  sendbuf and receivebuf will fail (sendbuf writes nothing). I don't understand why this would happen. I have tried insert other codes in the middle, but it appears to be only message box can make it work. Could someone tell me how to resolve it? I don't want user keep pressing enter when the program is running!
Thank you.
Avatar of TomGrills
TomGrills

From your code, it appears that you might not be waiting for the socket connection to be completed before sending and receiving...  putting the showmessage in between the lines causes the app to pause and allows for the connection to be completed...  perhaps you should put the code into the OnConnect event or start a timer event OnConnect that will periodically send the "I AM ALIVE" message....
hi!
if you are using TClientSocket, then try to set ClientType property to ctBlocking. That will enforce synchronous data exchange between server and client i.e. program will proceed to next line only when all data will be sent.
Read also Help (TClientSocket.ClientType) about how to avoid program freezing if server hangs, for example.

wbr, mo.
Avatar of kc2592

ASKER

I found that clientsocket.active property won't turn TRUE until my first showmessage code. I think that's why my sendbuf failed if I took out that code. But I thought when I established the connection, then active would be true. Any suggestion?  (I don't want my socket to be active when the application start up)
It's also because of ctNonBlocking ClientType. if you use nonblocking (asynchronous) connection, then execution of next line occurs immediately after connection witout waiting to connect to server host (i.e. sepparate thread is created). in nonblocking mode you should write OnRead and OnWrite events for read/write operations.
also ctNonBlocking is preferable way to perform communications. in neither case your server will respond to client while all client request is received.
Did you tried to set ClientType to ctBlocking?
Avatar of kc2592

ASKER

If I use OnWrite event to do sendbuf function, then in my function, how do I trigger the OnWrite event? Or I still do sendbuf in my function, then what to do in OnWrite event?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of mocarts
mocarts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial