Board index » delphi » Sending Data through Sockets

Sending Data through Sockets

Hi!

If I wanted to send a string from a clientsocket to a serversocket, I'd use
SendText. But what would I use if I wanted to do the same procedure with a
record instead of a string?

Thanks.

 

Re:Sending Data through Sockets


"Terje B. Olsen" <terje...@idi.ntnu.no> schreef in bericht
news:a5tor8$be2$1@tyfon.itea.ntnu.no...

Quote
> Hi!

> If I wanted to send a string from a clientsocket to a serversocket, I'd
use
> SendText. But what would I use if I wanted to do the same procedure with a
> record instead of a string?

Terje,

You can force anything into a string...

s : string;
MyRecord : TMyRecord;
...

SetLength( s, SizeOf(MyRecord));
move( MyRecord, s[1], SizeOf(MyRecord));
...
MySocket.SendText( s );

Alternatively, you can use the SendBuf()  method:

MySocket.SendBuf( MyRecord, SizeOf(MyRecord));
or:
MySocket.SendBuf( s[1], Length(s));

Just one last advise: always define your records as PACKED, to insure that
Delphi does not add padding bytes ( you know: byte alignment, efficient
memory usage, etc..)

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://users.pandora.be/dirk.claessens2
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

Re:Sending Data through Sockets


Thanks for the answer. SendBuf is what I am looking for then I guess.
But what do you mean with PACKED? :)

- Slap me around, and call me a newbie.

Quote
"Dirk Claessens" <will.bounce@invalid> wrote in message

news:5Aug8.233982$rt4.22550@afrodite.telenet-ops.be...
Quote
> "Terje B. Olsen" <terje...@idi.ntnu.no> schreef in bericht
> news:a5tor8$be2$1@tyfon.itea.ntnu.no...
> > Hi!

> > If I wanted to send a string from a clientsocket to a serversocket, I'd
> use
> > SendText. But what would I use if I wanted to do the same procedure with
a
> > record instead of a string?

> Terje,

> You can force anything into a string...

> s : string;
> MyRecord : TMyRecord;
> ...

> SetLength( s, SizeOf(MyRecord));
> move( MyRecord, s[1], SizeOf(MyRecord));
> ...
> MySocket.SendText( s );

> Alternatively, you can use the SendBuf()  method:

> MySocket.SendBuf( MyRecord, SizeOf(MyRecord));
> or:
> MySocket.SendBuf( s[1], Length(s));

> Just one last advise: always define your records as PACKED, to insure that
> Delphi does not add padding bytes ( you know: byte alignment, efficient
> memory usage, etc..)

> --
> Regards,

> Dirk Claessens
> ---------------------------------------------------------
> http://users.pandora.be/dirk.claessens2
> Attention: All spamshields raised; E-mails will bounce!
> ---------------------------------------------------------

Re:Sending Data through Sockets


Quote
Terje B. Olsen wrote in message ...
>Thanks for the answer. SendBuf is what I am looking for then I guess.
>But what do you mean with PACKED? :)

Place the cursor in it, press F1, read help.

Quote
>- Slap me around, and call me a newbie.

Don't tempt me.

Groetjes,
Maarten Wiltink

Other Threads