jorge Posted May 8, 2004 Posted May 8, 2004 (edited) webserver(c# code, but need vb.net) Hey all, i am trying to include a small webserver in one of my application, so far i have found a very nice example, but it is in c# i've been trying to convert it, but i always hit the same error(to many to sum up) i'm talking about: http://www.codeproject.com/Purgatory/webservergui.asp if any one has converted it or know a vb.net very. can you post a link? Thanx in advance Edited May 8, 2004 by jorge Quote Jorge - http://www.blackdot.be/?page=apache.htm
jorge Posted May 11, 2004 Author Posted May 11, 2004 no one knows?? Quote Jorge - http://www.blackdot.be/?page=apache.htm
Moderators Robby Posted May 11, 2004 Moderators Posted May 11, 2004 If you're able to convert the form code then simply compile the WebServer code into an assembly and use it as C#. Can't you? Quote Visit...Bassic Software
jorge Posted May 11, 2004 Author Posted May 11, 2004 (edited) If you're able to convert the form code then simply compile the WebServer code into an assembly and use it as C#. Can't you?That would work, but i want to modify the server code a bit, so then that won't work anymore :( Here is what i have converted: Imports System Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Text Imports System.Threading Namespace WebServer Class WebServer Private sMyWebServerRoot As String Private mySocket As Socket Public Sub New(ByVal s As Socket, ByVal Location As String) mySocket = s sMyWebServerRoot = Location + "\" End Sub Private Function GetMimeType(ByVal fileName As String) As String If fileName.EndsWith(".htm") OrElse fileName.EndsWith(".html") Then Return "text/html" Else If fileName.EndsWith(".jpg") OrElse fileName.EndsWith(".jpeg") Then Return "image/jpeg" Else If fileName.EndsWith(".gif") Then Return "image/gif" Else If fileName.EndsWith(".txt") Then Return "text/plain" Else Return "application/octet-stream" End If End If End If End If End Function Private Sub SendHeader(ByVal sHttpVersion As String, ByVal sMIMEHeader As String, ByVal iTotBytes As Integer, ByVal sStatusCode As String) Dim sBuffer As String = "" If sMIMEHeader.Length = 0 Then sMIMEHeader = "text/html" End If sBuffer = sBuffer + sHttpVersion + sStatusCode + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" sBuffer = sBuffer + "Server: MyWebServer-b" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" sBuffer = sBuffer + "Accept-Ranges: bytes" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" sBuffer = sBuffer + "Content-Length: " + iTotBytes + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" Dim bSendData As Byte = Encoding.ASCII.GetBytes(sBuffer) SendToBrowser(bSendData) End Sub Private Sub SendToBrowser(ByVal sData As String) SendToBrowser(Encoding.ASCII.GetBytes(sData)) End Sub Private Sub SendToBrowser(ByVal bSendData() As Byte) Dim numBytes As Integer = 0 Try If mySocket.Connected Then If (numBytes = mySocket.Send(bSendData, bSendData.Length, 0)) = -1 Then Form1.WriteToConsole("Socket Error cannot Send Packet" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") Else Form1.WriteToConsole("No. of bytes send " + numBytes + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") End If Else Form1.WriteToConsole("Connection Dropped....") End If Catch e As Exception Form1.WriteToConsole("Error Occurred : " + e + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") End Try End Sub Public Sub HandleConnection() Dim iStartPos As Integer = 0 Dim sRequest As String Dim sDirName As String Dim sRequestedFile As String Dim sErrorMessage As String Dim sLocalDir As String Dim sPhysicalFilePath As String = "" Dim bReceive(1024) As Byte Dim i As Integer = mySocket.Receive(bReceive, bReceive.Length, 0) Dim sBuffer As String = Encoding.ASCII.GetString(bReceive) If Not (sBuffer.Substring(0, 3) = "GET") Then Form1.WriteToConsole("Only Get Method is supported.." + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") mySocket.Close() Return End If iStartPos = sBuffer.IndexOf("HTTP", 1) Dim sHttpVersion As String = sBuffer.Substring(iStartPos, 8) sRequest = sBuffer.Substring(0, iStartPos - 1) Form1.WriteToConsole("File Requested : " + sRequest + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") sRequest.Replace("\", "/") If (sRequest.IndexOf(".") < 1) AndAlso (Not sRequest.EndsWith("/")) Then sRequest = sRequest + "/" End If iStartPos = sRequest.LastIndexOf("/") + 1 sRequestedFile = sRequest.Substring(iStartPos) sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/") - 3) sLocalDir = sMyWebServerRoot If sRequestedFile.Length = 0 Then If sRequestedFile = "" Then sRequestedFile = "index.htm" End If End If Dim sMimeType As String = GetMimeType(sRequestedFile) sPhysicalFilePath = sLocalDir + sDirName + sRequestedFile If File.Exists(sPhysicalFilePath) = False Then sErrorMessage = "<H2>404 Error! File Does Not Exist...</H2>" SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found") SendToBrowser(sErrorMessage) Form1.WriteToConsole("File Not found" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "") Else Dim fs As FileStream = New FileStream (sPhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read) Dim reader As BinaryReader = New BinaryReader (fs) Dim bytes(fs.Length) As Byte bytes = reader.ReadBytes(CType(fs.Length, Integer)) Dim totbytes As Integer = CType(fs.Length, Integer) reader.Close() fs.Close() SendHeader(sHttpVersion, sMimeType, totbytes, " 200 OK") SendToBrowser(bytes) End If mySocket.Close() End Sub End Class End Namespace but that doesnt work :confused: Edited May 11, 2004 by Robby Quote Jorge - http://www.blackdot.be/?page=apache.htm
Moderators Robby Posted May 11, 2004 Moderators Posted May 11, 2004 Does it compile? Quote Visit...Bassic Software
jorge Posted May 11, 2004 Author Posted May 11, 2004 No, give error on teh from1 thing's, but i suspected that, and i removed them but it still gives a error on: sBuffer = sBuffer + "Content-Length: " + iTotBytes + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" + Microsoft.VisualBasic.Chr(13) + "" + Microsoft.VisualBasic.Chr(10) + "" Dim bSendData As Byte = Encoding.ASCII.GetBytes(sBuffer) Quote Jorge - http://www.blackdot.be/?page=apache.htm
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.