我正在尝试在C#中创建一个简单的控制台应用程序。我需要制作流媒体视频,必须直接从浏览器访问。
我正在审核链接Streaming large video files .net,但似乎此代码适用于asp.net
我尝试的另一件事是TCPCliente,但在发送大文件时遇到了问题。
var fs = File.Open("../../video.mp4", FileMode.Open);
outHeaders.Add("Content-Type", "video/mp4");
outHeaders.Add("Accept-Ranges", "0-" + fs.Length);
long size = fs.Length;
long length = size;
long startBytes = 0;
long endBytes = size - 1;
if (headers["Range"] != null)
{
var msg = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 206 Partial Content" + "\n");
stream.Write(msg, 0, msg.Length);
var range = ParseRange(size, headers["Range"].ToString());
startBytes = range.Start;
endBytes = range.End;
fs.Seek(startBytes, SeekOrigin.Begin);
length = endBytes - startBytes + 1;
}
else
{
var msg = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK" + "\n");
stream.Write(msg, 0, msg.Length);
}
outHeaders.Add("Content-Length", length);
outHeaders.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startBytes, endBytes, size));
SendHeader(stream, outHeaders);
int bytesRead = 0;
byte[] buf = new byte[client.SendBufferSize];
while (fs.Position <= endBytes &&
(bytesRead = fs.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, bytesRead);
stream.Flush();
}
fs.Close();
问题在于:
while (fs.Position <= endBytes &&
(bytesRead = fs.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, bytesRead);
stream.Flush();
}
当fs.Position = 425984时,我收到错误:
No se controló System.IO.IOException
HResult=-2146232800
Message=No se pueden leer los datos de la conexión de transporte: Se ha forzado la interrupción de una conexión existente por el host remoto.
Source=System
StackTrace:
en System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
en HttpServer.Program.Main(String[] args) en j:\HttpServer\HttpServer\Program.cs:línea 85
en System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
en System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
Message=Se ha forzado la interrupción de una conexión existente por el host remoto
Source=System
ErrorCode=10054
NativeErrorCode=10054
StackTrace:
en System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
en System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
InnerException:
在stream.Write(buf, 0, bytesRead);
行
我该怎么办? THX