-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
I am experimenting with writing a program that does not rely on LineMode to send and receive data. The following procedure is attached to a server component: TriggerWriteDebug is my own event, which at this time only outputs text to a TMemo. Also, I plan on changing the code to except Network Byte Order numeric data, but for now I want to get this out of the way. :-) procedure TMySockServer.ClientDataAvailable(Sender: TObject; ErrCode: Word); var I: Integer; Buff: Array[0.._MAX_BUFF - 1] of Byte; begin TriggerWriteDebug('DataAvailable() enter...'); if (TMySockServerClient(Sender).State <> wsClosed) then begin I := ReadFully(Sender, @Buff, 5); end; TriggerWriteDebug('DataAvailable() exit.'); end; ReadFully is another procedure I have written in hopes that I can then read an entire string, word, integer, what have you and read until I gather up enough data to continue (since I'm not using LineMode). function TMySockServer.ReadFully(Sender: TObject; Data: Pointer; BytesExpected: Integer): Integer; var Buffer: PChar; Offset: Integer; Received: Integer; begin Offset := 1; GetMem(Buffer, BytesExpected); while (BytesExpected > 0) do begin Received := TMySockServerClient(Sender).Receive(@Buffer[Offset], BytesExpected); if (Received > 0) then begin Offset := Offset + Received; BytesExpected := BytesExpected - Received; end; end; TriggerWriteDebug(Buffer); Result := Offset; end; I have a couple of questions: When I output Buffer in the last lines of ReadFully(), if I have a client that sends "blah!" (which is five characters), the Memo has a non-displayable character (but shows up on the Memo as a black block) and then 'blah!'. So I'm not sure if I'm grabbing the string correctly. I hope with minimal changes that this procedure might also grab numbers without error, too. When my test client disconnects from the server, the server enters its ClientDataAvailable event and does not exit, which is why I have a "if (TMySockServerClient(Sender).State <> wsClosed)" line in my ClientDataAvailable - I was hoping that I could detect whether or not the socket is still connected and only then try to read data. As of right now, it stays in DataAvailable and my server goes into an infinite loop. :( I realize the advantages of a LineMode = TRUE. I may just switch, but at the current time I'm just trying to learn more about sockets and building custom protocols in general. Does anyone have any advice for me to make these routines operational or more robust? Cheers, Wes -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (MingW32) iD8DBQFCm3nqB5TesQcz1CoRAn5dAKDy9Likg8P8bYqcLx5m5dOSiGrT3gCgqpMm JnYB2ex9LdhMnuX+aaNszX0= =KZhv -----END PGP SIGNATURE----- -- To unsubscribe or change your settings for TWSocket mailing list please goto http://www.elists.org/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be