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. Socket question: How to send different datatypes in a byte array ?

Socket question: How to send different datatypes in a byte array ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structurestutorial
6 Posts 3 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.
  • F Offline
    F Offline
    FreeCastle
    wrote on last edited by
    #1

    Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks

    C C 2 Replies Last reply
    0
    • F FreeCastle

      Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      something like this?

      BYTE bytes[100];
      int anInt = 10, anotherInt = 50;
      char *str = "Hi there";

      BYTE *pPos = bytes;
      memcpy(pPos, anInt, sizeof(int));
      pPos += sizeof(int);

      strcpy(pPos, str, strlen(str));
      pPos += strlen(str);

      memcpy(pPos, anotherInt, sizeof(int));
      pPos += sizeof(int);

      image processing toolkits | batch image processing | blogging

      1 Reply Last reply
      0
      • F FreeCastle

        Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        copying an integer value inside a byte array (an array of unsigned char, I suppose) is simple, for instance:

        int i=5;
        unsigned char buff[0x100];
        memcpy((void*)buff, (void *)&i, sizeof(i));

        but you must be aware that INTEL processors have little-endian byte order, while TCP/IP network has big-endian one. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        F 1 Reply Last reply
        0
        • C CPallini

          copying an integer value inside a byte array (an array of unsigned char, I suppose) is simple, for instance:

          int i=5;
          unsigned char buff[0x100];
          memcpy((void*)buff, (void *)&i, sizeof(i));

          but you must be aware that INTEL processors have little-endian byte order, while TCP/IP network has big-endian one. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          F Offline
          F Offline
          FreeCastle
          wrote on last edited by
          #4

          Hi, thanks that helped me. I already thought that it's something like memcpy. But there's my next question. How can I get then from this byte array the original values back ? Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?

          C 1 Reply Last reply
          0
          • F FreeCastle

            Hi, thanks that helped me. I already thought that it's something like memcpy. But there's my next question. How can I get then from this byte array the original values back ? Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            FreeCastle wrote:

            I get then from this byte array the original values back

            if you, at the opposite side of the TCP line, don't know what is the nature of the data contained inside the byte array then you are lost. So maybe you have to preceede each chunk of data with, say, a marker section (maybe few bytes) that state: (1) the type of the following items (2) the lenght of the following items Hey, you're establishing a protocol! :)

            FreeCastle wrote:

            Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?

            When you are sending bulk data maybe BYTES (hence unsigned char) are more appropriate. :) hope that helps.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            F 1 Reply Last reply
            0
            • C CPallini

              FreeCastle wrote:

              I get then from this byte array the original values back

              if you, at the opposite side of the TCP line, don't know what is the nature of the data contained inside the byte array then you are lost. So maybe you have to preceede each chunk of data with, say, a marker section (maybe few bytes) that state: (1) the type of the following items (2) the lenght of the following items Hey, you're establishing a protocol! :)

              FreeCastle wrote:

              Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?

              When you are sending bulk data maybe BYTES (hence unsigned char) are more appropriate. :) hope that helps.

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              F Offline
              F Offline
              FreeCastle
              wrote on last edited by
              #6

              Hey thanks a lot again. And you're right, I am writing in fact a little protocol :-)

              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