mcManiac
Nov 8th, 2009, 11:40 AM
I'm sorry I posted this in the wrong section before...
So to begin with i'm pretty new to programming and i've been working on a simple application things on my desktop through a tcpserver that responds to Http requests.
The code itself is "working", the page comes up with the an image and everything, it's just that I often get a "This webpage is not available" on the browser and I constantly have to refresh until the page comes up.
The only clue I have to what might be happening comes from Google Chrome browser where the error message says .. " Error 101 (net::ERR_CONNECTION_RESET): Unknown error. "
Here's the code...
Public Sub HttpPage()
Try
Dim hostName As String = Dns.GetHostName()
Dim serverIP As IPAddress = Dns.Resolve(hostName).AddressList(0)
Dim Port As String = txtPort.Text
Dim tcpListener As New TcpListener(serverIP, Int32.Parse(Port))
tcpListener.Start()
lblStatus.Text = "Web server started at: " & serverIP.ToString() & ":" & Port
Me.tcpListener = tcpListener
Dim serverThread As New Thread(New ParameterizedThreadStart(AddressOf ProcessThread))
serverThread.Start()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
End Try
End Sub
Public Sub ProcessThread()
Dim clientSocket As System.Net.Sockets.Socket
While (True)
Try
clientSocket = tcpListener.AcceptSocket()
' Socket Information
Dim clientInfo As IPEndPoint = CType(clientSocket.RemoteEndPoint, IPEndPoint)
Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
' Set Thread for each Web Browser Connection
Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf ProcessRequest))
clientThread.Start(clientSocket)
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientSocket.Connected Then
clientSocket.Close()
End If
End Try
End While
End Sub
Public Sub ProcessRequest(ByVal clientSocket As System.Net.Sockets.Socket)
Dim recvBytes(1024) As Byte
Dim htmlReq As String = Nothing
Dim bytes As Int32
Try
' Receive HTTP Request from Web Browser
bytes = clientSocket.Receive(recvBytes, 0, clientSocket.Available, SocketFlags.None)
htmlReq = Encoding.ASCII.GetString(recvBytes, 0, bytes)
Label5.Text = "HTTP Request: " & htmlReq
' Set WWW Root Path
Dim rootPath As String = Directory.GetCurrentDirectory() & "\Monitor\" & GetName() & "\"
' Set default page
Dim defaultPage As String = GetName() & ".html"
Dim strArray() As String
Dim strRequest As String
strArray = htmlReq.Trim.Split(" ")
' Determine the HTTP method (GET only)
If strArray(0).Trim().ToUpper.Equals("GET") Then
strRequest = strArray(1).Trim
If (strRequest.StartsWith("/")) Then
strRequest = strRequest.Substring(1)
End If
If (strRequest.EndsWith("/") Or strRequest.Equals("")) Then
strRequest = strRequest & defaultPage
End If
strRequest = rootPath & strRequest
sendHTMLResponse(strRequest, clientSocket)
Else ' Not HTTP GET method
strRequest = rootPath & "Error\" & "400.html"
sendHTMLResponse(strRequest, clientSocket)
End If
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientSocket.Connected Then
clientSocket.Close()
End If
End Try
End Sub
' Send HTTP Response
Private Sub sendHTMLResponse(ByVal httpRequest As String, ByVal clientsocket As System.Net.Sockets.Socket)
Try
Dim info As New IO.FileInfo(httpRequest)
'Set HTML Header
Dim htmlHeader As String = _
"HTTP/1.0 200 OK" & ControlChars.CrLf & _
"Server: WebServer 1.0" & ControlChars.CrLf & _
"Content-Length: " & info.Length & _
"Content-Type: " & getContentType(httpRequest) & _
ControlChars.CrLf & ControlChars.CrLf
' The content Length of HTML Header
Dim headerByte() As Byte = Encoding.ASCII.GetBytes(htmlHeader)
Console.WriteLine("HTML Header: " & ControlChars.CrLf & htmlHeader)
' Send HTML Header back to Web Browser
clientsocket.Send(headerByte, 0, headerByte.Length, SocketFlags.None)
' Send HTML Content back to Web Browser
Dim stream As IO.FileStream = info.OpenRead
Dim buffer(1024) As Byte
Dim read As Integer = -1
Do Until read = 0
read = stream.Read(buffer, 0, buffer.Length)
clientsocket.Send(buffer, 0, read, SocketFlags.None)
Loop
stream.Close()
' Close HTTP Socket connection
clientsocket.Shutdown(SocketShutdown.Both)
clientsocket.Close()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientsocket.Connected Then
clientsocket.Close()
End If
End Try
End Sub
' Get Content Type
Private Function getContentType(ByVal httpRequest As String) As String
If (httpRequest.EndsWith("html")) Then
Return "text/html"
ElseIf (httpRequest.EndsWith("htm")) Then
Return "text/html"
ElseIf (httpRequest.EndsWith("txt")) Then
Return "text/plain"
ElseIf (httpRequest.EndsWith("gif")) Then
Return "image/gif"
ElseIf (httpRequest.EndsWith("jpg")) Then
Return "image/jpeg"
ElseIf (httpRequest.EndsWith("jpeg")) Then
Return "image/jpeg"
ElseIf (httpRequest.EndsWith("pdf")) Then
Return "application/pdf"
ElseIf (httpRequest.EndsWith("pdf")) Then
Return "application/pdf"
ElseIf (httpRequest.EndsWith("doc")) Then
Return "application/msword"
ElseIf (httpRequest.EndsWith("xls")) Then
Return "application/vnd.ms-excel"
ElseIf (httpRequest.EndsWith("ppt")) Then
Return "application/vnd.ms-powerpoint"
Else
Return "text/plain"
End If
End Function
Any idea what might be causing the connection to reset?
So to begin with i'm pretty new to programming and i've been working on a simple application things on my desktop through a tcpserver that responds to Http requests.
The code itself is "working", the page comes up with the an image and everything, it's just that I often get a "This webpage is not available" on the browser and I constantly have to refresh until the page comes up.
The only clue I have to what might be happening comes from Google Chrome browser where the error message says .. " Error 101 (net::ERR_CONNECTION_RESET): Unknown error. "
Here's the code...
Public Sub HttpPage()
Try
Dim hostName As String = Dns.GetHostName()
Dim serverIP As IPAddress = Dns.Resolve(hostName).AddressList(0)
Dim Port As String = txtPort.Text
Dim tcpListener As New TcpListener(serverIP, Int32.Parse(Port))
tcpListener.Start()
lblStatus.Text = "Web server started at: " & serverIP.ToString() & ":" & Port
Me.tcpListener = tcpListener
Dim serverThread As New Thread(New ParameterizedThreadStart(AddressOf ProcessThread))
serverThread.Start()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
End Try
End Sub
Public Sub ProcessThread()
Dim clientSocket As System.Net.Sockets.Socket
While (True)
Try
clientSocket = tcpListener.AcceptSocket()
' Socket Information
Dim clientInfo As IPEndPoint = CType(clientSocket.RemoteEndPoint, IPEndPoint)
Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
' Set Thread for each Web Browser Connection
Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf ProcessRequest))
clientThread.Start(clientSocket)
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientSocket.Connected Then
clientSocket.Close()
End If
End Try
End While
End Sub
Public Sub ProcessRequest(ByVal clientSocket As System.Net.Sockets.Socket)
Dim recvBytes(1024) As Byte
Dim htmlReq As String = Nothing
Dim bytes As Int32
Try
' Receive HTTP Request from Web Browser
bytes = clientSocket.Receive(recvBytes, 0, clientSocket.Available, SocketFlags.None)
htmlReq = Encoding.ASCII.GetString(recvBytes, 0, bytes)
Label5.Text = "HTTP Request: " & htmlReq
' Set WWW Root Path
Dim rootPath As String = Directory.GetCurrentDirectory() & "\Monitor\" & GetName() & "\"
' Set default page
Dim defaultPage As String = GetName() & ".html"
Dim strArray() As String
Dim strRequest As String
strArray = htmlReq.Trim.Split(" ")
' Determine the HTTP method (GET only)
If strArray(0).Trim().ToUpper.Equals("GET") Then
strRequest = strArray(1).Trim
If (strRequest.StartsWith("/")) Then
strRequest = strRequest.Substring(1)
End If
If (strRequest.EndsWith("/") Or strRequest.Equals("")) Then
strRequest = strRequest & defaultPage
End If
strRequest = rootPath & strRequest
sendHTMLResponse(strRequest, clientSocket)
Else ' Not HTTP GET method
strRequest = rootPath & "Error\" & "400.html"
sendHTMLResponse(strRequest, clientSocket)
End If
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientSocket.Connected Then
clientSocket.Close()
End If
End Try
End Sub
' Send HTTP Response
Private Sub sendHTMLResponse(ByVal httpRequest As String, ByVal clientsocket As System.Net.Sockets.Socket)
Try
Dim info As New IO.FileInfo(httpRequest)
'Set HTML Header
Dim htmlHeader As String = _
"HTTP/1.0 200 OK" & ControlChars.CrLf & _
"Server: WebServer 1.0" & ControlChars.CrLf & _
"Content-Length: " & info.Length & _
"Content-Type: " & getContentType(httpRequest) & _
ControlChars.CrLf & ControlChars.CrLf
' The content Length of HTML Header
Dim headerByte() As Byte = Encoding.ASCII.GetBytes(htmlHeader)
Console.WriteLine("HTML Header: " & ControlChars.CrLf & htmlHeader)
' Send HTML Header back to Web Browser
clientsocket.Send(headerByte, 0, headerByte.Length, SocketFlags.None)
' Send HTML Content back to Web Browser
Dim stream As IO.FileStream = info.OpenRead
Dim buffer(1024) As Byte
Dim read As Integer = -1
Do Until read = 0
read = stream.Read(buffer, 0, buffer.Length)
clientsocket.Send(buffer, 0, read, SocketFlags.None)
Loop
stream.Close()
' Close HTTP Socket connection
clientsocket.Shutdown(SocketShutdown.Both)
clientsocket.Close()
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString())
If clientsocket.Connected Then
clientsocket.Close()
End If
End Try
End Sub
' Get Content Type
Private Function getContentType(ByVal httpRequest As String) As String
If (httpRequest.EndsWith("html")) Then
Return "text/html"
ElseIf (httpRequest.EndsWith("htm")) Then
Return "text/html"
ElseIf (httpRequest.EndsWith("txt")) Then
Return "text/plain"
ElseIf (httpRequest.EndsWith("gif")) Then
Return "image/gif"
ElseIf (httpRequest.EndsWith("jpg")) Then
Return "image/jpeg"
ElseIf (httpRequest.EndsWith("jpeg")) Then
Return "image/jpeg"
ElseIf (httpRequest.EndsWith("pdf")) Then
Return "application/pdf"
ElseIf (httpRequest.EndsWith("pdf")) Then
Return "application/pdf"
ElseIf (httpRequest.EndsWith("doc")) Then
Return "application/msword"
ElseIf (httpRequest.EndsWith("xls")) Then
Return "application/vnd.ms-excel"
ElseIf (httpRequest.EndsWith("ppt")) Then
Return "application/vnd.ms-powerpoint"
Else
Return "text/plain"
End If
End Function
Any idea what might be causing the connection to reset?