首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有UDP TCP协议的直播电台

带有UDP TCP协议的直播电台
EN

Stack Overflow用户
提问于 2022-08-07 09:37:54
回答 1查看 278关注 0票数 0
代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace NetworkProgramming
{
    public class IPEndPointSample
    {
       
            public static void Main()
            {
                IPAddress host = IPAddress.Parse("149.6.43.235");
                IPEndPoint hostep = new IPEndPoint(host, 443);
                Socket sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    sock.Connect(hostep);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Problem connecting to host");
                    Console.WriteLine(e.ToString());
                    sock.Close();
                    return;
                }
                try
                {
                    sock.Send(Encoding.ASCII.GetBytes("testing"));
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Problem sending data");
                    Console.WriteLine(e.ToString());
                    sock.Close();
                    return;
                }
                sock.Close();
            }
        }
}

我想用这个xml中的ip和端口信息制作一个直播电台。

https://github.com/learnergthb/TcpUdpProtocolsConnection-XmlFileRead/blob/main/XMLFile1.xml

代码语言:javascript
运行
复制
 string HostName = Dns.GetHostName();
   Console.WriteLine("Host Name of machine =" + "21303.live.streamtheworld.com"); 
    IPAddress[] ipaddress = Dns.GetHostAddresses("21303.live.streamtheworld.com");
   Console.WriteLine("IP Address of Machine is");
  foreach (IPAddress ip in ipaddress)
   {
        Console.WriteLine(ip.ToString());
    }

我转换了21303.live.streamtheworld.com <-149.6.43.235->。

当我运行它时,它不会给出任何结果,没有错误。我该怎么做才能让收音机工作呢?谢谢大家:)

更新更新

代码语言:javascript
运行
复制
//port 443
    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    public class Program
    {
        public static void Main()
        {
            using var client = new TcpClient();
    
            var hostname = "21303.live.streamtheworld.com";
            client.Connect(hostname,443);
            using NetworkStream networkStream = client.GetStream();
            networkStream.ReadTimeout = 2000;
    
            var message = "\nHost: 21303.live.streamtheworld.com\r\n\n\r\n";
    
            Console.WriteLine(message);
    
            using var reader = new StreamReader(networkStream, Encoding.UTF8);
    
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            networkStream.Write(bytes, 0, bytes.Length);
    
            Console.WriteLine(reader.ReadToEnd());
    
        }
        
    }

新更新

代码语言:javascript
运行
复制
using NAudio.Wave;
using System.Net;
using System.Net.Sockets;

namespace tcpnadudio
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using var client = new TcpClient();

            var hostname = "21303.live.streamtheworld.com";
            client.Connect(hostname, 443);
            using NetworkStream networkStream = client.GetStream();
            Stream ms = new MemoryStream();

            PlayMp3FromUrl("21303.live.streamtheworld.com");

            void PlayMp3FromUrl(string url)
            {
                new Thread(delegate (object o)
                {
                    var response = WebRequest.Create(url).GetResponse();
                    using (var stream = response.GetResponseStream())
                    {
                        byte[] buffer = new byte[65536]; // 64KB chunks
                        int read;
                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            var pos = ms.Position;
                            ms.Position = ms.Length;
                            ms.Write(buffer, 0, read);
                            ms.Position = pos;
                        }
                    }
                }).Start();

                // Pre-buffering some data to allow NAudio to start playing
                while (ms.Length < 65536 * 10)
                    Thread.Sleep(1000);

                ms.Position = 0;
                using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
                {
                    using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                    {
                        waveOut.Init(blockAlignedStream);
                        waveOut.Play();
                        while (waveOut.PlaybackState == PlaybackState.Playing)
                        {

 
 
                             System.Threading.Thread.Sleep(100);
                        }
                    }
                }
            }
        }
    }
}

错误:新尝试

新误差

*

代码语言:javascript
运行
复制
using Microsoft.AspNet.SignalR;
using NAudio.Wave;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;

namespace RadioStream
{
    public partial class Form1 : Form
    {
        private IHubContext<byte[]> _hubContext;

        public Form1()
        {
            InitializeComponent();
        }
          
        private void  Form1_LoadAsync(object sender, EventArgs e)
        {
            Stream ms = new MemoryStream();

            PlayMp3FromUrl("http://21303.live.streamtheworld.com/METRO_FM_SC");

            async void PlayMp3FromUrl(string url)
            {
                new Thread(delegate (object o)
                {
                    var response = WebRequest.Create(url).GetResponse();
                    using (var stream = response.GetResponseStream())
                    {
                        byte[] buffer = new byte[65536]; // 64KB chunks
                        int read;
                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            var pos = ms.Position;
                            ms.Position = ms.Length;
                            ms.Write(buffer, 0, read);
                            ms.Position = pos;
                        }
                    }
                }).Start();

                // Pre-buffering some data to allow NAudio to start playing
                while (ms.Length < 65536 * 10)
                    Thread.Sleep(1000);

                ms.Position = 0;
                using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
                {
                    using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                    {
                        waveOut.Init(blockAlignedStream);
                        waveOut.Play();
                        while (waveOut.PlaybackState == PlaybackState.Playing)
                        {

                            //readonly IHubContext<Stream> _hubContext;
                            async Task Channel(IHubContext<byte[]> hubContext)
                            {
                                _hubContext = hubContext;
                            }
                            //public async Task Stream(string byte[])
                            //{
                            //    _hubContext.Clients.All.SendAsync("RadioStream", byteArray[]);
                            //}


                            
                        var client = new HttpClient();
                        var request = new HttpRequestMessage
                        {
                            Method = HttpMethod.Get,
                            RequestUri = new Uri("https://shazam.p.rapidapi.com/songs/get-details?key=-------=en-US"),
                            Headers =
{
    { "X-RapidAPI-Key", "--------------" },
    { "X-RapidAPI-Host", "shazam.p.rapidapi.com" },
},
                        };
                        using (var response = await client.SendAsync(request))
                        {
                            response.EnsureSuccessStatusCode();
                            var body = await response.Content.ReadAsStringAsync();
                            Console.WriteLine(body);
                        }// SHAZAM API
                        }

                        System.Threading.Thread.Sleep(100);
                        
                    }
                }
            }
            
        }
    }

}

StreamHub.cs

代码语言:javascript
运行
复制
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RadioStream.Hubs
{
    
        public class StreamHub : Hub
        {
            public async Task Stream(string bytee)
            {
                await Clients.All.SendAsync("RadioStream", bytee);
            }
        }
}

沙扎姆

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-07 15:21:33

下面将获得流的URL。您可以将URL放入浏览器URL中,以侦听结果,这样就可以测试它是否有效。然后编写c#代码。如果用HTTS替换HTTP,您将得到端口443,而不是80/3690:

代码语言:javascript
运行
复制
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ConsoleApp2
{
    class Program
    {

        const string URL = "http://playerservices.streamtheworld.com/pls/METRO_FM.pls";
        static void Main(string[] args)
        {
            WebRequest request = HttpWebRequest.Create(URL); 
   
            request.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 103.0.0.0 Safari / 537.36");
            request.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9");
            WebResponse response =  request.GetResponse();


            Stream stream = response.GetResponseStream();
            StreamReader sReader = new StreamReader(stream);

            List<string> urls = new List<string>();
            string line = "";
            while((line = sReader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.StartsWith("File") && line.Contains("="))
                {
                    string[] splitLine = line.Split(new char[] { '=' });
                    urls.Add(splitLine[1]);
                    Console.WriteLine(splitLine[1]);

                }
            }
            Console.ReadLine();
        }

    }
 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73266383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档