Copying data to bytes for sockets
-
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?
-
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?
Assuming you have a string: string dataToSend = "lotsandlotsandlotsofdata"; Byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dataToSend);