This project has moved and is read-only. For the latest updates, please go here.

Sending of stream via HTTP

Jul 18, 2013 at 3:15 AM
So... Here is very simple code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net;
using NAudio;
using NAudio.Wave;
using NAudio.FileFormats;
using NAudio.CoreAudioApi;

namespace MahStation
{
    delegate void Ahaha(string Text);
    delegate void Wtf();
    

    public partial class MainForm : Form
    {
        Thread BgThread;
        Server Serv = new Server();

        public MainForm()
        {
            InitializeComponent();
            Logging.Log = TextOut;
            Tumbler.CheckedChanged += Manipulate;
            Logging.Print("Система готова к работе");
        }

        private void Manipulate(object sender, EventArgs e)
        {
            if (Tumbler.Checked)
            {
                BgThread = new Thread(new ThreadStart(Serv.Begin));
                BgThread.IsBackground = true;
                BgThread.Name = "DataSend";
                BgThread.Start();
                Logging.Print("Запуск фонового процесса передачи");
            }
            else
            {
                BgThread.Abort();
                Logging.Print("Завершение фонового процесса передачи");
            }
        }
    }

    public static class Logging
    {
        public static RichTextBox Log { get; set; }
        public static void Print(string Text)
        {
            if (Log.InvokeRequired)
            {
                Ahaha d = new Ahaha(Print);
                Log.BeginInvoke(d, new object[] { Text });
            }
            else
            {
                Log.Text += Text + "\n";
                Log.SelectionStart = Log.TextLength;
                Log.ScrollToCaret();
            }
        }
        public static void Clear()
        {
            if (Log.InvokeRequired)
            {
                Wtf d = new Wtf(Clear);
                Log.BeginInvoke(d);
            }
            else
            {
                Log.Clear();
            }
        }
    }

    public class Server
    {
        WaveInEvent MicStream;
        private HttpListener Servir = new HttpListener();

        public void Begin()
        {
            MicStream = new WaveInEvent();
            MicStream.DeviceNumber = 0;
            MicStream.WaveFormat = new WaveFormat(8000, 1);
            MicStream.DataAvailable += Sending;
            Servir.Prefixes.Clear();
            Servir.Prefixes.Add("http://192.168.0.68:80/");
            MicStream.StartRecording();
        }

        public void Sending(object sender, WaveInEventArgs e)
        {
            Servir.Start();
            while (true)
            {
                HttpListenerContext Context = Servir.GetContext();
                HttpListenerRequest Request = Context.Request;
                HttpListenerResponse Answer = Context.Response;
                Stream atwet = Answer.OutputStream;
                Logging.Print(String.Format("Method: {0}, URL: {1}", Request.HttpMethod, Request.Url));
                Answer.StatusCode = (int)HttpStatusCode.OK;
                Answer.ContentLength64 = e.BytesRecorded;
                atwet.Write(e.Buffer, 0, e.BytesRecorded);
            }
        }
    }
}
But i can't read this stream via Winamp (Sync error). I know, error in my shit-code, but I can not figure out where exactly. Do not hit me strong, I'm still a beginner C:
And yeah, sorry for my bad language, i almost don`t know english =\