PDA

View Full Version : Sockets: Send objects and Client/Server architecture



PandMonium
5th June 2010, 00:45
Hi all,

My goal is to create a (command line) server that handles multiple clients and provides a bunch of different functionality. It will have a DB (and i've already found the database examples around :P) to store data. Clients will request data from the serve, send new data to be added, send files to be processed and so on.

I've already started looking at examples like the Threaded Fortune Server and Fortune Client, SQL browser and have some specifications and architecture design of what i want / need.

The problem is my lack of knowledge about Qt (and C++ too). I'm more used to Java, have done stuff with C but i don't know how to send complex messages or even objects between clients and server. In Java i would/could send complex objects (Message for instance). I've seen by the examples how to send a simple string by convert it to a ByteArray, is this the correct way to go on more complex stuff?

So i will just have the server waiting for strings that identify each desired action? How would i receive more complex calls that included parameters, for example get me info about books with "Qt" in title or Author. And how would i reply with vectors/arrays and objects? Is it the best/only option to send it string, int, etc. part by part in each message? :S

Hope i'm being clear.
Thanks in advance, :)
PandMonium

tbscope
5th June 2010, 08:05
A couple of things I would recommend for your design:
1. Don't use threads unless really really necessary. It will make your source code very complex.
2. Don't use blocking sockets unless really really necessary. But since you're creating the server and the clients, you don't need them.
3. Use QXmlStreamReader. You can use it to directly read xml messages from your socket. Using xml, you can send extremely complex commands and don't have to worry a great deal about parsing them.
4. If you want to support multilingual messages, check your encodings. Try Utf8 for example.

numbat
5th June 2010, 09:54
Before you reinvent the wheel have a look at some of the RPC (remote procedure call) protocols out there such as SOAP, XML-RPC, and REST. IF you do a search on each of these items with "QT" you should get Qt compatible libraries.

PandMonium
5th June 2010, 15:01
Thanks for replying :)

So, is that 'the' solution? I've already used RPCs (Java RMI for instance) but not in C/C++ and wasn't expecting to use it here since i'm just trying to do a simple prototype for now (and already late :P).
Picking the fortune client/server, if i wanted to get a list of (for example) persons with "Smith" in their name, how would i send the request and return something like a list of persons (with id, name, age)? In other words, is it possible to send more complex data over a socket?

As for the RPC idea, Qt implements any of them? The idea is actually good but i feel that the extra time i would need to gain the knowledge / understand it, eventual problems and to make it work would kill my (already few) time left. No?

Edit:
Didn't saw the first reply to my post :o Anyway it is more or less the same, using RPC-XML would be good if i could get it working fast, don't want to encounter some big problems and lose extra time since i've few days to end this prototype (only the server). The QXmlDataStream might be the solution.
About the threads, i don't plan to use them if not necessary. Obviously to have a server supporting multiple clients i will need one there. Other part would be in some routines doing serious audio/math processing that might take minutes. It uses an extra framework and my idea is to have its source in a separate thread and just launch a new one when i want to process something.
Thanks again!

wysota
6th June 2010, 09:02
So, is that 'the' solution?
It's not Java, there is no "the" solution here. You can receive a similar effect using much different means.


I've already used RPCs (Java RMI for instance) but not in C/C++ and wasn't expecting to use it here since i'm just trying to do a simple prototype for now (and already late :P).
Picking the fortune client/server, if i wanted to get a list of (for example) persons with "Smith" in their name, how would i send the request and return something like a list of persons (with id, name, age)? In other words, is it possible to send more complex data over a socket?
If you want something simple, have a look at QDataStream. I believe this is what the fortune cookie example uses as well.


As for the RPC idea, Qt implements any of them?
"Any of them" what? Qt is not Java, it's not a language, it's just a library that has *some* functionality and not *all the functionality in the world*. But you can use this library and other libraries together to obtain new possibilities.


The idea is actually good but i feel that the extra time i would need to gain the knowledge / understand it, eventual problems and to make it work would kill my (already few) time left. No?
I'm sorry but when in one sentence someone says "I lack knowledge in X" and in the next he says "I don't want to waste time learning" then I don't really see how anybody can help him. If you want a ready solution without spending time on learning then pay someone who already posseses this knowledge to do the job for you. Remember we are help to help you, not to do your job for you.

PandMonium
6th June 2010, 14:24
Nice bashing, do you feel better now?? :)

I'm not that good with english and may have explained incorrectly but i don't think i was rude or asked someone to do my work. Plus i don't see a point with that kind of reply, it is far from helping me out and just gives a bad impression about you/me/us to others visiting the forum. I think i know what Qt is and what Java is, i was just asking if Qt implements RPC-XML, SOAP or REST that i could use instead of researching for an extra library, just that.

Explaining again, i've designed something and was starting to code it using Qt. It is something simple, just a prototype for now and planed to use sockets but the Fortune Client/Server just send a string and i decided to post hopping someone would give me their opinion on what was the better way to send more data at once (for example, 2 strings one float and an array of doubles). The first 2 posts were helpful and good suggestions but i was not expecting those suggestions (RPC). My later post was just to know more details about the RPC solution and sockets. I didn't complain about Qt or the suggestions and much less did i ask to someone do my work. This is just an academic -not paid- work and i have a few days to present my first prototype, using RPC would make me pass that deadline and change some of the plans i presented before.

Anyway, for now i think i found the answer i was looking for in a book (Prentice Hall C++ GUI Programming with Qt 4), it looks simple with an example sending a bit more than just a string with a datastream/bytearray. I really appreciate all of your suggestions and will surely look at them once i've more time, after all they might be useful in other personal projects too. :)

Hope i (we?) can be friendlier in the next topics :D

Cya

wysota
7th June 2010, 08:10
Nice bashing, do you feel better now?? :)

I'm not that good with english and may have explained incorrectly but i don't think i was rude or asked someone to do my work. Plus i don't see a point with that kind of reply, it is far from helping me out and just gives a bad impression about you/me/us to others visiting the forum.
An alternative would be that you received no response at all. I think it is better to have some kind of response that at least pushes the conversation a bit forward than to be ignored.


I think i know what Qt is and what Java is, i was just asking if Qt implements RPC-XML, SOAP or REST that i could use instead of researching for an extra library, just that.
It doesn't but there are other libraries that do.


Explaining again, i've designed something and was starting to code it using Qt. It is something simple, just a prototype for now and planed to use sockets but the Fortune Client/Server just send a string and i decided to post hopping someone would give me their opinion on what was the better way to send more data at once (for example, 2 strings one float and an array of doubles). The first 2 posts were helpful and good suggestions but i was not expecting those suggestions (RPC). My later post was just to know more details about the RPC solution and sockets. I didn't complain about Qt or the suggestions and much less did i ask to someone do my work. This is just an academic -not paid- work and i have a few days to present my first prototype, using RPC would make me pass that deadline and change some of the plans i presented before.
QDataStream, as already said.

PandMonium
7th June 2010, 14:13
Indeed having a reply is always better and i appreciate all of them. I just thought your post could have been a bit nicer but saying the same things, after all bashing a new member/newbie without a big reason doesn't make the reply more helpful. I'm sorry if i didn't express well but i was not asking for someone to do my work, like i explained before.
Anyway it ended being helpful since i've found a way of dong with DataStream and ByteArray, thanks to all for helping me out :)