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#
  4. Copying data to bytes for sockets

Copying data to bytes for sockets

Scheduled Pinned Locked Moved C#
questionc++
2 Posts 2 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.
  • N Offline
    N Offline
    NorthWoodsman
    wrote on last edited by
    #1

    What is the graceful way of moving your data to byte[]'s to be sent over the wire? In C++, I'd do something like this struct SomeDataHeader { AnEnum SomeDescriptiveFlag DWORD dwDataLength } void SendThis(SOCKET &rS, LPCTSTR szData) { SomeDataHeader h; h.SomeDescriptiveFlag = SDH_RANDOMSTUFF; h.dwDataLength = lstrlen(szData) + 1; BYTE *pbData = new BYTE[sizeof(h) + h.dwDataLength]; memcpy(pbData, h, sizeof(h)); memcpy(&pbData[sizeof(h)], szData, h.dwDataLength); Send(rS, pbData, sizeof(h) + h.dwDataLength); } (Note that this code is pretty sloppy/probably not right, but you get my idea) What I'm trying to do is send arbitrary data over the line; What's the best way to do this?

    R 1 Reply Last reply
    0
    • N NorthWoodsman

      What is the graceful way of moving your data to byte[]'s to be sent over the wire? In C++, I'd do something like this struct SomeDataHeader { AnEnum SomeDescriptiveFlag DWORD dwDataLength } void SendThis(SOCKET &rS, LPCTSTR szData) { SomeDataHeader h; h.SomeDescriptiveFlag = SDH_RANDOMSTUFF; h.dwDataLength = lstrlen(szData) + 1; BYTE *pbData = new BYTE[sizeof(h) + h.dwDataLength]; memcpy(pbData, h, sizeof(h)); memcpy(&pbData[sizeof(h)], szData, h.dwDataLength); Send(rS, pbData, sizeof(h) + h.dwDataLength); } (Note that this code is pretty sloppy/probably not right, but you get my idea) What I'm trying to do is send arbitrary data over the line; What's the best way to do this?

      R Offline
      R Offline
      Rein Hillmann
      wrote on last edited by
      #2

      Assuming you have a string: string dataToSend = "lotsandlotsandlotsofdata"; Byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dataToSend);

      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