+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

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

  1. #1
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

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

    awesome thread!!
    Checkout my new forum! http://adminreference.com/

  4. #3
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

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

    Thank You mate, why not +rep me up

  5. #4
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    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.

  6. #5
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    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

  7. #6
    scsefrmr is offline Learning Programmer
    Join Date
    May 2008
    Posts
    45
    Rep Power
    0

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

  8. #7
    thecobra is offline Newbie
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

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

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

  9. #8
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

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

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

  10. #9
    Kouhai is offline Newbie
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

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

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

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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 123 ... LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Visual Basic Assighment Help. (Translating pseudocode to visual basic)
    By Adolf B in forum Visual Basic Programming
    Replies: 1
    Last Post: 03-18-2011, 09:16 PM
  2. Create Visual basic code for a joke and its punchline
    By tonyfingures in forum Visual Basic Programming
    Replies: 2
    Last Post: 09-10-2010, 12:39 PM
  3. can we visual basic to combine sql server at mainframes
    By kvsr_ven in forum Visual Basic Programming
    Replies: 1
    Last Post: 02-11-2010, 03:10 AM
  4. Visual Basic 2008 vs. Visual Basic 2005 - Compatibility
    By cande_300 in forum Visual Basic Programming
    Replies: 1
    Last Post: 01-23-2010, 09:00 AM
  5. Executing SQL Server 2k DTS Package via Visual Basic
    By Joemama in forum Visual Basic Programming
    Replies: 0
    Last Post: 04-09-2007, 04:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

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