Server/Client Communication

Hi guys,
I need a little support in this matter. The topic about server client communication is quite extensive in my opinion and to tell you the truth I don’t have yet a clue where to dig in really. So basically I have a C++ program which contains several objects with variables (the server). Now a client should have access to this objects when requested. The client right now is written in vb.net.

Please bear with me if I get anything wrong, it is not my strong suit here.
So the server should listen to a port and if a request comes in, the object should be transferred to the client. Because I don’t know any better, I’d try to achieve this by ‘MarshalByRefObject’, because the data will change frequently (therefore no serialization).

The general steps might be like this:
• Convert the objects to remotable objects (with MarshalByRefObject)
• Create app.config (protocol)
• Program the server to listen to a port
• Whatever has to be done on the client

Correct?
Right now my concerns are mainly about the server and if that is the way to go. Still of course, I do not know how to implement the steps but if I know I am running in the right direction, it is a start.
Passing data through the network involves serialization. No way to avoid this.

What sort of data do you need to move from the server to the client?
Alright, I reckon I got the terminology slightly wrong. Following up your answer, I checked again and found this statement also within this forum. As marshalling is another form of serialization (+ recording the codebase), yes there won’t be a way around that.

„Both do one thing in common - that is serializing an Object. Serialization is used to transfer objects or to store them. But:
• Serialization: When you serialize an object, only the member data within that object is written to the byte stream; not the code that actually implements the object.
• Marshalling: Term Marshalling is used when we talk about passing Object to remote objects(RMI). In Marshalling Object is serialized(member data is serialized) + Codebase is attached.
So Serialization is part of Marshalling.“


And further:

"To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially."

The objects I want to pass contain several data members (mostly data arrays - double, integers), which are non static and therefore should be updated every couple of seconds. On a basic level, I press a button in a gui on the client side and get access to this data.
That definition of marshaling is specific to Java. In Java it's possible to send an object's code with the object. In other contexts, marshaling and serialization are synonymous.
In particular, in C++ it's impossible to serialize an object's code. Even if it was possible, the VisualBasic.net client would be unable to use that code.

The objects I want to pass contain several data members (mostly data arrays - double, integers), which are non static and therefore should be updated every couple of seconds. On a basic level, I press a button in a gui on the client side and get access to this data.
Sounds really trivial. The client should simply periodically make a request and the server should respond with the data in question. I don't see any reason to make it more sophisticated.
Well, trivial is good. That answer might have saved me hours of fruitless effort.
I created a simple example where I can send a char array through a socket, which works fine (the ‚send‘ syntax actually is only able to handle char).

send(int socket, char *buffer, int length, int flags);

As a next step I will try to send the data arrays (with some converting). If that's the way to go. I reckon I will run in some problems, but for now I am in good spirits.
Thanks.

EDIT: Still have some troubles getting an array to the other side.
I can convert a single integer to a string with ‘to_string()’ and then send it.
I tried to cast it to char*, but then I will get a ‘smiley’ (2) or a ‘heart’ (3) on my client. :)

1
2
int single = 2;
char* singleInt = (char*)&single;


Maybe sprintf will do the trick?
I also read that boost provides some godd libraries, I probably gonna check that out too.
Omg, I am such a noob in this.
Last edited on
If the amount of data is fixed and only numeric, the simplest solution I can think of is with an std::stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Foo{
    int a;
    double b;
    int c[5];
    std::string to_string(){
        std::stringstream stream;
        stream << this->a << ' ' << this->b;
        for (auto i : this->c)
            stream << ' ' << i;
        return stream.str();
    }
    Foo(const std::string &s){
        std::stringstream stream(s);
        stream >> this->a >> this->b;
        for (auto &i : this->c)
            stream >> i;
    }
}
Alright, I can send now Arrays via:

1
2
char* c = reinterpret_cast<char*>(arr);
int arrSize = sizeof(arr);


and
send(sock_CONNECTION, c, arrSize + 1, NULL)

On the client side:
double(&pArray)[4] = *reinterpret_cast<double(*)[4]>(MESSAGE);

I haven’t really figured out how to use your Foo struct though.

Topic archived. No new replies allowed.