Board index » cppbuilder » How do I send data

How do I send data


2003-12-07 12:56:07 AM
cppbuilder69
I'm using the serversocket and clientsocket from the Internet tab. I wish to
transmit a structure that I created to the server what should I do to send
it. Once it gets to the server I wish to save the structure. Also, later I
will retrieve the data by sending a request to send file Item so and so.
Would my best bet be to sendbuffer, receivebuffer and type cast to a
structure or will things be paded after the send and recieve.
Thanks any help would be helpfull.
I'm using the example chat server program for 5.0 but using 6.0 if you can
direct me to a page to help me understand.
 
 

Re:How do I send data

Quote
I wish to transmit a structure that I created
Then you know how to create one and how it 'acts' I presume.
// Just quick...
cMyStruct
{
char SomeOption[33];
}
cMyStruct MyStruct;
// Ok, now put some info in "SomeOption"...
strncpy(MyStruct.SomeOption, "ThisIsTextInYourOption", 32+1);
Quote
Would my best bet be to sendbuffer, receivebuffer and type cast to a
structure or will things be paded after the send and recieve.
// Yeps, you could use something like;
ClientSocket->Socket->SendBuf(&MyStruct, sizeof(MyStruct));
-----------------------
// On the receiving end;
// Of course the same struct;
cMyStruct
{
char SomeOption[33];
}
cMyStruct MyStruct;
ServerSocket->Socket->ReceiveBuf(&MyStruct, sizeof(MyStruct));
// Test it...
AnsiString Bla = MyStruct.SomeOption;
ShowMessage(Bla);
You could also do a search on groups.google.com or www.mers.com.
GL, Ben.
 

Re:How do I send data

I prefer to write stuff out to a decedent of TStream and then use the
SendStream method. I usually append on some header information for your
example just the size of the structure will do.
Any class that I write which I think I might save to a file or load from a
file or send to a server, etc I always write a SaveToStream and a
LoadFromStream method for that class.
1.) read in 4 bytes
2.) convert that to an unsigned int called data_size
3.) try reading in data_size more bytes
4.) if you receive all the bytes then convert them back either via a classes
LoadFromStream or type casting...basically the reverse of what you did to
put it into the stream.
I noticed you talking about structs...which have a fixed size memory.
struct s_type
{
int a, b, c;
} x;
structure x will be exactly 12 bytes so you can do something simple like
TMemoryStream* ms = new TMemoryStream();
int data_size = sizeof(x); //should be 12 but just use this so if you change
the struct in future it will still work
ms->Write(&data, sizeof(data_size));
ms->Write(&x, data_size);
Socket->SendStream(ms);
The thing you have to be really carefull of is that just because your using
a structure which has a fixed size in memory doesn't mean your going to get
all the data when using the method above. Any pointers to anything should
raize a red flag that it wont work.
For Instance
struct s_type
{
std::string str;
int* ptrI
} x;
Be carefully of any thing like that...the pointer to the address on client
machineis not valid on the server machine and vice a versa.
 

{smallsort}

Re:How do I send data

My original post I said structure I'm assuming I can treat that the same if
it is a CLASS.
Any suggestion on how to handle CLASSes that have AnsiStrings.
Should I send the AnsiStrings seperate. And save them sep.
How do I then wait for all data arives before I continue on to use the CLASS
after rebuilding it on the client.
Also, I'm using the CLX libraries so I can run these on unix as well
(when/if i finish)
"Ben (nl)" < XXXX@XXXXX.COM >wrote in message
Quote
>I wish to transmit a structure that I created
Then you know how to create one and how it 'acts' I presume.

// Just quick...
cMyStruct
{
char SomeOption[33];
}


cMyStruct MyStruct;
// Ok, now put some info in "SomeOption"...
strncpy(MyStruct.SomeOption, "ThisIsTextInYourOption", 32+1);


>Would my best bet be to sendbuffer, receivebuffer and type cast to a
>structure or will things be paded after the send and recieve.

// Yeps, you could use something like;
ClientSocket->Socket->SendBuf(&MyStruct, sizeof(MyStruct));

-----------------------
// On the receiving end;

// Of course the same struct;
cMyStruct
{
char SomeOption[33];
}

cMyStruct MyStruct;
ServerSocket->Socket->ReceiveBuf(&MyStruct, sizeof(MyStruct));

// Test it...
AnsiString Bla = MyStruct.SomeOption;
ShowMessage(Bla);


You could also do a search on groups.google.com or www.mers.com.

GL, Ben.


 

Re:How do I send data

Hi!
Joe Vasher wrote:
Quote
My original post I said structure I'm assuming I can treat that the same if
it is a CLASS.
struct and class are the same.
Frank
PS: Cut quotes!
 

Re:How do I send data

Hi!
Junk Mail wrote:
Quote
For Instance
struct s_type
{
std::string str;
int* ptrI
} x;

Be carefully of any thing like that...the pointer to the address on client
machineis not valid on the server machine and vice a versa.
You cannot transmit the std::string this way, either. Your method works
only with "pod" (plain old data): builtin types like int, float, and
structs/classes made of these:
struct pod_1
{
int i,j,k;
float f1,f2,f3;
double d1,d3,d4;
};
struct pod_2
{
int a,b,c;
pod_1 p1,p2,p3;
};
This will work. Anything else: I doubt it. You will have to create some
kind of protocol and transmit the data that way.
Frank
 

Re:How do I send data

Quote
Should I send the AnsiStrings seperate. And save them sep.
Yep, or you could use a big char maybe... If you do want to send any
AnsiString, it may be wise to make somekind of protocol like Frank also
said, and then maybe not use any struct's.
Quote
How do I then wait for all data arives before I continue on to use the
CLASS
after rebuilding it on the client.
If it's one struct you'll send, one struct will arrive and then you can do
anything you want with it like in my short example.
If you want to send eg. a struct and then an AnsiString you should also
receive it that way/in that order.
SendBuf the struct
SendBuf the AnsiString
ReceiveBuf the struct
ReceiveBuf the AS
If you want to do it any other way, it may be wise to do what I said on top
of this reply.
GL, Ben.
 

Re:How do I send data

Hi!
Ben (nl) wrote:
Quote
If you want to send eg. a struct and then an AnsiString you should also
receive it that way/in that order.

SendBuf the struct
SendBuf the AnsiString

ReceiveBuf the struct
ReceiveBuf the AS

If you want to do it any other way, it may be wise to do what I said on top
of this reply.
You cannot SendBuf an AnsiString. sizeof(AnsiString) is always 4,
because it is just a pointer to the data. There is no other way, as to
SendBuf the data:
AnsiString as;
SendBuf(as.c_str(), as.Length()+1); //also send `\0`
When receiving, read up to the `\0` and put that data into an AnsiString.
Frank
 

Re:How do I send data

Whoops, you're absolutely right!
I actually meant Send/ReceiveText for the AnsiString, even easier.
My hands where not typing what I was thinking? :) Something like that.
Ben.
 

Re:How do I send data

"Frank Birbacher" < XXXX@XXXXX.COM >wrote in message
Quote
Hi!

Junk Mail wrote:
>For Instance
>struct s_type
>{
>std::string str;
>int* ptrI
>} x;
>
>Be carefully of any thing like that...the pointer to the address on
client
>machineis not valid on the server machine and vice a versa.

You cannot transmit the std::string this way, either. Your method works
only with "pod" (plain old data): builtin types like int, float, and
structs/classes made of these:
Thats why i posted that example telling him things like std::string and int*
x you would have to be carefull with...most of not of made that clear
enough.
In case I wasn't clear enough either on the other subject...I really suggest
having a SaveToStream and LoadFromStream method so that you can avoid these
types of problems.
 

Re:How do I send data

Now just a question about the OOP portion of this. When I send tha above
files I'm going to right a Message Class With the pertanent info about the
file Like should file be locked/read only , file names, Type of file like
is it this type of class that type (use an enum look up table) then in the
same message class I plan on writing the routines to handle proper saving of
file as well as sending and recieving the files to the client and server.
The server will only use the send/recieve/read/and write routines for each
class type and the client will use the send and recieve only.
Now to follow (best follow OOB style) should I just right the message
structure with send and recieve routines and share that between the
client/server and just add an interface for the various type of classes and
strings that I will send. ( I might have 3 or 4 different classes to
send/recieve which may even contain stringlists that have to be handled).
Perhaps even a interface for each different message type. But share the
common message structure.
I feel sometimes I'm catching onto this OOP style....