Jump to content

How to create a Web Server in Visual Basic 6

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
31 replies to this topic

#1
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
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:

Winsock1.LocalPort = 80
Winsock1.Listen

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:

Quote

On Error Resume Next
If Winsock1.State <> sckClosed Then Winsock1.Close

Winsock1.Accept requestID

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:

Dim DT As String

Winsock1.GetData DT
Call GetFile(DT)

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:

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 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:

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

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.

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:

Winsock1.Close
Winsock1.LocalPort = 80
Winsock1.Listen

That’s all, but don’t expect this to be a Server like Apache, this server has these disadvantages:


  • 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

Attached Files


Edited by Dren, 29 July 2008 - 04:51 PM.
Edited three times, Im so sorry, now seems cool :D


#2
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
awesome thread!!
Checkout my new forum! http://adminreference.com/

#3
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
Thank You mate, why not +rep me up :p

#4
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
I would.. but I already gave you in another thread so it won't let me.

#5
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
THANK YOU TcM :D 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).

As I said this server wont be able to transfer EXE files. To do this change the open file line to Open File For Binary Input. You can also create a loop to show files in the server if there is no index file.

Thanks again,
Dren

#6
scsefrmr

scsefrmr

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Excellent thread :)

I'm unsure about this, but I think you could also use the "Microsoft Internet Transfer Control 6.0" component along with winsock to get files transferring without getting corrupted. I'm sure someone will confirm this if its correct.

Rep++

#7
thecobra

thecobra

    Newbie

  • Members
  • Pip
  • 2 posts
nice thread and good tutoring. this convince me to register here :)

#8
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Good.. good.. now I hope you will contribute some posts to the community :D

#9
Kouhai

Kouhai

    Newbie

  • Members
  • Pip
  • 1 posts
I really like the idea of doing this. Nice tutorial too.

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Once again Dren, you have rewarded us with a VB tutorial! You can have a good dollop of +rep from me! :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
Thank you, thank you everyone. I have been off these days because I was in vacation in Albania for 10 days. From there I came back in CC only once (because I was accessing the Internet from a HotSpot wich had low signal were I was staying) to view a post in the C++ forums. I really feel great when CodeCall users like my treads. I will keep thinking for new tutorial ideas and enriching CodeCall with new tutorials.

Thanks again,
Dren

#12
libran30

libran30

    Newbie

  • Members
  • Pip
  • 3 posts
best tutorial available on net...thnx dren u solved my problem...