8 Replies - 603 Views - Last Post: 27 September 2010 - 07:14 AM Rate Topic: -----

#1 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 23-July 10

Streaming Files over a network

Posted 25 September 2010 - 09:47 PM

Hey guys, I have tried and tried to have a simple app that is capable of streaming a music file from a server, and saving it in the folder of the client.
If anyone can recommend a good tutorial, it would be very greatly appreciated.
I have literally been sitting here for hours trying.

Thanks guys.
Note: This isn't homework, its just a personal thing.
Is This A Good Question/Topic? 0
  • +

Replies To: Streaming Files over a network

#2 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Streaming Files over a network

Posted 26 September 2010 - 02:28 PM

Well depending on the file type, it shouldn't really be much different from streaming any file, as long as the first chunks can play without needing a full file etc. I'm not too sure as I haven't made a music streamer before, but if you simply open a connection and start sending bytes, you can then save and play them on the other side.

What you might want to do, is once you receive a chunk and play it, when you're half way through you could send from the player to the server a 'give me more' and that could then send the next chunk, or, you could buffer it by getting lets say, 5 chunks, and then start playing, and ask for more. You -may- download the file pretty quickly, which then takes away the streaming, over a LAN network, but over the internet, this should work ok.

The main thing here is to go ahead and do some code, don't always rely on tutorials, always have a go yourself first. That's the way I always do it.

So, if you know about how to send a file using sockets, try that, if you don't, then search for sending files with sockets rather than streaming with it. Maybe even just read about sending text through sockets first?

Good luck
Was This Post Helpful? 0
  • +
  • -

#3 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 23-July 10

Re: Streaming Files over a network

Posted 26 September 2010 - 02:35 PM

This is the thing, I got the bytes to send over the network(I got them to output into the console, and it worked), but it will not save them to the file, not matter what I try.
Its really annoying me, because it would help if the file would actually save.
It creates it, opens it, and leaves it empty.
So, I guess I can do the stream, just not the save.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Streaming Files over a network

Posted 26 September 2010 - 02:44 PM

How about posting the code that doesn't work? Wouldn't that make sense???
Was This Post Helpful? 1
  • +
  • -

#5 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 23-July 10

Re: Streaming Files over a network

Posted 26 September 2010 - 02:53 PM

True, I shall post it.
I'd think it would have an error of some type:
NetworkStream netStream = tcpClient.GetStream();
            StreamReader reader = new StreamReader(netStream);
            char[] b = new char[10240];
            //string tempFile = Path.GetTempFileName();
            
            //Console.WriteLine("[debug]Temp File Path: " + tempFile);
            Random rnd = new Random();
            //rnd.Next(10252, 929287);
            string rn = Convert.ToString(rnd.Next(10252, 929287));
            string nSong = song + rn + ".wav";
            //FileInfo tmpFile = new FileInfo(nSong);
            Stream tehStrm = tcpClient.GetStream();
            FileStream tmpWrite = File.Open(nSong, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            StreamWriter str = new StreamWriter(tehStrm);
            byte[] buffer = new byte[32768];
            string strReadLine = null;
            do
            {
                strReadLine = reader.ReadLine();
                if (strReadLine != null & !strReadLine.Contains("endBin"))
                {
                    //str.WriteLine(strReadLine);
                    str.Write(strReadLine);
                    str.Flush();
                    //Console.Write(strReadLine);
                }
            } while (!strReadLine.Contains("endBin"));
            Console.WriteLine("Done...");
            str.Flush();
            str.Close();

Its messy and may have unused code, but thats all from when I have been trying to make it work.

Is it too screwed up for words? :P
Was This Post Helpful? 0
  • +
  • -

#6 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Streaming Files over a network

Posted 26 September 2010 - 03:23 PM

It's too late and the code is too messy for me to pull it apart, but I will tell you the way I code. Each part of my code which does something different, gets put into a different method. So I have a method that takes the connection, I then pass the connection to one which reads the bytes, I then have one which saves the bytes to a file. This way, my code is not only readable but also I am able to do unit tests and find out which areas of my code.. well.. don't work as intended.

I would recommend you receive the bytes first, then pass them to a method called SaveBytesToFile etc, also, try setting break points and stepping through your code with F10 and checking that you're actually receiving data.

We call the above method debugging :)
Was This Post Helpful? 0
  • +
  • -

#7 cokacola  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 23-July 10

Re: Streaming Files over a network

Posted 26 September 2010 - 04:02 PM

It might not matter, as I am currently redoing it using the Lidgren networking library, so maybe I can make it work with that :D
and I bet there are plenty of tutorials on saving a file properly :P
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Streaming Files over a network

Posted 27 September 2010 - 06:34 AM

It's pretty simple: read some data from one stream, write that data to another stream. You seem to be working with some weird format though. If this is a video file, I'm not sure why you're looking for endBin in the input stream's data; it should be all binary data.
Was This Post Helpful? 1
  • +
  • -

#9 merlinfire  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 114
  • Joined: 23-February 09

Re: Streaming Files over a network

Posted 27 September 2010 - 07:14 AM

I might be reading your code wrong, but I don't see where you are actually connecting your stream writer with the filestream tmpWrite. So you never fill that file with data, and you never close/flush it. So it never has anything in it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1