Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 2nd, 2008, 1:35 AM   #1
prototype_angel
Newbie
 
Join Date: Dec 2006
Posts: 9
Rep Power: 0 prototype_angel is on a distinguished road
Red face how to encode a struct in a string for communication (c++)(pointers)

Hi, I have a situation where I've got some programs communicating via sockets. They can only send strings to each other.
so if I try this:
c++ Syntax (Toggle Plain Text)
  1. struct mystruct
  2. {
  3. int a, b, c;
  4. char a,b,c;
  5. int array[10];
  6. someotherclass object;
  7. }
  8.  
  9. char *encode2(mystruct a)//method 2
  10. {
  11. char *ret,*ret1;
  12. ret=new char[sizeof(a)];
  13. ret[sizeof(a)]=NULL;
  14. ret=&a;
  15. strcpy(ret1,ret);
  16. }
  17.  
  18. mystruct decode(char *a)
  19. {
  20. //please help me with this
  21. }

Essentially i can only send a string and have two processes of the same program sending and reciving the same struct, so the structure of the memory will be preserved.

can someone suggest a way to efficiently do this and it should work for zero values in arrays and integers.

Thanks a lot in advance
prototype_angel
prototype_angel is offline   Reply With Quote
Old Jan 2nd, 2008, 1:59 AM   #2
Salem
Programmer
 
Join Date: Nov 2007
Posts: 33
Rep Power: 0 Salem is on a distinguished road
Re: how to encode a struct in a string for communication (c++)(pointers)

http://www.parashift.com/c++-faq-lit...alization.html
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Salem is offline   Reply With Quote
Old Jan 2nd, 2008, 2:20 AM   #3
prototype_angel
Newbie
 
Join Date: Dec 2006
Posts: 9
Rep Power: 0 prototype_angel is on a distinguished road
Re: how to encode a struct in a string for communication (c++)(pointers)

That was useful. thanks.
prototype_angel is offline   Reply With Quote
Old Jan 2nd, 2008, 3:26 AM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Re: how to encode a struct in a string for communication (c++)(pointers)

You can also use Boost.Serialization. It will handle all that work for you. Its lets you chose text, binary, or xml output. If you are on windows, you can get installers for boost here. See the tutorial, it will help you write a simple to way serialization function. You will then be able to dump the struct to a string and build the struct back from the string.

Its text format will help you avoid endian and struct data member alignment issues which you appear to be ignoring here.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 2:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 3:04 PM
how to use pointers to display a part of a string rahulsr123 C++ 10 Feb 15th, 2006 6:46 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 11:39 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 8:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC