我是socket编程的新手,并希望使用套接字连接开发一个无线电流应用程序。
流的URI类似于http://abc11.abcradio.com:8256/stream
将主机名设置为abc11.abcradio.com,将端口设置为8256,并使用以下代码:
private async void Connect_Click(object sender, RoutedEventArgs e)
{
if (connected)
{
StatusText.Text = "Already connected";
return;
}
try
{
OutputView.Text = "";
StatusText.Text = "Trying to connect ...";
serverHost = new HostName(ServerHostname.Text);
// Try to connect to the
await clientSocket.ConnectAsync(serverHost, ServerPort.Text);
connected = true;
StatusText.Text = "Connection established" + Environment.NewLine;
dataReader = new DataReader(clientSocket.InputStream);
string receivedString = String.Empty;
uint numBytesRead;
try
{
while (true)
{
numBytesRead = await dataReader.LoadAsync(128);
StatusText.Text += numBytesRead.ToString();
}
}
catch (Exception)
{
// something went wrong in the socket connection
}
}
catch (Exception exception)
{
// If this is an unknown status,
// it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
StatusText.Text = "Connect failed with error: " + exception.Message;
// Could retry the connection, but for this simple example
// just close the socket.
closing = true;
// the Close method is mapped to the C# Dispose
clientSocket.Dispose();
clientSocket = null;
}
}
}
我到了0岁。
abc11.abcradio.com:8256在浏览器中返回包含流信息的页面,abc11.abcradio.com:8256 / stream打开播放该流的页面。
如何获取流数据?