views:

188

answers:

2

Hi

Could someone please help and tell me if there is a possibility to pass an xml schema through a socket program written in C/C++ ? here is an example:
---- c/C++ ----
...

struct zone { // as an example
char *Var_name="xxx";
float var_value = 1.3;
};

----- xml ---
...
<xs:element name="zone">
<xs:complexType>
<xs:sequence>
<xs:element name="Var_name" type="xs:string"/>
<xs:element name="var_value" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>

------ C/C++ ----
...
// send message to server
if (send(csocket, buffer_snd, BUFSIZE, 0) > BUFSIZE)
cout << send() failed << endl;
...

Is this the way to exchange data as XML to avoid endianness when communicating between program running on Unix and other on Windows?

Thanking you in advance for your replies,

+4  A: 

Anything that can be represented as a series of bytes can be sent via a socket. Since an XML schema is in textual form, it can be represented as a series of bytes. Hence, it can be sent via a socket.

And as additional information, not only must it be expressible as a series of bytes, it must also have some meaningful representation on the other end of the wire. Hence, a pointer to an object can be represented as a series of bytes, and you could send it over a socket, but it has no meaning when it's received by the receiver. An XML schema however, has meaning on either end, so it can be meaningfully sent over.

Edit:

std::string s;
s = "this is the static part of the string";
s += GetSomeStringFromSomewhere(); // This is a string that was dynamically retrieved
// And so on...
blwy10
thanks for your reply. Actually I know the theory about but how to pass as a class/struct?
make
Use something called serialization. This depends on your programming language. But why do you want to serialize a class/struct representing an XML schema? Why not just send the schema?
blwy10
send the schema, how?
make
do you mean as a file? If yes, it isn't what I am looking for?
make
Send it as a string, like a byte string. Any socket library you use will have a function that takes in a byte string that you can send over. Any string type holding the schema in memory can be converted into a byte string. Just send it across using that function.
blwy10
using soap, is this possible?
make
It is, but it's not necessary. SOAP is a service-oriented protocol, which is not particularly relevant to sending generic XML schema. If you just want to transfer the schema, then just transfer it as a raw string. If you want to implement more sanity checking, then send the string size first, then send the actual string, so that you know how much to read off the socket.
blwy10
It is still a problem. Yes! we can send xml schema as an array of strings when the values are fixes, which is not the case as the values may change. see question I modified few things ...
make
Then what you should do is build the string dynamically. Your string will have hardcoded parts that stay the same, and then some way of inserting values that change. Then you send over.
blwy10
Thanks! please give an example how to build the string dynamically?Actually I don't how inserting values can change in the strings? Could you please give an example. This is very interesting what you said, so let's share it ... thanks again
make
@mk How much C++ do you know?
blwy10
+2  A: 

When you uploaded your question to SO, the sample schema was sent via a socket. When we viewed your question, it was sent via a socket again (and both transmissions appear to have succeeded).

Right now, it's apparent that the code you gave wasn't a direct copy and paste though, because it clearly won't compile exactly as-is. Our inability to see the real code you tried to use is a significant handicap in diagnosing any problems it may include.

Jerry Coffin
yes! I know and this is a theory, but I am looking for a solution. If you know ... tell me than, how? Thanks!
make
@mk: You first have to tell us what problem you are facing passing XML schema over socket. As blwy10 has pointed out any byte array data can be sent over socket. There should not be any problem sending XML schema over socket as it can be expressed as array of characters.
Xinus
this is an idea. It would be nice if you can give an example? thanks!
make