473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

how do i send and receive a classType over aTcp network in Qt.

7
Qt problem
Hello all,
i would like to know how i can prepair(or encode) a class type for sending over a TCP connection. and when sent, how do i decode that info at the other end. That should be done in Qt (c++).
Thanks
I am sorry, i dont have any example, but if you have any that explains this situation, i will be very greatifull.
Aug 2 '10 #1
3 2066
Banfa
9,065 Expert Mod 8TB
Serialise (encode) or marshal the class data into a byte array. Send the array then at the other end deserialise (decode) or unmarshal the received byte array back into a class.
Aug 2 '10 #2
ssmny
7
thanks banfa,
could u please give me an example of how i would do that. am not familia with Qt. I have have even failed getting a start.
Aug 2 '10 #3
Banfa
9,065 Expert Mod 8TB
Serialisation is not as complex as it sounds, you just split the data up into bytes so for a 4 byte long variable it would look something like

Expand|Select|Wrap|Line Numbers
  1. long startValue, endvalue;
  2. unsigned char buffer[4];
  3.  
  4. //Serialise to big endian data buffer
  5. startValue = 54321;
  6.  
  7. buffer[0] = (unsigned char)((startValue >> 24) & 0xFF);
  8. buffer[1] = (unsigned char)((startValue >> 16) & 0xFF);
  9. buffer[2] = (unsigned char)((startValue >> 8) & 0xFF);
  10. buffer[3] = (unsigned char)(startValue & 0xFF);
  11.  
  12. //Deserialise from big endian data buffer
  13. endvalue  = ((long)buffer[0]) << 24;
  14. endvalue |= ((long)buffer[1]) << 16;
  15. endvalue |= ((long)buffer[2]) << 8;
  16. endvalue |= (long)buffer[3];
  17.  
Just do that for all the POD (plain old data) variables in the class and similarly in a recursive fashion for any composite types the class holds.
Aug 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: T.S.Negi | last post by:
Hi All, I have to write a stored procedure which will send/Receive text file from/to a server by using FTP. Is anybody have done anything on it? or know about it. If yes, I would like to know...
1
by: **** KiteOregon **** | last post by:
I need to send & receive XML Document Objects via the Message Queue. We have several applications that insert XML Document Object into the message queue. This works with no problem. I am trying...
1
by: syncman | last post by:
I know C++ pretty well, but there are some things I have never tried in C++. I want to make a super-fast program to perform a few tasks in parallel (with threads), and eventually send data over...
1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
1
by: EppO | last post by:
I wrote a small server/client app. I created a class for my customized socket object using (synchronous) Sockets functions for both server and client use. When client connects to server, if It...
2
by: Mohammad-Reza | last post by:
Hi I want to know is there any way to send structures instance to another computer through the network with sockets? When Send() method only accept byte, How can I send them to the network? ...
4
by: saurabhaggarwal | last post by:
Hi Could anyone please tell me how can we capture send/receive event in MS Outlook. Suppose I want to pop up a message box when user hits send/receive button, how can we do that. Actually...
0
by: ChopDown | last post by:
Hi, I want to send/receive a serializable object in socket: ==========sender========== Dim MyMessage As New MessageClass.Message() Dim client As New TcpClient...
5
by: SungHyun Nam | last post by:
Hello, If there are two ethernet device, how I can select a device to send/receive? Actually I want to do loopback test between twe ethernet device. Send a UDP packet through eth0 and receives...
4
by: opkarmalkar | last post by:
I want to interface 8051 mcu to pc using USB port. I want to know that how can i send/receive a byte from pc to 8051 mcu via USB port using C language? is there any C program availaible which can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.