Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sending data - TCP connection

Sending data - TCP connection

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialcomsysadminquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B bigdenny200

    Hey all, I have implemented a simple TCP server/client class from the following web-site (http://www.codeproject.com/internet/winsockintro02.asp). Which shows us how to send raw char* data over the TCP connection. My problem is that I want to be able to send a whole instance of a class, for example ClassFoo objectFoo; i.e. I want to be able to send the object: objectFoo, instead of just text, i.e. "hello". Can anyone help me, by hinting how to achieve this ? Thanks

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #3

    bigdenny200 wrote:

    Can anyone help me, by hinting how to achieve this ?

    It's called "Serialization". You might want to use it as a keyword in Google and read some material to gain a basic understanding of it.

    B 1 Reply Last reply
    0
    • J Jim Crafton

      You need to do what is frequently referred to as "Marshalling", in other words, you need to convert an instance of your class into a byte stream, and then, when you recv this byte stream from a source, you need to be able to "reconstitute" it. Look up marshalling for ideas on how to do this. COM supports this and performs it automatically (as well as allowing you to customize the process if you're crazy enough). I think .Net supports this as well. There are C++ frameworks that also support this, you can look for them.

      ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #4

      Jim Crafton wrote:

      You need to do what is frequently referred to as "Marshalling"

      :confused: Isn't marshalling used in terms of memory where reconstituting objects is referred to as serialization?

      J 1 Reply Last reply
      0
      • L led mike

        Jim Crafton wrote:

        You need to do what is frequently referred to as "Marshalling"

        :confused: Isn't marshalling used in terms of memory where reconstituting objects is referred to as serialization?

        J Offline
        J Offline
        Jim Crafton
        wrote on last edited by
        #5

        Actually I was going to ask you the same question! I was going to say "serialization" but then I *thought* (always a dangerous idea) that serialization was just a way to store object data, not necessarily create a new object from scratch. Whereas marshalling was actually the ability to send the object across the wire with all the info needed to completely recreate it from scratch on the other side. Now that I think about it, I suspect I'm getting my "definition" from how COM does things, which may or may not be the way most other OO frameworks/libraries do things.

        ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

        L 1 Reply Last reply
        0
        • L led mike

          bigdenny200 wrote:

          Can anyone help me, by hinting how to achieve this ?

          It's called "Serialization". You might want to use it as a keyword in Google and read some material to gain a basic understanding of it.

          B Offline
          B Offline
          bigdenny200
          wrote on last edited by
          #6

          Dear led, thanks for reply. I found a tutorial, which should have done a serialization, as you suggested. Below is the code (with my object, instead the one from tutorial):

            // Serialize the object
            std::vector word\_list;
          word\_list.push\_back(\_T("HI"));
          ofstream ofs("c:\\\\fifthgrade.ros", ios::binary);
          ofs.write((char \*)&word\_list, sizeof(word\_list));
          
          // Deserialize it
          std::vector word\_list2;
          ifstream ifs("c:\\\\fifthgrade.ros", ios::binary);
          ifs.read((char \*)&word\_list2, sizeof(word\_list)); // last line
          

          The problem is that this code (the last line) doesnt work. And I think this is because I am using not simple Class instances, but a vector object. Do you have any idea, what could be other reason for it not to work ? thanks

          L 1 Reply Last reply
          0
          • B bigdenny200

            Dear led, thanks for reply. I found a tutorial, which should have done a serialization, as you suggested. Below is the code (with my object, instead the one from tutorial):

              // Serialize the object
              std::vector word\_list;
            word\_list.push\_back(\_T("HI"));
            ofstream ofs("c:\\\\fifthgrade.ros", ios::binary);
            ofs.write((char \*)&word\_list, sizeof(word\_list));
            
            // Deserialize it
            std::vector word\_list2;
            ifstream ifs("c:\\\\fifthgrade.ros", ios::binary);
            ifs.read((char \*)&word\_list2, sizeof(word\_list)); // last line
            

            The problem is that this code (the last line) doesnt work. And I think this is because I am using not simple Class instances, but a vector object. Do you have any idea, what could be other reason for it not to work ? thanks

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #7

            bigdenny200 wrote:

            I found a tutorial

            Please provide a link. Either it is not adequate or you have not understood it correctly. I would think most people would take longer than a single hour of studying to understand serialization. You are trying to understand it right? Not just trying to reproduce the tutorial source code for your purposes, that's not going to go well. You need to study it with the goal of understanding it.

            B 1 Reply Last reply
            0
            • J Jim Crafton

              Actually I was going to ask you the same question! I was going to say "serialization" but then I *thought* (always a dangerous idea) that serialization was just a way to store object data, not necessarily create a new object from scratch. Whereas marshalling was actually the ability to send the object across the wire with all the info needed to completely recreate it from scratch on the other side. Now that I think about it, I suspect I'm getting my "definition" from how COM does things, which may or may not be the way most other OO frameworks/libraries do things.

              ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #8

              Jim Crafton wrote:

              Now that I think about it, I suspect I'm getting my "definition" from how COM does things, which may or may not be the way most other OO frameworks/libraries do things.

              Well I just checked an according to the Wikipedia they are interchangeable. From my experience I learned about marshalling way back in Remote Procedure Calls and Serialization came later in terms of Objects so I made the connection that way. Learn something new everyday.

              1 Reply Last reply
              0
              • L led mike

                bigdenny200 wrote:

                I found a tutorial

                Please provide a link. Either it is not adequate or you have not understood it correctly. I would think most people would take longer than a single hour of studying to understand serialization. You are trying to understand it right? Not just trying to reproduce the tutorial source code for your purposes, that's not going to go well. You need to study it with the goal of understanding it.

                B Offline
                B Offline
                bigdenny200
                wrote on last edited by
                #9

                I agree with what you are saying, but the problem is that I am working on a project which is due soon. And I cant devote too much time to serialization only. But, in this case, I really thought that using the tutorial was supposed to work and would suffice. Here it is: http://www.functionx.com/cpp/articles/serialization.htm, and it seemed pretty easy for me from here. Thanks

                L 1 Reply Last reply
                0
                • B bigdenny200

                  I agree with what you are saying, but the problem is that I am working on a project which is due soon. And I cant devote too much time to serialization only. But, in this case, I really thought that using the tutorial was supposed to work and would suffice. Here it is: http://www.functionx.com/cpp/articles/serialization.htm, and it seemed pretty easy for me from here. Thanks

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #10

                  Yeah, I would not recommend that tutorial. The example "class" used is not a class, it is a simple 'C' data structure and there has been intrinsic support for reading and writing 'C' data structures since.... well 'C'. That is not the same thing as 'Serialization' of complex C++ classes that may contain pointers which are memory addresses. You cannot serialize a memory address. You must serialize/deserialize the data at the memory address which requires knowledge of the data structure. Knowledge of the data structure is something that only the developer has meaning you cannot rely on the compiler to properly serialize your class.

                  bigdenny200 wrote:

                  but the problem is that I am working on a project which is due soon. And I cant devote too much time to serialization only.

                  I can't help the fact that poor project management and planning conflicts with reality. I am only a software developer not a magician that can bend time and warp reality. Perhaps your situation now calls for the Post Agile Manifesto approach to software development[^]. I don't recommend it, but if that's what you must do.... whatever.

                  B 1 Reply Last reply
                  0
                  • L led mike

                    Yeah, I would not recommend that tutorial. The example "class" used is not a class, it is a simple 'C' data structure and there has been intrinsic support for reading and writing 'C' data structures since.... well 'C'. That is not the same thing as 'Serialization' of complex C++ classes that may contain pointers which are memory addresses. You cannot serialize a memory address. You must serialize/deserialize the data at the memory address which requires knowledge of the data structure. Knowledge of the data structure is something that only the developer has meaning you cannot rely on the compiler to properly serialize your class.

                    bigdenny200 wrote:

                    but the problem is that I am working on a project which is due soon. And I cant devote too much time to serialization only.

                    I can't help the fact that poor project management and planning conflicts with reality. I am only a software developer not a magician that can bend time and warp reality. Perhaps your situation now calls for the Post Agile Manifesto approach to software development[^]. I don't recommend it, but if that's what you must do.... whatever.

                    B Offline
                    B Offline
                    bigdenny200
                    wrote on last edited by
                    #11

                    > I can't help the fact that poor project management and planning conflicts with reality. I am >only a software developer not a magician that can bend time and warp reality. Perhaps your >situation now calls for the Post Agile Manifesto approach to software development[^]. I don't >recommend it, but if that's what you must do.... whatever. There is no project management, its a University project. Had a lazy life before ;) So now, I am tight in my deadlines.

                    M 1 Reply Last reply
                    0
                    • B bigdenny200

                      > I can't help the fact that poor project management and planning conflicts with reality. I am >only a software developer not a magician that can bend time and warp reality. Perhaps your >situation now calls for the Post Agile Manifesto approach to software development[^]. I don't >recommend it, but if that's what you must do.... whatever. There is no project management, its a University project. Had a lazy life before ;) So now, I am tight in my deadlines.

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      bigdenny200 wrote:

                      So now, I am tight in my deadlines.

                      Hmm....in the 5 hours this thread has been around, you probably could have researched and implemented serialization for a class ;P Whether or not you use MFC, I would take a look at Serialization (MFC)[^] This is an example of a fairly robust, generic, serialization solution. On the other (easy) end of the solution spectrum, you could simply add a serialize method and an unserialize method to your class, both of which take a socket as a parameter. The serialize method would write (binary) all the class' data, one at a time, to the socket. The unserialize method would read the binary data into the class' members, in the same order it was serialized.  That's a simple, socket-only class serialization solution. led mike...sorry for posting in your thread :) Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      B 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        bigdenny200 wrote:

                        So now, I am tight in my deadlines.

                        Hmm....in the 5 hours this thread has been around, you probably could have researched and implemented serialization for a class ;P Whether or not you use MFC, I would take a look at Serialization (MFC)[^] This is an example of a fairly robust, generic, serialization solution. On the other (easy) end of the solution spectrum, you could simply add a serialize method and an unserialize method to your class, both of which take a socket as a parameter. The serialize method would write (binary) all the class' data, one at a time, to the socket. The unserialize method would read the binary data into the class' members, in the same order it was serialized.  That's a simple, socket-only class serialization solution. led mike...sorry for posting in your thread :) Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        B Offline
                        B Offline
                        Bram van Kampen
                        wrote on last edited by
                        #13

                        Mark Salsbery wrote:

                        On the other (easy) end of the solution spectrum, you could simply add a serialize method and an unserialize method to your class, both of which take a socket as a parameter.

                        But that will ofcourse only work if the class contains no pointers or virtual functions. If you have already a successful way to save and retrieve to and from a file, your're nearly there if you have no virtual functions. Just use the same code to write to and retrieve from the socket. Hope you get it together in time :)

                        Bram van Kampen

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups