Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
all,
I am very new to C# programming. I am trying to read all the files (mp3) in a directory and writing to a network stream... my code is below... the problem I have is, when I run this program, the song is not playing with all the content... it is missing some chunks of data... please let me know where I am doing wrong...
C#
private void button2_Click(object sender, EventArgs e)
{
    string[] files= null;
    string appPath = Path.GetDirectoryName(Application.ExecutablePath);
    //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
    folderBrowserDialog1.SelectedPath = @"C:\MyMusic\";
    DialogResult result = folderBrowserDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3");
        songFolderTxtbox.Text = folderBrowserDialog1.SelectedPath;
    }
    IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(s), 81);
    TcpClient client = new TcpClient();
    client.Connect(serverEndPoint);
    NetworkStream targetStream = client.GetStream();
    InitializeTheConnection(targetStream, "password", null);

    if (files != null)
    {
        foreach (string filename in files)
        {
            using (Stream ms = new MemoryStream())
            {
                using (Stream stream = new FileStream(filename,FileMode.Open))
                {
                    byte[] buffer = new byte[65536];
                    int read;
                    while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        System.Threading.Thread.Sleep(1000);
                        //ms.Write(buffer, 0, read);
                        targetStream.Write(buffer, 0, read);
                    }

                }
                while (ms.Length < 65536 * 10)
                    System.Threading.Thread.Sleep(1000);
                targetStream.Flush();
                ms.Position = 0;
            }
        }
    }
}
thanks
--r

-Edit by Caydence for code block
Posted
Updated 23-Apr-12 10:47am
v3
Comments
Sergey Alexandrovich Kryukov 23-Apr-12 15:08pm    
The question would not make any sense until you show the code sample for the opposite side, the one reading form the stream (do I understand you right that the network stream is targetStream?); you should also show where network stream instances are obtained. And, to start with, your Thread.Sleep call is a big abuse; you never should do such things.

If you need some help, you need to create a very short but complete code sample, with both participants in your network transport.
--SA
noviceincsharp 23-Apr-12 16:42pm    
Hi SA,
I added the full code...
thanks
--R
Sergey Alexandrovich Kryukov 25-Apr-12 16:18pm    
I don't really understand... you are supposed to have two sides -- sending and receiving -- are you not?
--SA

1 solution

I would move targetStream.Flush() to just after targetStream.write() in the read-from-memory-stream-write-to-network-stream loop. This insures that everything written to the network stream gets moved immediately to the device at the other end.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900