Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to create a Web Server in Visual Basic 6


  • Please log in to reply
31 replies to this topic

#1 Dren

Dren

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 29 July 2008 - 04:47 PM

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:

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

  • 4

#2 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 29 July 2008 - 06:49 PM

awesome thread!!
  • 0

#3 Dren

Dren

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 30 July 2008 - 12:15 AM

Thank You mate, why not +rep me up :P
  • -1

#4 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 31 July 2008 - 09:56 AM

I would.. but I already gave you in another thread so it won't let me.
  • 0

#5 Dren

Dren

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 01 August 2008 - 09:13 AM

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
  • -1

#6 scsefrmr

scsefrmr

    CC Regular

  • Validating
  • PipPipPip
  • 38 posts
  • Programming Language:C#, (Visual) Basic
  • Learning:Python

Posted 01 August 2008 - 11:05 AM

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++
  • 0

#7 thecobra

thecobra

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 20 August 2008 - 08:57 AM

nice thread and good tutoring. this convince me to register here :)
  • 0

#8 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 21 August 2008 - 02:26 AM

Good.. good.. now I hope you will contribute some posts to the community :D
  • 0

#9 Kouhai

Kouhai

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 27 August 2008 - 05:32 AM

I really like the idea of doing this. Nice tutorial too.
  • 0

#10 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 August 2008 - 05:53 AM

Once again Dren, you have rewarded us with a VB tutorial! You can have a good dollop of +rep from me! :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 Dren

Dren

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 27 August 2008 - 12:34 PM

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
  • 0

#12 libran30

libran30

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 September 2008 - 03:12 AM

best tutorial available on net...thnx dren u solved my problem...
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download