Hello there,
In this tutorial I’ll be discussing about creating a Web Server in Visual Basic 6. To create a Web Server we need to open the port 80 on the local IP and wait for requests. Every request tells the Server what the user(respectively the users web browser) needs to open. The Server takes the file name from the request, opens the file, and sends it to the user. To do all this stuff in Visual Basic 6 we will need to use the Microsoft Winsock Control.
How we can do this?
First we add a winsock control in our form and leave its name as is, Winsock1. Make Winsock1 start listening on port 80 every time the program runs by adding this code to on the Form_Load() sub:
Now we are listening at port 80 and clients may send connection request so we have to accept them first. Add the following code to the Winsock1_ConnectionRequest() sub:Code:Winsock1.LocalPort = 80 Winsock1.Listen
This means when a connection request comes Winsock1 checks itself if any client is already connected and if so it disconnects it then accepts the connection request. Now that we are listening on port 80 and we are ready to accept connection requests that will come we have to manage the data requests that will come. This will be done by adding the following code to the Winsock1_DataArrival() sub:On Error Resume Next
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
What have we done here? We have set a variable named DT and later we filled this variable with the data that came from the client and at least we call the GetFile function and tell it to use the data stored in DT. What is on the GetFile function? The GetFile function contains this code:Code:Dim DT As String Winsock1.GetData DT Call GetFile(DT)
As we see on the GetFile function, first are created 2 variables unpFile(wich I used in short of unprepaired File) and unpFileS(or unprepared File the Second). As we see the second one will be an array, we will see later why. Later we see that unpFile = Right(Request, Len(Request) - 5). Why? Because if we will make a Text Box ant make the program show in it the clients request we will see that this is added before the filename in the request “GET /”. That tells the server that the GET method is being used. We don’t need that because our server will be simple and wont use any other methods like PUT. But we still need modifications to the request to get the file name. At the end of the file name in the request is added “ HTTP” so we use the split function to get only the file name. This is done in the next line. At the end, we call the OpenFile sub. The OpenFile sub will open the file and sent its contents back to the client. Here’s whats in the OpenFile sub:Code:Public Sub GetFile(Request As String) Dim unpFile As String Dim unpFileS() As String unpFile = Right(Request, Len(Request) - 5) unpFileS = Split(unpFile, " HTTP") Call OpenFile(unpFileS(0)) End Sub
As we see here we have 2 variables too. TempsStr will be used to hold temporary data from the file and the second, OpenedFile to hold the whole file. Next we see some code which is used to open the file. First we see a path(App.Pth & “\Root\” & File). As we see before the File variable(in which the file name that the user needs) is the App.Path & “\Root” which means in the same folder that the program is running there is a folder named Root in which the HTML files are stored. Before this function we see something like “On Error GoTo Err:”. This means that if the file cant be opened it doesn't exist so send “Error 404” to the user.Code:Public Sub OpenFile(File As String) Dim TempStr As String Dim OpenedFile As String On Error GoTo Err: Open App.Path & "\Root\" & File For Input As #1 Do Until EOF(1) Input #1, TempStr OpenedFile = OpenedFile & TempStr Loop Close #1 Winsock1.SendData OpenedFile Exit Sub Err: Dim ErrString As String ErrString = "Error 404" & vbNewLine & "File not found" Winsock1.SendData ErrString End Sub
After opening the file we send it back to the user by the “Winsock1.SendData OpenedFile” command.
After sending the file to the user we have to close the connection to wait for other connection request so we write the following code to the Winsock1_SendComplete() sub:
That’s all, but don’t expect this to be a Server like Apache, this server has these disadvantages:Code:Winsock1.Close Winsock1.LocalPort = 80 Winsock1.Listen
- Cant connect more than 1 users at the same time
- If you try downloading non-Plain Text files they will be corrupted
- If you try downloading large files the servers RAM will be overloaded
- Find others yourself lol.
In the attached project there are added features like opening index.htm or index.html if no file specified. Study them yourself.
I wish you all the best,
Dren


LinkBack URL
About LinkBacks






Reply With Quote


You may use this type of Web Server if you want to create a network software where this web server will be the best way for the client to comunicate with the server (example in an Internet Caffee software).




Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum