簡體   English   中英

帶有 UDP TCP 協議的直播電台

[英]Live Radio With UDP TCP Protocols

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();
            }
        }
}

我想使用 ip 和此 xml 中的端口信息制作直播電台

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

 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->。

當我運行它時,它沒有任何結果,也沒有錯誤。 我應該怎么做才能讓收音機工作? 感謝大家:)

下面將獲取 stream 的 URL。 您可以將 URL 放入瀏覽器 URL 以聽到結果,以便測試它是否有效。 然后編寫 c# 代碼。 如果您將 HTTP 替換為 HTTS,您將獲得端口 443 而不是 80/3690:

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();
        }

    }
 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM