-1

I'm using socket programming to send live audio from my mobile to Desktop. I'm using C# language in Desktop side and JAVA in Mobile side. In desktop side I am receiving the audio that is send by mobile and is in the form of bytes. Can someone tell how I would convert these bytes that I have read from socket to audio file ? Below is, how I am reading bytes from socket

Console.WriteLine("Program started!!");
TcpClient client = null;
client = new TcpClient("192.168.0.113", 9999);
if (client.Connected)
{
    Console.Write("Client connected" );

    NetworkStream ns = client.GetStream();
    StreamReader sr = new StreamReader(ns);
           
    byte[] buffer = new byte[2048];
    ns.Read(buffer, 0, buffer.Length);
    Console.Write("Server>> " + sr.ReadLine());
    Console.Write("\n Audio Bytes are " );
    foreach (byte b in buffer )
    {
        if (b!=0)
        {
            Console.Write(b);
            //TODO Convert these bytes 
        }
    }
    Console.Read();
}

Below is the output: enter image description here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Aqib Naveed
  • 45
  • 1
  • 7
  • "How to read audio files from Socket in C#?" You are already doing that. You are reading the audio file and you get bytes. What do you want to achieve besides reading audio files? – Thomas Weller Feb 10 '22 at 10:43
  • Files contain bytes. Write those bytes to a file with `File.WriteAllBytes` or using a FileStream – Panagiotis Kanavos Feb 10 '22 at 10:44
  • "Can someone tell how I would convert these bytes that I have read from socket to audio file?" Use a file stream and write them into a file? – Thomas Weller Feb 10 '22 at 10:44
  • `using var fileStream=File.Create(path); ns.CopyTo(fs);` This will copy the bytes from the network stream directly to the file stream. – Panagiotis Kanavos Feb 10 '22 at 10:45
  • Does this answer your question? [How do I save a stream to a file in C#?](https://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file-in-c) – Thomas Weller Feb 10 '22 at 10:46
  • No, Because the the file is not playing the audio that i have sent from android device via socket. – Aqib Naveed Feb 10 '22 at 16:05

1 Answers1

-1

If you want to write the bytes to a file, you can create a FileStream and copy the bytes from the network stream directly to the file stream with CopyTo:

var ns = client.GetStream();
using(var fileStream=File.Create(path))
{
    ns.CopyTo(fs);
}

If you want to keep the bytes in memory and process them you can use a MemoryStream. This class is a Stream wrapper over a byte[] buffer.

using(var ms=new MemoryStream(2048))
{
    ns.CopyTo(ms);
}

var theBuffer=ms.GetBuffer();

A MemoryStream grows by reallocating its internal buffer when it gets full. Specifying the capacity can eliminate or reduce reallocations, even if the initial capacity is a rough estimate.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • ...which is no different than the duplicate I linked. – Thomas Weller Feb 10 '22 at 10:51
  • Which you did after I had already posted the comment. – Panagiotis Kanavos Feb 10 '22 at 10:51
  • 1
    5 min ago (duplicate) versus 4 min ago (answer). And you could search for duplicates as well – Thomas Weller Feb 10 '22 at 10:52
  • 1
    Thanks, I've been doing this for quite a while now. New users often have trouble understanding why a specific duplicate answers their question. If someone is confused about files or buffers, posting a duplicate will confuse them even more – Panagiotis Kanavos Feb 10 '22 at 10:53
  • @PanagiotisKanavos I have coped the bytes to the file in the way you said above. But when I change the extension of file to .mp3, (because I want audio) It says this file is not playable. – Aqib Naveed Feb 10 '22 at 16:04
  • Are you sure the data is an actual MP3 file? MP3 is a compressed format and the image you posted has a *lot* of repeating values. Where does that data come from? How was it generated? Or is this raw audio data? You can't just save raw audio to a file with an mp3 extension to make it an MP3 file – Panagiotis Kanavos Feb 10 '22 at 16:14