+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast
Results 1 to 10 of 31

Thread: How to create a Web Server in Visual Basic 6

  1. #1
    Guru Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren's Avatar
    Join Date
    Nov 2006
    Location
    Kosovo
    Age
    19
    Posts
    448

    How to create a Web Server in Visual Basic 6

    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:

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

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

    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:

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

    Code:
    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
    Last edited by Dren; 07-29-2008 at 07:51 PM. Reason: Edited three times, Im so sorry, now seems cool :D

  2. #2
    Speaks fluent binary phpforfun has a spectacular aura about phpforfun has a spectacular aura about phpforfun's Avatar
    Join Date
    Feb 2008
    Posts
    1,204
    Blog Entries
    17

    Re: How to create a Web Server in Visual Basic 6

    awesome thread!!
    AmpHosted - Reliable Hosting [Shared, Reseller, Exchange, VPS]

  3. #3
    Guru Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren's Avatar
    Join Date
    Nov 2006
    Location
    Kosovo
    Age
    19
    Posts
    448

    Re: How to create a Web Server in Visual Basic 6

    Thank You mate, why not +rep me up

  4. #4
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: How to create a Web Server in Visual Basic 6

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

  5. #5
    Guru Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren is a name known to all Dren's Avatar
    Join Date
    Nov 2006
    Location
    Kosovo
    Age
    19
    Posts
    448

    Re: How to create a Web Server in Visual Basic 6

    THANK YOU TcM 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. #6
    Learning Programmer scsefrmr is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    45

    Re: How to create a Web Server in Visual Basic 6

    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. #7
    Newbie thecobra is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    2

    Re: How to create a Web Server in Visual Basic 6

    nice thread and good tutoring. this convince me to register here

  8. #8
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    Re: How to create a Web Server in Visual Basic 6

    Good.. good.. now I hope you will contribute some posts to the community

  9. #9
    Newbie Kouhai is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    1

    Re: How to create a Web Server in Visual Basic 6

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

  10. #10
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    Re: How to create a Web Server in Visual Basic 6

    Once again Dren, you have rewarded us with a VB tutorial! You can have a good dollop of +rep from me!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 4
1 2 3 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Visual Studio 2005 and Windows Vista
    By Jordan in forum General Programming
    Replies: 14
    Last Post: 01-10-2009, 02:39 PM
  2. Learn how to create your own web server
    By Jaan in forum Tutorials
    Replies: 5
    Last Post: 05-18-2008, 06:43 PM
  3. Creating a Class in Visual Studios.Net (Visual Basic Section)
    By dream in forum Visual Basic Programming
    Replies: 11
    Last Post: 05-12-2008, 02:47 PM
  4. Executing SQL Server 2k DTS Package via Visual Basic
    By Joemama in forum Visual Basic Programming
    Replies: 0
    Last Post: 04-09-2007, 06:21 PM
  5. Graphical programming add-in for Visual Basic 6.0
    By xXHalfSliceXx in forum Visual Basic Programming
    Replies: 10
    Last Post: 01-03-2007, 07:14 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts