写入nwtwork流 [英] write to a nwtwork stream
本文介绍了写入nwtwork流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部,
我是C#编程的新手.我正在尝试读取目录中的所有文件(mp3)并写入网络流...我的代码在下面...我的问题是,当我运行此程序时,歌曲无法全部播放内容...缺少一些数据...请让我知道我做错了什么地方...
all,
I am very new to C# programming. I am trying to read all the files (mp3) in a directory and writing to a network stream... my code is below... the problem I have is, when I run this program, the song is not playing with all the content... it is missing some chunks of data... please let me know where I am doing wrong...
private void button2_Click(object sender, EventArgs e)
{
string[] files= null;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
//folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
folderBrowserDialog1.SelectedPath = @"C:\MyMusic\";
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3");
songFolderTxtbox.Text = folderBrowserDialog1.SelectedPath;
}
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(s), 81);
TcpClient client = new TcpClient();
client.Connect(serverEndPoint);
NetworkStream targetStream = client.GetStream();
InitializeTheConnection(targetStream, "password", null);
if (files != null)
{
foreach (string filename in files)
{
using (Stream ms = new MemoryStream())
{
using (Stream stream = new FileStream(filename,FileMode.Open))
{
byte[] buffer = new byte[65536];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
System.Threading.Thread.Sleep(1000);
//ms.Write(buffer, 0, read);
targetStream.Write(buffer, 0, read);
}
}
while (ms.Length < 65536 * 10)
System.Threading.Thread.Sleep(1000);
targetStream.Flush();
ms.Position = 0;
}
}
}
}
感谢
--r
-由Caydence编辑代码块
thanks
--r
-Edit by Caydence for code block
推荐答案
我会将targetStream.Flush()移至从内存流中读取"中的targetStream.write()之后.写入网络流循环.这样可以确保写入网络流的所有内容都立即移到另一端的设备.
I would move targetStream.Flush() to just after targetStream.write() in the read-from-memory-stream-write-to-network-stream loop. This insures that everything written to the network stream gets moved immediately to the device at the other end.
这篇关于写入nwtwork流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文