How to send Bytes completely via WebSocketSharp - c#

I am making a audio chat program, So I tried to sending Audio bytes via web-socket
first I did, I get audio bytes and send it on But it was failed(maybe there can't pass completely)
second I tried is convert bytes to string with use BitConverter and convert again to byte array with Encoding.UTF8.GetBytes method
this is my code
var pcmAudio = stream.ToByteArray();
var audio = Encoding.UTF8.GetBytes(BitConverter.ToString(pcmAudio));
if I send that 'audio' it works. I can convert to byte array and I can play audio.
but, if I send pcmAudio there are an error
Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
above is my receiving audio code. data is string. there's no way to receive with Byte type.
So i had to convert data to bytes.
Unfortunately it doesn't work.
the error message is 'Wave header is corrupted'
i want to send byte array compeletly.
ur question 1. Why do you want to send bytes ? You know the way to send audio with bitconverter
my answer 1. Byte length would be larger than not converted
thank you

Related

What is the structure of a NAudio capture?

I am trying to send over PCM audio using UDP via WLAN. The audio I am trying to send is the data I get from NAudio's WasapiLoopBackCapture(). When the DataAvailable event is called I send the whole buffer in one message:
var capture = new WasapiLoopbackCapture();
capture.DataAvailable += RecorderOnDataAvailable;
static void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
Byte[] msg = waveInEventArgs.Buffer;
udpClient.Send(msg, msg.Length);
Console.WriteLine(msg.Length);
Console.WriteLine(ByteArrayToString(waveInEventArgs.Buffer));
}
Now, a python script on the other end has to receive and store the bytes in order to convert them later into signed integers (I need to visualize the audio). In order to do that, I need to know what is the structure of the data when it's written to the UDP packets.
I tried looking at the hex of the bytes:
008059380080f1380080dc3800c007390040193900c03039004039390080633900404c390090873900e0583900a09b39
00206a390010b13900e07d390030c43900c0803900a0cf3900c06f3900b0d23900a05d3900a0d0390040573900c0c939
00e04f3900c0b53900e0443900608d390020443900a02f3900c04f3900408e3800e05a390000ccb700405939000007b9
00805139001082b9006052390010bcb900a05a390040eab900205f39001808ba0020593900f018ba00604d39001825ba
00e04039004829ba00202f3900c026ba00000e39007821ba0000b23800481cba0000e43700c817ba0000d8b7005814ba
00809bb8000811ba00c002b900700dba00203bb900a00bba008064b900000cba000070b900200eba00e063b9003012ba
006053b9008817ba00804bb900601dba00c03bb9006821ba004013b900b021ba0080b1b800701fba00009bb700a01cba
0080723800b016ba00801539004807ba00406e390030dab900f09c3900e09ab900f0b53900c02fb90010c3390000f9b7...
Still, I can't make sense of it, because it's basically my first time interacting with bytes at this level.
So, what is the structure of the captured audio, and does it depend on the system I am on? How do I go about converting the bytes to integers? Thanks in advance
WASAPI captures audio using IEEE floating point 32 bit samples (and will be stereo). It would make sense to convert those to 16 bit integer.

check if byte array is fully sent through TCP connection c#

I have a bitmap, I converted it to bytes using BinaryFormatter and MemoryStream then sent the bytes to the TCP server, when I try to convert it back to BitMapI get this error System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed. I tried converting the bitmap to bytes then converting the bytes to bitmap on the client side just to check if the error is due to the conversion but everything worked just fine. So I think the problem is that the server is receiving the byte of array in chunks not in 1 big array, so my question is how check if byte array is fully sent?
As you say the data can absolutely be sent in multiple chunks and you need to have a way of knowing when all the data is received. For HTTP you use Content-Length in the headers to let the client know when all data is received. Since you control both sides you can transform your image into a byte array, check the size and lets say its 5000 bytes.
Then you create an int (or long if necessary, not in this case probably) and set it to 5000 and send that first (as bytes) and then the rest of the data. You will then have created your own header. On the other side you start by reading the exact amount of bytes for an int (or other if you chose long etc). Then transform the bytes into an int and you know it will now come 5000 bytes. Then start reading until you have 5000 bytes. This can always be optimized but this is a simple way you can do it.

Read RFID tag using LibUsbDotNet

I have a generic USB RFID card reader.
I am using code from How to read from a usb rfid reader? to read the data. It seems to read okay, however, the output is a byte array. What I want to get is the RFID number, the one that's printed on the card. How can I get this?
EDIT
I successfully retrieved the tag number by implementing a key logger. It seems that the reader does not directly send the tag number down the wire, but rather, sends a command to type the tag number out. This solution works but I'm still open to other, more direct approaches.
After successfully reading the RFID tag, all you need to do is to convert the byte array into a string. Please refer to the following line of code and this link for more details.
// readBuffer: The byte array containing the sequence of bytes to decode.
// 0: The index of the first byte to decode.
// bytesRead: The number of bytes to decode.
var strRfidTag = System.Text.Encoding.Default.GetString(readBuffer, 0, bytesRead);

Sending c# string to c++ server through network

I'm writing a c# client who need to communicate with a c++ server. I'm trying to find a way to send a string to the server, and i'm stuck, since a char in c# is 2 bytes and in c++ it's 1.
How can I convert my strings to send them as a readable char array for the server?
Thanks a lot!
Ps : I will have the same problem with other types like int and stuff I think.
In C++ you can use std::wstring which is wide character and of two bytes.
You can quite easy convert your string to an ascii byte array before sending it to the server:
string message = ...
byte [] data = Encoding.ASCII.GetBytes(message);
server.Send(data);
Be sure however that you send messsages that consist of characters covered in the ascii-table. Characters outside that table can bring some surprises when converted to ascii.
Converting the received answer from the server back into a string
byte [] received = ...
string response = Encoding.ASCII.GetString(received);
Generally speaking, sending data from client to server and back through some kind of connection is not the easiest thing to do.
I can share my experience, in which I need to serialize properties of serializable classes into stream of bytes to be sent across a generic connection.
Using the System.BitConverter you can get the representation of basic data types (bool, char, double, float, ...) into arrays of bytes:
byte[] f1 = BitConverter.GetBytes(MsgID); // MsgID is a ulong
for string objects you could use the UTF8 encoding:
// == Payload is a C# string ==
// calculates how many bytes we need to stream the Payload
payloadBufferSize = Encoding.UTF8.GetByteCount(Payload);
// create a suitable buffer
payloadBuffer = new byte[payloadBufferSize];
// Encode the Payload in the buffer
Encoding.UTF8.GetBytes(Payload, 0, Payload.Length, payloadBuffer, 0);
doing so you have an array of bytes you can send through your connection, given that on the other side you have some kind of object that is able to decode an UTF8 stream of bytes.
If you just want to get a plain ASCII stream, you could use the Encoding.ASCII encoder instead of the Encoding.UTF8 in the sample above, but if you have unicode character you'll get a '?' as a resulting char.

RestSharp posting ByteArray / Stream data

trying to post a byte array or memory stream with RestSharp.
I have tried the following
request.AddFile("stream", x => new MemoryStream(blocks.First().Value), "stream", "application/binary");
And
request.AddFile("stream", blocks.First().Value, "stream", "application/binary");
Where blocks.First().Value is a Byte Array
On the server end I am expecting a form with stream parameter in it that I can extract the bytes from.
Additional information:
Adding null or string.Empty to AddFile sends the byte array
request.AddFile("stream", blocks.First().Value, string.Empty);
The problem is that it adds 2 bytes to each byte array sent (1 for carriage return and one for new line). And I cannot remove them on every post on server side since other clients do not behave this way.
Thank you for any input on this!
We fixed it with a pull request in Nancy 0.11 so this is no longer an issue.

Categories