Board index » delphi » Sending array through socket help

Sending array through socket help

I finally learned enough of WinSock to send stuff and get stuff back.

But im trying to send this piece of code through

procedure send;
var
str01:string;
begin
str01:='TEST' + buff[5] + buff[6] + buff[7];
SendBuffer:=str01;
send(sock1,SendBuffer,SizeOf(SendBuffer),)
end;

It says Incompatible Types 'string and array'

SendBuffer is an Array of 512 chars
and buff is an Array ofr 512 chars

they both have to be arrays or else the socket will stop working

BTW: this is for my irc, and dont want to use tstringlist.

 

Re:Sending array through socket help


var
  sbuf: array [0..512] of char;
  str01: string;
begin
  StrCopy(sbuf, str01);
  send(sock1, sbuf, sizeof(sbuf));
end;

or you can just use the string

send(socket1, Pointer(str01)^, length(str01))
works fine

"Execution" <Executio...@hotmail.com>, haber iletisinde tunlary
yazdy:3dd9b79...@newsgroups.borland.com...

Quote
> I finally learned enough of WinSock to send stuff and get stuff back.

> But im trying to send this piece of code through

> procedure send;
> var
> str01:string;
> begin
> str01:='TEST' + buff[5] + buff[6] + buff[7];
> SendBuffer:=str01;
> send(sock1,SendBuffer,SizeOf(SendBuffer),)
> end;

> It says Incompatible Types 'string and array'

> SendBuffer is an Array of 512 chars
> and buff is an Array ofr 512 chars

> they both have to be arrays or else the socket will stop working

> BTW: this is for my irc, and dont want to use tstringlist.

Re:Sending array through socket help


Quote
> procedure send;
> var
> str01:string;
> begin
> str01:='TEST' + buff[5] + buff[6] + buff[7];
> SendBuffer:=str01;

Any reason why you are not just sending str01 directly or make SendBuffer a
string too?

Quote
> they both have to be arrays or else the socket will stop working

Why?
Use,
Send(Sock1, StringVar[1], Length(StringVar)

This works and makes your copying unnecessary.

Andy

Re:Sending array through socket help


If i send a string the server wont even reply to me.
I tried making SendBuffer a string and when i send the 'info' for the server
nothing happens wont even get a ping message, but if i send the same exact
info as an array then i get the ping message to reply to.

Quote
"Andy M." <1...@123.com> wrote in message

news:3dda576a$1@newsgroups.borland.com...
Quote
> > procedure send;
> > var
> > str01:string;
> > begin
> > str01:='TEST' + buff[5] + buff[6] + buff[7];
> > SendBuffer:=str01;

> Any reason why you are not just sending str01 directly or make SendBuffer
a
> string too?

> > they both have to be arrays or else the socket will stop working

> Why?
> Use,
> Send(Sock1, StringVar[1], Length(StringVar)

> This works and makes your copying unnecessary.

> Andy

Re:Sending array through socket help


Quote
> If i send a string the server wont even reply to me.
> I tried making SendBuffer a string and when i send the 'info' for the
server
> nothing happens wont even get a ping message, but if i send the same exact
> info as an array then i get the ping message to reply to.

You're obviously doing something wrong then.
The server has no way of telling whether you're using a string or an array.
It all comes out as a stream of bytes on the other side.
A common mistake when using strings is, to use them as if they were old
pascal strings.

A string var is nothing more than a pointer to a location where the data is
stored, so, if you have a send routine that takes a typeless argument, you
can - for an array *ONLY* - use

Send(Socket, MyArrayVar, SizeOf(MyArrayVar));

for strings you *HAVE* to use instead:

Send(Socket, MyStringVar[1], Length(StringVar));

if you leave out the [1], the other side will receive garbage, which most
likely caused the problems in your case.

Andy

Re:Sending array through socket help


Quote
> This is done so often that there ought to be a label on the Delphi
> shrinkwrap, 'Beware calls that take a length as the second
> parameter - they need a pointer to the data'.  That, and PChars.

:-)

Andy

Re:Sending array through socket help


Quote

> if you leave out the [1], the other side will receive garbage,
which most
> likely caused the problems in your case.

This is done so often that there ought to be a label on the Delphi
shrinkwrap, 'Beware calls that take a length as the second
parameter - they need a pointer to the data'.  That, and PChars.

Rgds,
Martin

Other Threads