Question Server Hosting Problems...

DalexL

Active member
Joined
Jul 3, 2010
Messages
34
Programming Experience
3-5
The entire project that I'm working with is located here (click the download link on the RIGHT side of the page, the files name is "Server.zip"), I'm sorry it's not onsite. I was unable to get the vbdotnetforums uploader to upload without errors. I really am.

This is a home made server thats hosts a server on port 80. To connect to the server just put "http://Localhost/" in you browsers address bar in case you didn't already know.

I'm currently able to send data from the "Site" folder to the browser, however, I'm currently having a problem sending non-text based files (Videos, Images, and other things of the sort). In fact, it errors attempting to send PNGs and Icons. It can send music and JPEGs but sends the text instead of the actual file. I was wondering if there was some kind of way to fix that. Explanations or tips would be appreciated.

Here is the function I think I've narrowed the problem down to:

VB.NET:
    Private Sub SendPage(ByVal response As HttpListenerResponse, ByVal Location As String, Optional ByVal directory As Boolean = False)

        With response
            .ContentType = "text/html"
            .ContentEncoding = Encoding.UTF8
            If directory Then
                .ContentLength64 = Location.Length
            Else
                .ContentLength64 = My.Computer.FileSystem.GetFileInfo(Location).Length
            End If

            Try
                Using writer As New StreamWriter(.OutputStream)
                    With writer
                        If directory Then
                            .Write(Location)
                        Else
                            .Write(My.Computer.FileSystem.ReadAllText(Location.Replace("%20", " ")))
                            .Flush()
                        End If
                    End With
                End Using
            Catch ex As Exception
                WriteMessage("Reply failed!")
            Finally
                .Close()
            End Try
        End With

    End Sub

The error I commonly receive is:

System.InvalidOperationEception: Cannot close stream until all bytes are written.
at System.Ne.HttpResponseStream.Dispose(Bolean disposing)
at System.IO.Stream.Close()
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TetWriter.Dispose()
at Server.ServerClass.SendPage(HttpListenerResponse response, String Location, Boolean directory) in C:\Users\*******\Documents\Visual Studio 2008\Projects\Server\Server\ServerClass.vb:line 143

Of course, you can always see this for yourself in the actual file. I'm sorry I've uploaded it to another site. It's in a ZIP, feel fine to scan it if it makes you feel better.

Thanks!
 
m sorry it's not onsite. I was unable to get the vbdotnetforums uploader to upload without errors.
The file is 21MB, of which 99% is a mp4 file. If you remove that and the generated binaries in Bin+Obj folders you'll end up with the complete source at a few KBs.
however, I'm currently having a problem sending non-text based files
That's because you are using ReadAllText and StreamWriter, both which read/write strings only. Try .ReadAllBytes and .OutputStream.Write (*). Also, you need to expand on the .ContentType, not all reponses you send is "text/html".

Note that you can read/write the content of text files as binary also, reading as string content is just a redirection, the contents of those files is a series of bytes just like any other file.

(*) for larger files it is inefficient to read everything into memory then write it to response stream. Your profile states .Net 4.0 so you can open a FileStream and use the CopyTo method.
 
Back
Top