Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Apr : Re: A polite request for Advice on using Sockets

www.cryer.info
Managed Newsgroup Archive

Re: A polite request for Advice on using Sockets

Subject:Re: A polite request for Advice on using Sockets
Posted by:"DC" (e..@egg.ns.com)
Date:Tue, 10 Apr 2007 10:35:35

I have looked and so am using TWinSocketStreams write method as per some
exaple i found via deja.

However, the string I send seems to get garbled.

I am using TCPTrace to see whats being sent/recieve and if I send

'1=325617' + #10 + '2=0' + #10 + '3=2.56' + #10 + '99=0' + #10;

my TCP trace shows it as

"/¤:J ?W¸)l÷ ÞùC (j

What could be causeing this and how do I stoip it.

Var S : TWinsocketStream;
    SendStr : String;
    SendBuf : PChar;
    ReadBuf : Array[0..4095] Of Char;
    BytesRead : Integer;
begin
  ClientSocket1.Port := 9999;
  ClientSocket1.Host := '127.0.0.1';
  ClientSocket1.ClientType := ctBlocking;
  ClientSocket1.Open;
  Try
   S := TWinSocketStream.Create(ClientSocket1.Socket, 5000);

   SendStr := '1=325617' + #10 + '2=0' + #10 + '3=2.56' + #10 + '99=0' +
#10;
   GetMem(SendBuf, Length(SendStr)+1);
   StrPCopy( SendBuf, SendStr);
   S.Write(SendBuf, Length(SendStr)+1);

   FreeMem(SendBuf);
   BytesRead := S.Read(ReadBuf, SizeOf(ReadBuf));
   Memo2.Text := Memo2.Text + String(ReadBuf);
  Finally
   S.Destroy;
  End;
  ClientSocket1.Close;


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:461b3fa0$1@newsgroups.borland.com...
>
> "DC" <egg@egg.ns.com> wrote in message
> news:461b30c0$1@newsgroups.borland.com...
>
>> I should establish a connection.
>> Send a request string
>> and I will receive the response on the same connection.
>>
>> So it seems that a TClientSocket in ctBlocking mode is up to the
> job.
>
> What you have described has nothing to do with blocking vs.
> non-blocking.  TCP is TCP regardless.  It is a two-way byte stream.
> Whether you use blocking or non-blocking is a matter of personal
> choice and code design.  If you like to use asynchronous events and
> in/out queues, use non-blocking.  If you are like to use sequential
> programming and threading, use blocking.  They both work equally well
> when used properly.
>
>> Also, I may need to also use this in a service app - would the same
>> code be OK there.
>
> Blocking sockets work just fine in service.s  But your code is not
> correct to begin with.
>
>> If I am wrong - some exapmples would be greatly appreciated.
>
> Please go to http://www.deja.com and search through the newsgroup
> archives.  Detailed examples have been posted many many many times
> before.
>
>>           Address := edtIP.Text;
>>           Host := edtIP.Text;
>
> Do not set both the Host and Address property at the same time.  You
> can only use one or the other, not both.
>
>>           Socket.SendText( Req );
>
> All reading/writing on a blocking socket must go through a
> TWinSocketStream.  Also, you are not taking into account the result of
> the writing in order to make sure that all of the data was actually
> sent before you then begin reading the response.
>
>>           Delay( 1000 );
>
> Use TWinSocketStream's WaitForData() method instead.  You don't need
> Delay() at all.
>
>>           Resp := '';
>>           repeat
>>           st := TWinSocketStream.Create( cs.Socket, 1000 );
>>              if ST.WaitForData( 1000 ) then
>>                  Resp := cs.Socket.ReceiveText;
>>           until s <> '' or Timout
>
> That can't be your real code.  Your reading the data into one
> variable, but using another variable for the loop counter.  Always
> always always copy/paste your real code.
>
>
> Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive