471,117 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,117 software developers and data experts.

NetworkStream.Write giving "forcibly closed" upon streaming

I'm trying to implement streaming over http and got stuck with a problem trying to send chunks of data using the .Net NetworkStream object.

All works fine as long as I send the entire data in one invocation to the NetworkStream, like:
Expand|Select|Wrap|Line Numbers
  1. _stream.Write(buffer, 0, buffer.Length)
But if i try:
Expand|Select|Wrap|Line Numbers
  1. byte[] buffer = new byte[4196];
  2. int bytesRead = Body.Read(buffer, 0, 4196);
  3. while (bytesRead > 0)
  4. {
  5.     _stream.Write(buffer, 0, bytesRead);
  6.     bytesRead = Body.Read(buffer, 0, 4196);
  7. }
I get the following exception: (sorry for the mess)
HttpRequestParser.ParseMessage => HttpClientContextImp.OnRequestCompleted => HttpServer.SetupRequest | Failed to respond on message with Internal Server Error: System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)

Only difference I can see is that the stream sends the data in bits (I do send some http headers in both case but ive stripped some of that code since its productioncode). Another twist is that it works when sending the code to for example Firefox but when trying to stream it to ie embedded data in a webpage it breaks.

Any help would be greatly appreciated.
Aug 19 '08 #1
1 4256
7,872 Expert 4TB
Did you send the "connection: keep-alive" header?
I'm not sure how streaming fits into the http protocol (I knwo it can be done, i've seen it) but the HTTP protocol specifies you give a content length, maybe there is something in that?

Streaming Request headers:
Expand|Select|Wrap|Line Numbers
  1. GET /WAAF HTTP/1.1
  2. Accept: */*
  3. User-Agent: NSPlayer/11.0.5721.5145
  4. Host: wmc2.liquidcompass.cc
  5. X-Accept-Authentication: Negotiate, NTLM, Digest, Basic
  6. Pragma: version11-enabled=1
  7. Pragma: no-cache,rate=1.000,stream-time=0,stream-offset=4294967295:4294967295,packet-num=4294967295,max-duration=0
  8. Pragma: xPlayStrm=1
  9. Pragma: client-id=648480293
  10. Pragma: LinkBW=3480189, AccelBW=2147483647, AccelDuration=18000
  11. Supported: com.microsoft.wm.srvppair, com.microsoft.wm.sswitch, com.microsoft.wm.predstrm, com.microsoft.wm.startupprofile
  12. Pragma: playlist-gen-id=18965
  13. Pragma: xClientGUID={3300AD50-2C39-46c0-AE0A-FF8E16F71442}
  14. Pragma: stream-switch-count=2
  15. Pragma: stream-switch-entry=ffff:1:0 ffff:2:0 
  16. Accept-Language: en-us, *;q=0.1
And the reply headers:
Expand|Select|Wrap|Line Numbers
  1. HTTP/1.1 200 OK
  2. Content-Type: application/x-mms-framed
  3. Server: Cougar/
  4. Date: Thu, 21 Aug 2008 18:18:30 GMT
  5. Pragma: no-cache, client-id=648480293, features="broadcast", timeout=60000, AccelBW=960000, AccelDuration=18946, Speed=1.000
  6. Cache-Control: no-cache, x-wms-stream-type="broadcast"
  7. Last-Modified: Sat, 30 Dec 1899 00:00:00 GMT
  8. Transfer-Encoding: chunked
  9. Supported: com.microsoft.wm.srvppair, com.microsoft.wm.sswitch, com.microsoft.wm.predstrm, com.microsoft.wm.fastcache, com.microsoft.wm.startupprofile
Aug 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.