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. Managed C++/CLI
  4. How to send a message in TCP/IP?

How to send a message in TCP/IP?

Scheduled Pinned Locked Moved Managed C++/CLI
jsontutorialquestion
8 Posts 4 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.
  • P Offline
    P Offline
    pShay
    wrote on last edited by
    #1

    Hello! I need to send a structure via TCP/IP withuot using serialization! since the other side is unknown i cant use this feature. thanks

    M 1 Reply Last reply
    0
    • P pShay

      Hello! I need to send a structure via TCP/IP withuot using serialization! since the other side is unknown i cant use this feature. thanks

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      say what?!?! can you try and explain your problem a little bit. What do you mean by the other side is unknown? And, which part of sending over TCP/IP don't you understand? Please, be a little more specific about what you are trying to accomplish. It will help us give you the right answer. ---- www.digitalGetto.com

      P 1 Reply Last reply
      0
      • M mikanu

        say what?!?! can you try and explain your problem a little bit. What do you mean by the other side is unknown? And, which part of sending over TCP/IP don't you understand? Please, be a little more specific about what you are trying to accomplish. It will help us give you the right answer. ---- www.digitalGetto.com

        P Offline
        P Offline
        pShay
        wrote on last edited by
        #3

        U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?

        M M 2 Replies Last reply
        0
        • P pShay

          U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          What format is the receiving side expecting the data to be in? Find that out, and send the data in that format.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

          1 Reply Last reply
          0
          • P pShay

            U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?

            M Offline
            M Offline
            mikanu
            wrote on last edited by
            #5

            The structure that you are trying to send.. is that something you made? Is the receiving end a program that you're writing? If not, what format does it expect the data to be in? Serialization/Deserialization is a process. That's all. There is nothing magic involved. You can always write your own functions to serialize and deserialize data. Here's an example of a simple structure that is serialized into a byte array:

            struct myStruct
            {
            int iValue; // integer value
            string strValue; // zero terminated string

            public myStruct(byte\[\] sourceArray)
            {
                int k;
                
                if(sourceArray.Length > 2)
                {
                    // reconstruct int value from lo byte and hi byte
                    this.iValue = sourceArray\[0\] + sourceArray\[1\] \* 256;
                
                    // reconstruct string value
                    this.strValue = "";
                    k = 2;
                    while(sourceArray\[k\]!=0 && k\> 8) & 0xFF;  // hi byte
                
                // save the string value
                k = 0;
                while(k < this.strValue.Length)
                   tmpArray\[k + 2\] = this.strValue.ToCharArray()\[k++\];
                
                return tmpArray;
            }
            

            }

            // ...
            // test code

            byte[] t;
            myStruct A;
            myStruct B;

            A = new myStruct();
            A.iValue = 567;
            A.strValue = "test string";

            t = A.Serialize(); // t = serialized array of the strucutre
            B = new myStruct(t); // B = a copy of A, constructed by deserializing t

            obviously, once you serialize your structure into an array you can send it over TCP/IP using a WinSock ---- www.digitalGetto.com

            P 1 Reply Last reply
            0
            • M mikanu

              The structure that you are trying to send.. is that something you made? Is the receiving end a program that you're writing? If not, what format does it expect the data to be in? Serialization/Deserialization is a process. That's all. There is nothing magic involved. You can always write your own functions to serialize and deserialize data. Here's an example of a simple structure that is serialized into a byte array:

              struct myStruct
              {
              int iValue; // integer value
              string strValue; // zero terminated string

              public myStruct(byte\[\] sourceArray)
              {
                  int k;
                  
                  if(sourceArray.Length > 2)
                  {
                      // reconstruct int value from lo byte and hi byte
                      this.iValue = sourceArray\[0\] + sourceArray\[1\] \* 256;
                  
                      // reconstruct string value
                      this.strValue = "";
                      k = 2;
                      while(sourceArray\[k\]!=0 && k\> 8) & 0xFF;  // hi byte
                  
                  // save the string value
                  k = 0;
                  while(k < this.strValue.Length)
                     tmpArray\[k + 2\] = this.strValue.ToCharArray()\[k++\];
                  
                  return tmpArray;
              }
              

              }

              // ...
              // test code

              byte[] t;
              myStruct A;
              myStruct B;

              A = new myStruct();
              A.iValue = 567;
              A.strValue = "test string";

              t = A.Serialize(); // t = serialized array of the strucutre
              B = new myStruct(t); // B = a copy of A, constructed by deserializing t

              obviously, once you serialize your structure into an array you can send it over TCP/IP using a WinSock ---- www.digitalGetto.com

              P Offline
              P Offline
              pShay
              wrote on last edited by
              #6

              First of all, thank u all!!! Let me tell u the all story. What I need to do is to send a message over the net. So, I have this structure (which is the message to be sent), and then I have to send it (somehow…) Then, I receive a message which I have to decode and then, according to the header, I parse it back to a structure which I will use in the program. I know that for some of u readers it may seem like a dumb question, but since it is my first time that I have to deal with communication, please accept my ignorance. Much appreciated

              J 1 Reply Last reply
              0
              • P pShay

                First of all, thank u all!!! Let me tell u the all story. What I need to do is to send a message over the net. So, I have this structure (which is the message to be sent), and then I have to send it (somehow…) Then, I receive a message which I have to decode and then, according to the header, I parse it back to a structure which I will use in the program. I know that for some of u readers it may seem like a dumb question, but since it is my first time that I have to deal with communication, please accept my ignorance. Much appreciated

                J Offline
                J Offline
                Jun Du
                wrote on last edited by
                #7

                What is your question exactly, how to send/receive data via TCP/IP, or how to encode/decode your data? If it is the former, start from here. Best, Jun

                P 1 Reply Last reply
                0
                • J Jun Du

                  What is your question exactly, how to send/receive data via TCP/IP, or how to encode/decode your data? If it is the former, start from here. Best, Jun

                  P Offline
                  P Offline
                  pShay
                  wrote on last edited by
                  #8

                  If u put it this way, then, it is how to encode and decode the data. But remembering that I can’t use the serialization option that .net gives me. Meaning, converting a structure to a Byte array and the other way around.

                  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