Results 1 to 3 of 3

Thread: [RESOLVED] Returning a web page

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Resolved [RESOLVED] Returning a web page

    Hi,

    A person called Atheist helped me return a static page upon request. However, I have two problems:

    1) It works 3/4 times, I figure this is because a connection is not finished. However, it creates a new thread each time.
    2) I now see a blank page but it has the <html> doctype specified.

    Code:
    Option Strict On
    Imports System.Net.Sockets
    Imports System.Net
    
    
    Public Class webFilter
        Private listenThread As System.Threading.Thread
        Private listenSocket As System.Net.Sockets.Socket
        Private Const PORT_NUMBER As Integer = 80
        Private Const BACKLOG As Integer = 10
    
        Private Sub webFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            listenSocket = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
            listenSocket.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), PORT_NUMBER)) ' Bind the socket to 127.0.0.1:PORT_NUMBER
            listenSocket.Listen(BACKLOG) ' Start listening on the port
            listenThread = New System.Threading.Thread(AddressOf doListen) ' Create the thread that will be used for listening and responding
            listenThread.IsBackground = True ' Set IsBackground to true to make the thread terminate when the main thread terminates.
            listenThread.Start() ' Start the the thread
        End Sub
        Private Sub doListen()
            Dim client As System.Net.Sockets.Socket
            Dim clientThread As System.Threading.Thread
            Do
                client = listenSocket.Accept() ' When this method returns, "client" will hold the connection to the remote host.
                ' Start a new thread to respond to the client, this will let us
                ' continue listening for new connections while responding to other hosts.
                clientThread = New System.Threading.Thread(AddressOf RespondToHost)
                clientThread.IsBackground = True
                clientThread.Start(client)
            Loop
        End Sub
    
        Private Sub RespondToHost(ByVal obj As Object)
            Dim resp() As Byte
            Dim client As System.Net.Sockets.Socket = DirectCast(obj, System.Net.Sockets.Socket)
            resp = BuildResponse("url-deleted-for-security")
            client.Send(resp, resp.Length, Net.Sockets.SocketFlags.None)
            client.Close()
        End Sub
    
        Private Function BuildResponse(ByVal file As String) As Byte()
            Dim response As New System.Text.StringBuilder
            Dim sReader As System.IO.StreamReader
            Dim fileContents As String
            If System.IO.File.Exists(file) Then
                sReader = New System.IO.StreamReader(file)
                fileContents = sReader.ReadToEnd()
                response.AppendLine("HTTP/1.1 200 OK")
                response.AppendLine("Content-type: text/html")
                response.AppendLine(fileContents.Length.ToString())
                response.Append(Environment.NewLine)
                response.Append(fileContents)
                sReader.Close()
            Else
                response.AppendLine("HTTP/1.1 404")
                response.Append(Environment.NewLine)
            End If
            Return System.Text.Encoding.UTF8.GetBytes(response.ToString())
        End Function
    
    
        Private Sub webFilter_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Me.Visible = False
        End Sub
    End Class
    A question is, are the threads terminated after they Client.Close. I do not want say 50 threads stacking up.

    Thanks

    .NET 3.5 is returning a vs2k3 warning:

    Warning 1 The element 'PropertyGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'PublishUrlHistory' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Property' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. MyProjDir 3 6 Miscellaneous Files
    Last edited by samtheman; May 2nd, 2008 at 01:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width