A very basic web server Test

Level:
Level1

Writing network related programs is fun and exciting. At times it almost seems like magic. This source code sample shows how you can create a very basic web server that when your browser connects to it the specified text will show up in the browser. This uses the Microsoft Winsock control.

This source code sample demonstrates how you can create a very, very, basic web server. It is not intended for you to actually use but instead to just wet your appetite with a simple networking example. Before someone leaves a nasty comment read this: I am in no way calling this a true web server - just a simple fun example.

What you will end up with after using this very simple code snippet is a little server running in VB that when a web browser hits it will return out whatever text you have set up. I know when I first started playing with networking development it seemed kind of like magic. You can very quickly begin to understand how web servers, a virtual private server, chat programs, email programs, etc. work.

To use this code example do the following: Create a new VB program. Next place a command button on your form named command1 and set the caption to Start Server, you will also need to place the Winsock control on the form. Set its name to Winser. If you don't know how to put a winsock control on your form check out this Client server tutorial to see how. Lastly place the code below into the source code area for your form.

  1. Private Sub Command1_Click()
  2.     WinSer.LocalPort = 80
  3.     WinSer.Listen
  4.     Command1.Enabled = False
  5. End Sub
  6.  
  7. Private Sub WinSer_ConnectionRequest(ByVal requestID As Long)
  8.     WinSer.Close
  9.     WinSer.Accept requestID
  10. End Sub
  11.  
  12. Private Sub WinSer_DataArrival(ByVal bytesTotal As Long)
  13. Dim DataSend As String
  14.     DataSend = "Wow My first Webserver Test"
  15.     DataSend = DataSend & vbNewLine & Now
  16.     WinSer.SendData DataSend
  17. End Sub
  18.  
  19. Private Sub WinSer_SendComplete()
  20.     WinSer.Close
  21. End Sub

Once you have the code in place press F5 to run your program. Then pull open a web browser and navigate to http://localhost - This will connect to your VB web server and you should see "Wow my first webserver test" show up in your browser.

Obviously this just barely scratches the surface as to what we can do with the winsock control. You can write virtually any networking application you can think of. If you would like to go more in depth with writing client server or other networking programs check out the links below.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

Reply

This is very cool. But, for me, it wasn't working (problem was in port 80).
So I needed to change it to something else ex. 20050
Then it worked.
But I needed to change web address too.
It was

    http://localhost:20050
    Anyway, veeery good program.

I need a log in tracking

I need a log in tracking system that needs multiple clients, the moment they log in, the log in time will post to the server.. no specific number of clients because, it depends.. I hope you can help me.. Thank you.. My email is open for your response. Thank you..

Problem

How come if I visit the server once it is OK, but if I visit it twice, my browser displays "The connection to the server was reset whilst loading". Is there a solution to this?

hghgh

hghgh

So, is it possible?

With this control, would it be possible to make an update button on an aplication, that goes to a server(like one used to hosts websites) and a sertain page shows like V 1.0.1 and since that current program is V 1.0.0, it tells the user that there is an update?

Also, would it be able to go and compare lets say highscores on a server, liek the one mentioned above?

Need Help

hi i want to host my database program with Ms access on localhost...plz help me in it and if possible plz send solution on my email id...just_omer4u@yahoo.com